views:

64

answers:

1

I am still kind of fuzzy on controllers in rails, especially so because a lot of things seem to happen magically behind the scenes, and that's not happening in this case.

So say I have a person model, which has many photos (using paperclip) and has many favorite quotes. The quotes can have the text, the attributed author, etc. In both of those models, they are set as belonging to my person model.

Within a new person form, I used some code elsewhere to create a new photo:

<% form.fields_for :screenshots, :html => { :multipart => true }  do |screen_form| %>
    <%= render :partial => 'screenshot', :locals => { :form => screen_form } %>
<% end %>

The partial for that is very simple, like this (minus some ajax javascript stuff I put in for nested models):

<%= form.label :photo, "Screenshot:" %>
<%= form.file_field :photo %>

This all works fine and magically the ID of the person is associated with a screenshot upon creation in person_id. I don't even have a controller for screenshots and it still works.

However, it's not working for my quotes.

<% remote_form_for :quote, :html => { :method => :put }, :url => {:controller => "quote", :action => "create", :person_id => @person.id}  do |quote_form| %>
<%= render :partial => 'quote', :locals => { :form => quote_form } %>
 <% end %>

The partial for this is also very simple.

  <%= form.label :quote_text %>
  <%= form.text_field :quote_text %>
    .........
 <%= form.submit 'Create' %>

I am not really sure if I can put person ID in there, but it didn't complain. However it didn't work, either. The quotes controller is very simple.

  def create
    @quote = Quote.create(params[:quote])
  end

Currently it gets put in the DB but person_id is not populated so I can't pull up the quotes associated with a particular person. Sorry if this is a silly question, but I'm kind of learning Rails by tweaking tutorials and mashing them together so bear with me :) It's just kind of mysterious how the photo thing works with NO controllers or special stuff and this doesn't.

A: 

The first form is a person form mainly that has snapshots fields associated to it, so looking at your HTML you will find something like person[snapshots][photo], this form will be submitted to person controller.

Passing person id to second form the is key to make it work, however it's a bit weird that it's not working, the form will submit to quote controller. Did you make sure(watch the log) that the params hash has person_id attribute?

khelll
I just checked out the hash and the app_id is outside the quotes hash. so something like this:{"commit"=>"Create", "authenticity_token"=>"3l5x22JGrXw+Wk1r9JLEK3em/LMKKaC7Z8Emv0bmlfw=", "person_id"=>"2", "quote"=>{...}Good catch. Not sure off the top of my head if I can figure out how to get it in there but I'll try..
Stacia
Someone told me to use a hidden form value and that worked. In the partial:<%= form.hidden_field :person_id, :value => @person.id %>
Stacia