我正在尝试在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&#39;为零: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
答案 0 :(得分:3)
无需通过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
希望它有所帮助。
答案 1 :(得分:0)
注意:这不是初始问题的答案,这是错误的修复(但对于评论来说太大了)
错误是因为,current_user
返回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