获得"未定义的方法`permit'为"创造""提交表单时出错

时间:2017-03-22 00:11:38

标签: ruby-on-rails

当我尝试创建一个" Action"我收到了这个错误。

我的动作控制器:

class ActionsController < ApplicationController

  def new
    @match_set = MatchSet.find(params[:match_set_id])
    @fixture = @match_set.fixture
    @teams = Team.where("id = " + @fixture.home_team_id.to_s + " OR id = " + @fixture.away_team_id.to_s)
    @players = Player.where("team_id = " + @teams.ids.first.to_s + " OR id = " + @teams.ids.last.to_s)
    @action = @match_set.actions.new
  end

  def create
    @match_set = MatchSet.find(params[:match_set_id])
    @action = @match_set.actions.new(action_params)

    if @action.save
      redirect_to match_set(@match_set)
    else
      render "actions/new"
    end
  end

  private
    def action_params
      params.require(:action).permit(:team_id, :player_id, :position, :action_type, :action_result)
    end
end

它是从views / actions / new.html.erb中的此表单提交的:

<%= form_for [@match_set, @action] do  |f| %> 
<%= render 'shared/error_messages', object: f.object %>
<!-- Team -->
<%= f.label :team_id %>
<%= f.collection_select :team_id, @teams,:id, :name, {include_blank: "Select team..."}, {class: "form-control"} %>

<!-- Player -->
<%= f.label :player_id %>
<%= f.collection_select :player_id, @players,:id, :last_name, {include_blank: "Select player..."}, {class: "form-control"} %>

<!-- Position -->
<%= f.label :position %>
<%= f.select :position, options_for_select(['1', '2', '3', '4', '5', '6']), {include_blank: "Select position on court..."}, class: "form-control" %>

<!-- Action Type -->
<%= f.label :action_type %>
<%= f.select :action_type, options_for_select(['Attack', 'Block', 'Pass', 'Set']), {include_blank: "Select action_type..."}, class: "form-control" %>

<!-- Action Result -->
<%= f.label :action_result %>
<%= f.select :action_result, options_for_select(['Score', 'Block', 'Pass', 'Set']), {include_blank: "Select action_type..."}, class: "form-control" %>

<!-- Submit Button -->
<%= f.submit "Add Action", class: "btn btn-primary" %>
<% end %>

还有相关路线:

resources :fixtures, shallow: true do
    resources :match_sets, shallow: true do
      resources :actions
    end
  end

我在控制器中的这一行收到错误:

params.require(:action).permit(:team_id,:player_id,
:position,:action_type,:action_result)

我也注意到我的参数似乎正在消失,但对原因一无所知。

参数:

{"utf8"=>"✓", "authenticity_token"=>"BqbEOfL7hEA8XSMXDvMW2qQ2uR74Egp5
jJvtQlsuyV2TikZJ+6hTIEMH05gy8TM6r3ZglFDRUFBl7ScZD1czCQ==", 
"commit"=>"Add Action", "match_set_id"=>"15"}

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

action是rails中的保留字。您需要更改型号名称。

Rails在每个请求上提供了一些参数,例如:

params[:controller] # maps to your controller name
params[:action]     # maps to an action with your controllers

http://api.rubyonrails.org/classes/ActionDispatch/Routing.html