ruby - rails post params getting a id not found -


i'm getting following error when posting form: couldn't find team without id

i have below post parameters

{"utf8"=>"✓",  "_method"=>"put",  "authenticity_token"=>"knq4dg1u/5njxmd6kyxfopkd3cuobhrlp6xcwdpwcnq=",  "match"=>{"name"=>"latest match",  "date(1i)"=>"2013",  "date(2i)"=>"5",  "date(3i)"=>"19",  "teams_attributes"=>{"1368967240149"=>{"name"=>"navi",  "id"=>"1"}}},  "commit"=>"update match",  "match_id"=>"2"} 

model:

team has_many :matchips  team has_many :matches :through => matchips  match has_many :matchips match has_many :teams :through => matchips 

teams controller:

  def create       @team = team.find(params[:team_id])  <-----fails here!       redirect_to @match    end 

form:

<%= nested_form_for @match, :url => {:action => "add_team_to_match_post"} |f| %>   <% if @match.errors.any? %>     <div id="error_explanation">       <h2><%= pluralize(@match.errors.count, "error") %> prohibited match being saved:</h2>        <ul>       <% @match.errors.full_messages.each |msg| %>         <li><%= msg %></li>       <% end %>       </ul>     </div>   <% end %>    <div class="field">     <%= f.label :name %><br />     <%= f.text_field :name %>   </div>   <div class="field">     <%= f.label :date %><br />     <%= f.date_select :date %>   </div>    <%= f.fields_for :teams, :html => { :class => 'form-vertical' } |builder| %>   <%= builder.label "team name:" %>     <%= builder.autocomplete_field :name, autocomplete_team_name_teams_path, :update_elements => {:id => "##{form_tag_id(builder.object_name, :id)}" },:class => "input-small",:placeholder => "search" %>   <%= builder.hidden_field :id %>    <% end %>    <%= f.link_to_add raw('<i class="icon-plus-sign"></i>'), :teams, :class => 'btn btn-small btn-primary' %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %> 

so have many-to-many relationship between matches , teams. using nested_form matches, issue it's looking association before creating or updating, want it. done many 1 association child model created when parent gets created or updated, on occasion have teams i'm doing passing id , name via post/put request can associate match.

if there better way associate created entities please let me know.

you need like:

def add_team_to_match_post      @match = match.find(params[:match_id])      params[:teams_attributes].each_value |team_attributes|       team = team.find(team_attributes[:id])       @match.teams << team       team.matches << @match     end      redirect_to @match end 

basically, iterating through every team in teams_attributes hash , adding match object (since many-to-many)


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -