首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图创建一个索引,该索引仅显示该用户使用user创建的任务

试图创建一个索引,该索引仅显示该用户使用user创建的任务
EN

Stack Overflow用户
提问于 2018-09-25 15:22:36
回答 1查看 59关注 0票数 0

代码语言:javascript
复制
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不存在

显示红色。是否需要更改创建方法?如果是的话,怎么做?谢谢

代码语言:javascript
复制
<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>
代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-25 15:31:49

PG::未定义柱:错误:列tasks.user_id不存在

此错误意味着tasks表没有列user_id

解决方案:

生成一个将user_id添加到tasks的迁移

代码语言:javascript
复制
rails g migration add_user_id_to_tasks user_id:integer

rake db:migrate

试图创建一个索引,该索引仅显示该用户使用user创建的任务

在迁移之后,将tasks_controllertasks_controller方法更改为下面

代码语言:javascript
复制
def index
  @task= Task.new
  @tasks = current_user.tasks #this will give the tasks of the current user
end

更新:

(0.2ms)回滚

您有一个回滚,这意味着记录没有保存。这可能是因为user_idparams中为零,而@task.save在没有引发异常的情况下默默地失败。为了解决这个问题,请添加以下内容

代码语言:javascript
复制
def create
  @task = Task.new(task_params)
  @task.user_id = current_user.id # add this line
  @task.save
  redirect_to tasks_path
end

和白名单:user_idtask_params方法中。

代码语言:javascript
复制
def task_params
  params.require(:task).permit(:name, :description, :user_id)
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52501682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档