2017-12-27 12 views
0

사용 장치와 작업을 만들 수 없습니다 및 작업 내 모델 사용자는 유증

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 

과를 만들 수 없습니다 작업을 만들려면 오류가 있음

(210)

1 오류가 저장되는이 작업을 금지 :

User must exist 

가 2017년 12월 27일 14시 20분 59초에서 127.0.0.1에 대한 "/ 작업"POST를 시작 내 응답 TasksController #에 의해 0200 처리 HTML 매개 변수로 생성 : { "UTF8"=> "✓", "authenticity_token"=> "b7EkQsJygYBW1xLIm1uFD8jluXy2LYeoYjAOjKcwWOMHLwtalXmkTrNJu0yhexucwY94COegDcuVrOWLRkf8dg ==", "작업"=> { "제목"=> "", "설명 (2i) "=>"12 ","due (3i) "=>"27 ","우선 순위 "=>" ","due (1i) "=>"2017 " , "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 

무엇이 잘못 되었나요?

답변

0

변경 belongs_to :user에서 belongs_to :user, optional: true으로 변경하십시오. Rails 5는 연관성에 대한 기본 유효성 검사 기능을 도입했습니다. 예약어의 전체 목록이없는 컨트롤러에서 작성하는 방법에

+0

작동하지만 올바르게 작동하지 않습니다. 내가 new_task_view에 추가 할 때

\n <%= form.label :user_id %>\n <%= form.text_field :user_id, id: :task_done %>\n
\ n 그것은 어떻게 작동하지만, 나는 handly put 사용자의 ID가 필요하다. 나는 그것을 자동적으로하고 싶어한다. – asda111sd

0

이 작업은 레일에서 https://reservedwords.herokuapp.com/words/task?q[word_or_notes_cont]=task

에 따라 모델에 대한 좋은 이름이 아니라는 것을, @task.user_id = current_user.id

+0

이것은 질문에 대한 답을 제공하지 않는다. 비평하거나 저자의 설명을 요청하려면 게시물 아래에 의견을 남겨 둡니다. - [검토 중] (리뷰/저품절 게시물/18362806) – Mamun

+0

답변입니다. 더 많은 설명을 사용할 수는 있지만 대답 일뿐입니다. –

0

이주의 추가해야 더 나은 이해를 위해 this link를 참조하십시오 언어와 프레임 워크의 성격 때문에. 그러나 때로는 충돌 키워드를 사용할 때 이상한 행동을 보일 수 있습니다.

0

당신은 당신의 코드에서 수정

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 

, 당신이 사용할 수있는 코드에 다음과 같은

하려고

task를 만들기위한 user_id 누락 한

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 

희망 도와달라고