在我的路线文件中,我有这个嵌套资源"动作"
resources :jobs do
resources :actions
end
相关模型。忽略" action_reference"。那是别的东西。
class Job < ActiveRecord::Base
has_many :actions
end
class Action < ActiveRecord::Base
belongs_to :job
belongs_to :action_reference
end
我正在尝试使用button_to
发出POST请求来创建新动作这里是ActionsController
class ActionsController < ApplicationController
before_action :set_job
before_action :set_action, only: [:show, :edit, :update]
# GET /jobs/:job_id/actions/:id
def show
end
# GET /jobs/:job_id/actions/new
def new
@action = Action.new
end
# GET /jobs/:job_id/actions/:id/edit
def edit
end
# POST /jobs/:job_id/actions/
def create
@action = @job.actions.create(action_params)
if @action.save
flash[:success] = "Next step successfully added."
redirect_to jobs_path
else
flash[:danger] = @action.errors.full_messages.join(", ")
redirect_to new_job_action_path
end
end
# PATCH to /jobs/:job_id/actions/:id
def update
if @action.update(action_params)
flash[:success] = "Next step successfully updated."
redirect_to jobs_path
else
flash[:danger] = @action.errors.full_messages.join(", ")
redirect_to edit_job_action_path
end
end
private
def set_job
@job = Job.find(params[:job_id])
end
def set_action
@action = Action.find(params[:id])
end
def action_params
params.require(:action).permit(:action_reference_id, :job_id, :completed_at, :next_action_date)
end
end
这是我的button_to
<%= button_to answer[:text], post_action_jobs_path(@job), method: "post", params: { action: { action_reference_id: answer[:action_reference_id], job_id: @job_id , completed_at: answer[:action_completed_at], next_action_date: answer[:next_action_date] } }, type: "button", class: "btn btn btn-info btn-block" %>
我知道这个问题与我在视图中传递给post_action_jobs_path的参数或我在控制器中传递给action_params的参数有关,但我无法理解进行。
当我运行时,我收到错误:
undefined method `permit' for "create":String Did you mean? print
前段时间我看到一些帖子说了一些关于&#34;动作&#34;在Rails中是一个保留字,所以你必须使用其他东西,但如果这是真的我不知道如何去做。
非常感谢任何帮助:)
答案 0 :(得分:0)
答案 1 :(得分:0)
是的,不幸的是,这是由于&#34;行动&#34;是rails控制器中的现有方法。它用于获取已调用的操作的名称。在这种情况下&#34;行动&#34;将等于字符串&#34;创建&#34;。
您可以做的一件事是将Action模型重命名为JobAction并使用params.require(:job_action)