我在尝试ActiveModel::ForbiddenAttributesError
新create
时收到task
。我想我已经消除了所有愚蠢的错误,所以我很乐意帮助搞清楚。
我的tasks_controller
如下:
class TasksController < ApplicationController
def index
@tasks = current_user.Task.all
end
def create
@task = Task.new(params[:task]) <<<<<<<<<<<THIS LINE
@task.user = current_user
if @task.save
flash[:notice] = "Your exercise results were saved!"
redirect_to tasks_path
else
render :action => 'new'
end
end
def new
@task = Task.new
end
def edit
@task = Task.find(params[:id])
end
def update
@task = Task.find(params[:id])
if @task.update_attributes(params[:task]) <<<<<<<<<<<THIS LINE
redirect_to tasks_path
end
def destroy
@task = Task.find(params[:id])
@tasks.destroy
redirect_to tasks_path
end
def show
@task = Task.find(params[:id])
end
private
def task_params
params.require(:task).permit(:name, :reps, :weight, :comments, :user_id)
end
end
end
在两条指示的行中,我尝试了params[:task]
和'task_params'。第一个抛出ForbiddenAttributesError
;后者为#`。
undefined local variable or method
task_params'
这是我的tasks#new
erb:
<%= form_for @task do |f| %>
<div class="col-xs-12">
<%= f.label "Exercise Name" %>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="col-xs-6">
<%= f.label "Reps" %>
<%= f.number_field :reps, class: "form-control" %>
</div>
<div class="col-xs-6">
<%= f.label "Weight" %>
<%= f.text_field :weight, class: "form-control" %>
</div>
<div class="col-xs-12">
<%= f.label "Comments/Notes" %>
<%= f.text_field :comments, class: "form-control" %>
</div>
<div class="text-center"><%= f.submit "Record Exercise", class: "btn-primary" %></div>
<% end %>
最后,我schema
的{{1}}如下,所以我知道变量的名称是正确的:
tasks
任何人都可以指导我修复这种“Ruby方式”吗?
答案 0 :(得分:0)
试试这个:
def create
@task = Task.new(task_params) ##change here
@task.user = current_user
if @task.save
flash[:notice] = "Your exercise results were saved!"
redirect_to tasks_path
else
render :action => 'new'
end
end