Seth Jackson
Seth Jackson

Reputation: 539

Rails nested resource on a singular resource form

I'm having a problem with Rails not POSTing anything in the params to an action. I'm using a singular resource with a nested plural resource which may or may not be where the problem is coming from (Rails has issues with form_for and singular resource URLs).

Anyway, I have this in my routes:

resource :event do
    resources :actions, :only => [:create], :controller => "events/actions"
end

The view:

<%= form_for([@event, Action.new], :remote => true) do |f| %>
  <div class="field">
    <%= f.label :team_id %>
    <br />
    <%= f.text_field :team_id %>
  </div>
  <div class="field">
    <%= f.label :message %>
    <br />
    <%= f.text_field :message %>
  </div>
  <div class="field">
    <%= f.label :score %>
    <br />
    <%= f.number_field :score %>
  </div>
  <br />
  <%= f.submit "Update score" %> or <%= link_to "cancel", "#", :id => "cancel" %>
<% end %>

The create action:

def create
    @event = Event.find(params[:event_id])
    @action = @event.actions.create(params[:action])
end

Ok pretty standard no worries there. But when I get the params from Rails nothing is there. :(

Params:

Started POST "/event/actions.4e67f09349ae71090c00000e"
Processing by Events::ActionsController#create as
Parameters: {"utf8"=>"Γ£ô", "authenticity_token"=>"stuff", "commit"=>"Update score"}

Completed 500 Internal Server Error in 31ms

What is going on here?

Edit:

If I remove the ":remote => true" line in my view, I see that in my params I get one param ":format" which appears to be the ID of the event.

However, I'm still not getting the action params. :(

Upvotes: 1

Views: 668

Answers (1)

PlankTon
PlankTon

Reputation: 12605

Ideally I'd like to see those event & action models - I suspect that's where the problem lies. Without seeing those, a few suggestions:

  • Is 'accepts_nested_attributes_for :action' set in the event model?

  • Remove any 'attr_accessible' line from both models & see if things work. (Keep in mind you need to set accessible attributes for nested forms in the parent model)

  • 'Action' seems like an imprudent name for a model. It's possible rails is overwriting 'action' methods with things related to the actual action

Hope this helps - I'd suggest posting the models if you still can't find a solution.

Upvotes: 1

Related Questions