Rails post params gets identifier not found

When I submit the form, I get the following error: Could not find a command without identifier

I have the following message options

{"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 

Command controller:

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

the form:

 <%= nested_form_for @match, :url => {:action => "add_team_to_match_post"} do |f| %> <% if @match.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@match.errors.count, "error") %> prohibited this match from being saved:</h2> <ul> <% @match.errors.full_messages.each do |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' } do |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, I basically have a many-to-many relationship between matches and teams. using nested_form for matches, but the problem is that it is looking for an association before creating or updating, I want it. I previously did this with many to the same association where the child model will be created, when the parent is created or updated, in this case I already have the commands that I do is passing the identifier and name through the post / put request so I can bind him with this match.

If there is a better way to link already created objects, then please let me know.

+4
source share
4 answers

You will probably need something like:

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

Basically, this is repeated through each team in the teams_attributes hash and adds it to the matching object (since it's many-to-many)

+2
source

After viewing and using team_attributes, the id key is the identifier of the team.

  "match"=>{"name"=>"latest match", "date(1i)"=>"2013", "date(2i)"=>"5", "date(3i)"=>"19", "teams_attributes"=>{"1368967240149"=>{"name"=>"Navi", "id"=>"1"}}} 

Do it in the controller

  team_key = params['match']['teams_attributes'].keys.first team_id = params['match']['teams_attributes']['team_key']['id'] Team.find(team_id) 

OR

Another choice is available to do the same thing.

If you select an autocomplete command, then after selecting the ajax command, you can also add a hidden field with the name team_id , and this value will be selected by the id command.

then you can easily access the parameters [: team_id]


Let me offer you a new way of thinking.

what if if you think that the match belongs to many teams remove the middle table

and in accordance with the new form and the editing form, select two teams whose coincidence you want to keep.

for reference look at this http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Why am I asking you to do this? Answer: you need to create a match between existing teams and do not need to save a match in the team.

But when you save the match you need to save the team.

Hope this medicine

+2
source

change params[:team_id] to params[:teams_attributes][:id]

0
source

has_many for matches <> Teams are overkill. See this has_two relationship bullying solution. This will simplify your forms and URLs quite a bit.

0
source

Source: https://habr.com/ru/post/1481627/


All Articles