0
votes

Using device and can't create task my models user

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

task

class Task < ApplicationRecord
  belongs_to :user
end

task controller

  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

and when trying to create task have an error

1 error prohibited this task from being saved:

User must exist

my response

Started POST "/tasks" for 127.0.0.1 at 2017-12-27 14:20:59 +0200 Processing by TasksController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"b7EkQsJygYBW1xLIm1uFD8jluXy2LYeoYjAOjKcwWOMHLwtalXmkTrNJu0yhexucwY94COegDcuVrOWLRkf8dg==", "task"=>{"title"=>"", "description"=>"", "priority"=>"", "due(1i)"=>"2017", "due(2i)"=>"12", "due(3i)"=>"27", "done"=>"0"}, "commit"=>"Create Task"}

  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

what's wrong?

4

4 Answers

0
votes

Change belongs_to :user to belongs_to :user, optional: true. Rails 5 has introduced default validation in associations. Refer this link for better understanding

0
votes

in method create in controller need to add @task.user_id = current_user.id

0
votes

Be aware, that Task is not good name for model according to https://reservedwords.herokuapp.com/words/task?q[word_or_notes_cont]=task

In Rails there is not full list of reserved words because of the nature of the language and framework. But sometimes you can face strange behaviour, when you use conflicting keyword.

0
votes

You have missing user_id for creating task

Try to the following

On your code

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

Modified from your code, you can use that

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

Hope to help