【问题标题】:Can't create task with devise无法使用设计创建任务
【发布时间】:2018-06-08 02:23:53
【问题描述】:

使用设备无法创建任务 我的模型 用户

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :tasks
end

任务

class Task < ApplicationRecord
  belongs_to :user
end

任务控制器

  def create
    @task = Task.new(task_params)

    respond_to do |format|
      if @task.save
        format.html { redirect_to @task, notice: 'Task was successfully created.' }
        format.json { render :show, status: :created, location: @task }
      else
        format.html { render :new }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end

在尝试创建任务时出现错误

1 个错误禁止保存此任务:

User must exist

我的回应

在 2017-12-27 14:20:59 +0200 为 127.0.0.1 开始 POST "/tasks" TasksController#create 作为 HTML 处理 参数:{"utf8"=>"✓", "authenticity_token"=>"b7EkQsJygYBW1xLIm1uFD8jluXy2LYeoYjAOjKcwWOMHLwtalXmkTrNJu0yhexucwY94COegDcuVrOWLRkf8dg==", "task"=>{"title"=>"", "description"=>"", "priority"=> "", "到期(1i)"=>"2017", "到期(2i)"=>"12", "到期(3i)"=>"27", "完成"=>"0"}, "提交"=>"创建任务"}

  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 2], ["LIMIT", 1]]
   (0.0ms)  begin transaction
   (0.1ms)  rollback transaction

怎么了?

【问题讨论】:

    标签: ruby-on-rails ruby devise rubygems


    【解决方案1】:

    belongs_to :user 更改为belongs_to :user, optional: true。 Rails 5 在关联中引入了默认验证。请参考this link 以获得更好的理解

    【讨论】:

    • 它工作但不正确。当我添加到 new_task_view
      \n \n \n
      \ n 它按我的意愿工作,但我需要方便地放置用户的 ID。我想自动这样做
    【解决方案2】:

    在controller的create方法中需要添加@task.user_id = current_user.id

    【讨论】:

    • 这是一个答案。它可以使用更多的解释,但它是一个答案。
    【解决方案3】:

    请注意,根据https://reservedwords.herokuapp.com/words/task?q[word_or_notes_cont]=task,Task 不是模型的好名称

    由于语言和框架的性质,Rails 中没有完整的保留字列表。但有时当您使用冲突的关键字时,您可能会遇到奇怪的行为。

    【讨论】:

      【解决方案4】:

      您缺少用于创建 taskuser_id

      尝试以下方法

      在你的代码上

      def create
        @task = Task.new(task_params)
      
        respond_to do |format|
          if @task.save
            format.html { redirect_to @task, notice: 'Task was successfully created.' }
            format.json { render :show, status: :created, location: @task }
          else
            format.html { render :new }
            format.json { render json: @task.errors, status: :unprocessable_entity }
          end
        end
      end
      

      从你的代码修改,你可以使用它

      def create
        @task = Task.new(task_params)
        @task.user = current_user
      
        respond_to do |format|
          if @task.save
            format.html { redirect_to @task, notice: 'Task was successfully created.' }
            format.json { render :show, status: :created, location: @task }
          else
            format.html { render :new }
            format.json { render json: @task.errors, status: :unprocessable_entity }
          end
        end
      end
      

      希望能帮到你

      【讨论】:

        猜你喜欢
        • 2017-07-02
        • 2022-12-11
        • 1970-01-01
        • 2021-08-25
        • 2021-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多