如何将 user_id 的隐藏字段传递给评论?
How to pass hidden field of user_id to comment?
我正在尝试在 comments/_form
中添加一个隐藏字段,以便用户的姓名显示在评论部分。我做什么都没用。我真的很想使用隐藏字段,所以我知道如何让它发挥作用。
用户有很多评论,评论属于用户。
这是我目前在 comments/_form
:
<%= form_for([@place, @place.comments.build]) do |f| %>
<%= hidden_field :user_id, :value => @user.id %>
<p>
<%= f.label :body %><br>
<%= f.text_field :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
这是我目前在_comment.html.erb
中的内容,请注意comment.user.first_name
。
这是 undefined method
first_name' 对于 nil:NilClass`:
的错误
<p>
<strong>Comment:</strong><br />
<%= comment.user.first_name %> wrote:<br /><br>
<%= comment.body %>
</p>
<p>
<%= link_to 'Destroy Comment', [comment.place, comment],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
这是我在 comments_controller
class CommentsController < ApplicationController
def create
@place = Place.find(params[:place_id])
@comment = @place.comments.create(comment_params)
redirect_to place_path(@place)
end
def destroy
@place = Place.find(params[:place_id])
@comment = @place.comments.find(params[:id])
@comment.destroy
redirect_to place_path(@place)
end
private
def comment_params
params.require(:comment).permit(:body, :user_id)
end
end
这是我在 ApplicationController
中的内容:
helper_method :current_user
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
有人有什么建议吗?
编辑我正在添加更多内容以供启发,以防有帮助。
用户控制器
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to root_url, :notice => "Signed Up!"
else
render "new"
end
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
会话控制器
class SessionsController < ApplicationController
def new
end
def create
user = User.authenticate(params[:email], params[:password])
if user
session[:user_id] = user.id
redirect_to places_path, :notice => "Logged in!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Logged out!"
end
end
放置控制器
class PlacesController < ApplicationController
def index
@places = Place.all
end
def new
@place = Place.new
end
def create
@place = Place.new(place_params)
if @place.save
redirect_to @place
else
render 'new'
end
end
def show
@place = Place.find(params[:id])
end
def edit
@place = Place.find(params[:id])
end
def update
@place = Place.find(params[:id])
if @article.update(place_params)
redirect_to @place
else
render 'edit'
end
end
def destroy
@place = Place.find(params[:id])
@place.destroy
redirect_to places_path
end
end
private
def place_params
params.require(:place).permit(:name, :location)
end
无需通过 hidden_field 发送,而是将其删除。
重写你的 create
方法如下:
def create
@place = Place.find(params[:place_id])
@comment = @place.comments.create(comment_params)
@comment.user = current_user
if @comment.save
redirect_to place_path(@place)
else
render "comments/_form"
end
end
希望对您有所帮助。
注意:这不是最初问题的答案,这是对错误的修复(但对于评论来说太大了)
错误是因为,current_user
returns nil
,根据您的要求,有两个选项,
1)
CommentsController < ApplicationController
before_filter :current_user
...
end
2)
ApplicationController < ActionController::Base
before_filter :current_user
.....
end
----更新方法
def current_user
if session[:user_id]
@current_user ||= User.find(session[:user_id]) if session[:user_id]
else
#redirect user to login to set the session[:user_id] variable
end
end
我正在尝试在 comments/_form
中添加一个隐藏字段,以便用户的姓名显示在评论部分。我做什么都没用。我真的很想使用隐藏字段,所以我知道如何让它发挥作用。
用户有很多评论,评论属于用户。
这是我目前在 comments/_form
:
<%= form_for([@place, @place.comments.build]) do |f| %>
<%= hidden_field :user_id, :value => @user.id %>
<p>
<%= f.label :body %><br>
<%= f.text_field :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
这是我目前在_comment.html.erb
中的内容,请注意comment.user.first_name
。
这是 undefined method
first_name' 对于 nil:NilClass`:
<p>
<strong>Comment:</strong><br />
<%= comment.user.first_name %> wrote:<br /><br>
<%= comment.body %>
</p>
<p>
<%= link_to 'Destroy Comment', [comment.place, comment],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
这是我在 comments_controller
class CommentsController < ApplicationController
def create
@place = Place.find(params[:place_id])
@comment = @place.comments.create(comment_params)
redirect_to place_path(@place)
end
def destroy
@place = Place.find(params[:place_id])
@comment = @place.comments.find(params[:id])
@comment.destroy
redirect_to place_path(@place)
end
private
def comment_params
params.require(:comment).permit(:body, :user_id)
end
end
这是我在 ApplicationController
中的内容:
helper_method :current_user
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
有人有什么建议吗?
编辑我正在添加更多内容以供启发,以防有帮助。
用户控制器
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to root_url, :notice => "Signed Up!"
else
render "new"
end
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
会话控制器
class SessionsController < ApplicationController
def new
end
def create
user = User.authenticate(params[:email], params[:password])
if user
session[:user_id] = user.id
redirect_to places_path, :notice => "Logged in!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Logged out!"
end
end
放置控制器
class PlacesController < ApplicationController
def index
@places = Place.all
end
def new
@place = Place.new
end
def create
@place = Place.new(place_params)
if @place.save
redirect_to @place
else
render 'new'
end
end
def show
@place = Place.find(params[:id])
end
def edit
@place = Place.find(params[:id])
end
def update
@place = Place.find(params[:id])
if @article.update(place_params)
redirect_to @place
else
render 'edit'
end
end
def destroy
@place = Place.find(params[:id])
@place.destroy
redirect_to places_path
end
end
private
def place_params
params.require(:place).permit(:name, :location)
end
无需通过 hidden_field 发送,而是将其删除。
重写你的 create
方法如下:
def create
@place = Place.find(params[:place_id])
@comment = @place.comments.create(comment_params)
@comment.user = current_user
if @comment.save
redirect_to place_path(@place)
else
render "comments/_form"
end
end
希望对您有所帮助。
注意:这不是最初问题的答案,这是对错误的修复(但对于评论来说太大了)
错误是因为,current_user
returns nil
,根据您的要求,有两个选项,
1)
CommentsController < ApplicationController
before_filter :current_user
...
end
2)
ApplicationController < ActionController::Base
before_filter :current_user
.....
end
----更新方法
def current_user
if session[:user_id]
@current_user ||= User.find(session[:user_id]) if session[:user_id]
else
#redirect user to login to set the session[:user_id] variable
end
end