views:

117

answers:

1

Woo. My first question.

I have a feeling I'm overlooking something pretty basic in the construction of my form. I'm using attachment_fu and can't get this form to pass anything besides the file data. A user has_many profiles and a profile has_many documents.

My form looks like this:

<%= error_messages_for :document %>

<% form_for([@user, @profile, @document], :html => {:multipart => true }) do |f| -%>
  <p>
    <label for="document">Upload A document:</label>
    <%= f.file_field :uploaded_data %>
  </p>
 <%= f.label :description%>
 <%= f.text_field :description%>
  <p>
    <%= submit_tag 'Upload' %>
  </p>
<% end -%>

And here's the controller:

  before_filter :require_user, :get_profile

  def new
    @document = @profile.documents.build
  end

  def create
    @document = @profile.documents.build(params[:document])

    if @document.save
      flash[:notice] = 'Your document was successfully created.'
      redirect_to document_url(@document)     
    else
      render :action => :new
    end
  end

  private

  def get_profile
    @user = current_user
    @profile = @user.profiles.find(params[:profile_id])
  end

The logs show all the image data getting posted, but I cannot pass the description or, more importantly, the profile_id, which is the foreign key in my document model. I was stuck on this all night, and can't think of anything fresh this morning. Any help would be great.

A: 

For the profile_id you will need something like:

<%= f.hidden_field :profile_id %>

Which in your controller you will get at using params[:document][:profile_id] if needed. Although from trying to guess at what your code is doing, I suspect that params[:profile_id] is already set from whatever route got you to this controller.

I am not sure why you aren't seeing anything for the description. It should be coming into your controller as params[:document][:description].

Aaron Hinni
Thanks Aaron. You're right, profile_id is set through the routes. After some more looking/playing around I think it has something to do with the attachment_fu plugin and the way it defends against mass assignment: https://ar-code.lighthouseapp.com/projects/35/tickets/37-mass-assignment-attr_accessibleI'm still not sure exactly how to handle it, but it's a start...
a-dilla
Not sure if you have any specific requirements for attachment_fu, but you might find paperclip easier to deal with: http://github.com/thoughtbot/paperclip
Aaron Hinni
Played around with attachment_fu for another few hours, thought I had something, but didn't. Paperclip was stupid-simple to get going, thanks for the tip.
a-dilla