Question

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.

Was it helpful?

Solution

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].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top