하나의 속성을 제외하고 모두 저장하는 양식이 있습니다. : user_id (외래 키)는 nil로 계속 나오게됩니다. 이것은 새로운 주석을 만들기위한 형식이며, 문제의 일부일 수있는 # show file에 있습니다. 다른 속성 (: name 및 : impression_id)은 괜찮은 것으로 보입니다. 나는 다음과 같은 한의 comments_controller에서서식 하나의 속성을 저장하지 못했습니다
def show
@impression = Impression.find(params[:id])
@play = Play.find_by(id: @impression.production.play_id)
@production = Production.find_by(id: @impression.production_id)
@comments = Comment.where(impression_id: @impression.id)
@comment = Comment.new
end
:
def new
@impression = Impression.find(params[:impression_id])
@comment = Comment.new(comment_params)
authorize @comment
end
def create
@comment = Comment.new(comment_params)
authorize @comment
if @comment.save
flash[:notice] = 'You have successfully added a comment.'
redirect_to :back
else
flash[:notice] = 'Please try again.'
render :new
end
end
private
def comment_params
params.require(:comment).permit(:body, :impression_id, :user_id)
end
당신이 볼 수 있듯이
= form_for @comment do |f|
= f.label 'Add Comment'
%br
= f.text_area :body, placeholder: 'Comment', cols: 50, rows: 3
%br
= f.hidden_field :impression_id, :value => @impression.id
= f.hidden_field :user_id, :value => current_user.id
%br
= f.submit "Save"
impressions_controller의 관련 섹션은 다음과 같습니다
폼의 , 나는 user_id에 current_user.id의 값을 할당하려고 시도하고 있지만 취하지는 않습니다. 아무도 내가 누락 된 것을 볼 수 있습니까?내 impressions 컨트롤러에 comment_params를 추가하려고했지만 param 주석이 비어 있다는 오류 메시지가 표시됩니다. . .
감사합니다.
내 current_user에는 값과 ID가 있습니다. . . 그것은 단지 저축하지 않습니다 : 새로운 코멘트에 대한 user_id! –