Started POST "/tasks" for 127.0.0.1 at 2018-09-25 17:25:24 +0100
Processing by TasksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"MnJuOlEcHfPYca7mzWk5WdEO0jVcuS+gGWiFVC++ARNmtpPTjC/9sd7AGpgV8LSybNjz1PoOIdDYddUhnOAFOw==", "task"=>{"name"=>"j", "description"=>"j"}, "commit"=>"Create Task"}
User Load (4.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/benherring/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
(0.3ms) BEGIN
↳ app/controllers/tasks_controller.rb:22
(0.2ms) ROLLBACK
↳ app/controllers/tasks_controller.rb:22
Redirected to http://localhost:3000/tasks
Completed 302 Found in 11ms (ActiveRecord: 4.9ms)
试图创建一个索引,该索引仅显示该用户使用devise创建的任务。在这里看了很多,但找不到解决我的问题的办法。我做过社团活动。当前错误在这一行<% @tasks.each do |task| %>。
PG::未定义柱:错误:列tasks.user_id不存在
显示红色。是否需要更改创建方法?如果是的话,怎么做?谢谢
<h1>TASKS</h1>
<div class="body">
<div class="list">
<% @tasks.each do |task| %>
<ul>
<li>
<%= link_to task.user.name ,task_path(task), class: "tasks" %>
|
<%= link_to "Delete", task_path(task), method: :delete, data: {confirm: "Are you sure?"} %>
</li>
</ul>
<% end %>
</div>
<div class="form">
<div>
<%= simple_form_for @task, id: "form-submit" do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.button :submit, class: "btn btn-danger" ,id: "sweet-alert-demo" %>
<% end %>
</div>
</div>
<%#= link_to "Add Task" ,new_task_path, class: "btn btn-primary" %>
</div>class TasksController < ApplicationController
def index
# @tasks = Task.all
@task= Task.new
@tasks = Task.where(user: current_user)
end
def show
@task = Task.find(params[:id])
end
def new
# @task = Task.new
# @user = current_user
end
def create
@task = Task.new(task_params)
@task.save
redirect_to tasks_path
end
def edit
@task = Task.find(params[:id])
end
def update
@task = Task.find(params[:id])
@task.update(task_params)
redirect_to task_path
end
def destroy
@task = Task.find(params[:id])
@task.destroy
redirect_to tasks_path
end
private
def task_params
params.require(:task).permit(:name, :description)
end
end
发布于 2018-09-25 15:31:49
PG::未定义柱:错误:列tasks.user_id不存在
此错误意味着tasks表没有列user_id。
解决方案:
生成一个将user_id添加到tasks的迁移
rails g migration add_user_id_to_tasks user_id:integer做rake db:migrate
试图创建一个索引,该索引仅显示该用户使用user创建的任务
在迁移之后,将tasks_controller的tasks_controller方法更改为下面
def index
@task= Task.new
@tasks = current_user.tasks #this will give the tasks of the current user
end更新:
(0.2ms)回滚
您有一个回滚,这意味着记录没有保存。这可能是因为user_id在params中为零,而@task.save在没有引发异常的情况下默默地失败。为了解决这个问题,请添加以下内容
def create
@task = Task.new(task_params)
@task.user_id = current_user.id # add this line
@task.save
redirect_to tasks_path
end和白名单:user_id在task_params方法中。
def task_params
params.require(:task).permit(:name, :description, :user_id)
endhttps://stackoverflow.com/questions/52501682
复制相似问题