0

I have an issue retrieving my file upload information. I am just starting to learn Rails.

I am using ruby 2.0.0p0 And Rails 4.0.0.beta1

Here is my form:

<%= form_for(@person, :html => { :multipart => true }) do |f| %>
<div class="field">
  <%= f.label :photo %><br />
  <%= f.file_field :photo %>
</div>
<div class="actions">
  <%= f.submit %>
</div>
<% end %>

And in my person.rb model:

def photo=(file_data)
logger.debug("PHOTO")
logger.debug(file_data)
logger.debug("END OUTPUT PHOTO")
unless file_data.blank?
  @file_data = file_data
  self.extension = file_data.original_filename.split('.').last.downcase
end
end

I can see in my console that nothing happens (no "PHOTO" output), the photo method is never called. Why is that?

When I looked in the console I also saw this line that got me worried:

Unpermitted parameters: photo

What does that mean?

2
  • 2
    If you are just starting to learn Rails, I wouldn't be using the Rails 4 beta until it's officially released. The exact problem you're running into likely wouldn't have happened in Rails 3.2. Commented Mar 31, 2013 at 14:44
  • You are right. I guess I didn't pay attention when I installed Rails. I will try to switch back to 3 (my learning book is for Rails 3 anyway) Commented Mar 31, 2013 at 14:51

1 Answer 1

3

In your controller, where you're dealing with params, you need to use .permit to list the attributes the form is allowed to post:

@person = Person.new(params.require(:person).permit(:photo))

Alternatively, if you used Rails' scaffolding generator, you might instead have a person_params method where you would need to add the :photo attribute:

def person_params
  params.require(:person).permit(:photo, :name, etc...)
end
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, found it. Makes a lot of sense. Thank you.

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.