0

I am very new to rails and attempting to build my first project. I have a problem whilst adding images using paperclip. Basically whenever I attempt to upload an image to a post, it still keeps the "missing" image.Any help is greatly appreciated. Here is my model

 class Pt < ActiveRecord::Base
        validates :name, presence: true
        has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100#" }
        validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
    end


  and my controller

class PersonaltrainersController < ApplicationController
  def index
      @PT = Pt.all
  end

  def new
      @PT = Pt.new
  end

  def show
      @PT = Pt.find(params[:id])
  end
def destroy
    @PT = Pt.find(params[:id])
  @PT.destroy

  redirect_to personaltrainers_path
end
def edit
      @PT = Pt.find(params[:id])
  end

    def update
        @PT = Pt.find(params[:id])
  if @PT.update(params[:pt].permit(:name, :age, :sex, :experience))
      redirect_to personaltrainer_path
        flash[:notice] = "The trainer was updated."
        else
            flash[:error] = "Something went wrong."
            render :edit
        end
            end

  def create
    @PT = Pt.new(params[:pt].permit(:name, :age, :sex, :experience))
      if @PT.save
            flash[:success] = "Successfully added a post."
            redirect_to personaltrainers_path
       else 
            flash[:error] = "Please check the form for errors and try again."
            render :new
        end
 end
      end
  def contact
  end

and my show page

<h1> <%= @PT.name %></h1>
<h2> <%= @PT.age %></h2>
<body>
<%= @PT.experience %>
</body>
<br>
<%= image_tag @PT.image.url()%>
<%= link_to "edit", edit_personaltrainer_path %>
<%= link_to "delete", personaltrainer_path, method: :delete, confirm: true %>
1
  • Please show us a your current form for @PT where you tried to adding image. Commented May 4, 2015 at 21:25

1 Answer 1

1

The problem is with your permit params, this part:

params[:pt].permit(:name, :age, :sex, :experience)

Should include the :image attribute of your Pt model. So, you need this as your permit params:

params[:pt].permit(:name, :age, :sex, :experience, :image)

That should allow your image to be saved correctly. Remember to check your server logs often, they will show warnings such as "Unpermitted parameters: :image" when you forget to add an attribute to your permit params.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.