希望有人能帮忙。我一直在搜索其他“缺少参数”的问题,但似乎无法弄清楚出了什么问题。
在我的路线文件中,我有这个嵌套资源“动作”
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
不久前我看到一些帖子说“动作”是Rails中的保留字,所以你必须使用其他东西,但如果这是真的,我不知道该怎么做。
任何帮助将不胜感激 :)