问题
I am using gem devise for creating users profile
Each user can create a comment. I need to add the user name beside each comment something like this <%= @comment.user.name %>
in user.rb
has_many :comments, dependent: :destroy
in comment.rb
belongs_to :users
in comment controller
before_action :find_comment ,only:[:show,:update,:edit,:destroy]
def new
@user =User.find(params[:id])
@comment = @user.comments.build
end
def create
@user =User.find(params[:id])
@comment = @user.comments.build(comment_params)
@comment.user = current_user
if @comment.save
redirect_to doctor_path(:id => @user.id)
end
end
private
def find_comment
@comment = Comment.find(params[:id])
end
def comment_params
params.require(:comment).permit(:text)
end
user controller
def show
@user = User.find(params[:id])
end
user show.html.erb
<% for item in @user.comments %>
<% if item.text.present? %>
<%= item.text %><br>
<%= @comment.user.name %>
<br><hr>
<% end %>
I got this error
undefined method `user' for nil:NilClass
回答1:
You could do it the other way around, in your show
method:
@comments = Comment.all
in your show
view:
<% @comments.each do |comment| %>
<%= comment.text %>
<%= comment.user.name %>
<% end %>
Since your question is not really clear I'll specifiy that if you want to show just the comments posted by the user:
def show
user_id = User.find(params[:id]).id
@comments = Comment.where(user_id: user_id)
end
回答2:
Just some quick rules to start with
A user has many comments, this will be the relationship between the user and a comment that the user has made. You already have this
A user has many profile comments, this is the relationship between a user and the comments that have been left for that user on their profile
Now you have that distinction things start to be come clearer.
Start by creating a single xref table to act as the go between users and comments that have been left for a profile and call it profile_comments
this profile_comments
table needs a user_id
and a comment_id
of type integer to store the primary keys from user and comments tables, where the user_id
is the id of the user that is having a comment left about them on their profile
You can now setup a profile_comment
model that with the following relationships
belongs_to comment
belongs_to user
So now you need to change your user model relationships to the following
user.rb
has_many :comments
has_many :profile_comments, dependent: :destroy
comment.rb
belongs_to :user #not users as you have defined in your question
has_many :profile_comments, dependent: :destroy
and the new profile_comment.rb
model needs the two belongs_to
clauses for comment and user
profile_comment.rb
belongs_to :user
belongs_to :comment
Now when you create a comment you need to assign to to the user, and also to the profile_comment
So now your comments controller needs to setup these relationships so instead of
before_action :find_comment ,only:[:show,:update,:edit,:destroy]
def new
@user =User.find(params[:id])
@comment = @user.comments.build
end
def create
@user =User.find(params[:id])
@comment = @user.comments.build(comment_params)
@comment.user = current_user
if @comment.save
redirect_to doctor_path(:id => @user.id)
end
end
You need something like this
def create
@user =User.find(params[:id])
@comment = current_user.comments.build(comment_params)
@profile_comment = ProfileComment.new
@user.profile_comment < @profile_comment
@comment.profile_comment < @profile_comment
if @comment.save
redirect_to doctor_path(:id => @user.id)
end
end
Your update action will need to also change accordingly
Now in your view instead of
<% for item in @user.comments %>
<% if item.text.present? %>
<%= item.text %><br>
<%= @comment.user.name %>
<br><hr>
<% end %>
<% end %>
You want this, it's a little complex because you need to get from the profile comment to the comment then to the user that created the comment
<% @user.profile_comments.each do | profile_comment |%>
<%comment = profile_comment.comment%>
<% if comment.text.present? %>
<%= comment.text %><br>
<%if comment.user.blank?%>
No user assigned to this comment
<%else%>
<%= comment.user.name #or email or whatever%>
<%end%>
<br><hr>
<% end %>
<% end %>
Although text is a reserved word and is an actual column type so you might want to change the column name text to something else
Any questions on this feel free to get back to me but I won't be around for the next 24 hours. Hope it's clear and helps you understand what has gone wrong with your initial setup
In the controller and the view I have assumed that the current_user
is the person making the comment and @user
is the person that is being commented on. Just switch that round if I have that wrong
来源:https://stackoverflow.com/questions/62886524/gem-devise-how-to-add-comment-created-user-email