如果用户尚未提交评论,如何才能仅显示添加评论表单?
您好,我是一个超级初学者,正在开发我的第一个应用程序,我有一个场地表,用户可以对场地添加评论。我希望能够在用户提交评论后向他们隐藏评论表单,以阻止他们提交更多评论。
这就是我现在所拥有的:
在场地展示页面上添加审核表格
<% if reviewed? %>
<%= form_for [@venue, @review], :class => 'rating_ballot' do |f| %>
<%= f.label("value_1", content_tag(:span, '1'), {:class=>"rating", :id=>"1"}) %>
<%= radio_button_tag("review[rating]", 1, :class => 'rating_button') %>
<%= f.label("value_2", content_tag(:span, '2'), {:class=>"rating", :id=>"2"}) %>
<%= radio_button_tag("review[rating]", 2, :class => 'rating_button') %>
<%= f.label("value_3", content_tag(:span, '3'), {:class=>"rating", :id=>"3"}) %>
<%= radio_button_tag("review[rating]", 3, :class => 'rating_button') %>
<%= f.label("value_4", content_tag(:span, '4'), {:class=>"rating", :id=>"4"}) %>
<%= radio_button_tag("review[rating]", 4, :class => 'rating_button') %>
<%= f.label("value_5", content_tag(:span, '5'), {:class=>"rating", :id=>"5"}) %>
<%= radio_button_tag("review[rating]", 5, :class => 'rating_button') %> <br>
<p>title: <br>
<%= f.text_field :title %></p><br>
<%= submit_tag %>
<% end %>
<% end %>
应用程序控制器
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
helper_method :reviewed?
protected
def reviewed?
true
end
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
我不知道审核了什么?帮助者应该允许我这样做,非常感谢任何帮助!
编辑
我已将 has_reviewed 帮助程序添加到应用程序控制器,它现在显示此错误:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Extracted source (around line #79):
76: <%= render :partial => 'reviews/review', :collection => @venue.reviews %>
77: </div>
78:
79: <% if reviewed? %>
应用程序控制器
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
helper_method :current_user
helper_method :has_reviewed
helper_method :reviewed?
protected
def reviewed?
Review.has_reviewed(@current_user.id, venue.id)
end
def has_reviewed
!Review.where(:user_id=>user,:venue_id=>venue).blank?
end
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
另一个编辑
我已更改了已审查的?辅助方法:
def reviewed?
if current_user
Review.has_reviewed(@current_user.id, @venue.id)
else
nil
end
end
给出了未定义的方法 `has_reviewed'
但它为 # error schema
一个场地有很多评论
一个用户有很多评论
一条评论属于一个用户和一个场地,
路线如下所示:
App::Application.routes.draw do
resources :sessions
resources :users
resources :venues do
resources :reviews
end
end
Hi I'm a super beginner and working on my first app, I have a table of venues and users can add reviews to the venues. I would like to be able to hide the review form to users once they have submitted a review, to stop them from submitting more.
This is what I have now:
add review form on venue show page
<% if reviewed? %>
<%= form_for [@venue, @review], :class => 'rating_ballot' do |f| %>
<%= f.label("value_1", content_tag(:span, '1'), {:class=>"rating", :id=>"1"}) %>
<%= radio_button_tag("review[rating]", 1, :class => 'rating_button') %>
<%= f.label("value_2", content_tag(:span, '2'), {:class=>"rating", :id=>"2"}) %>
<%= radio_button_tag("review[rating]", 2, :class => 'rating_button') %>
<%= f.label("value_3", content_tag(:span, '3'), {:class=>"rating", :id=>"3"}) %>
<%= radio_button_tag("review[rating]", 3, :class => 'rating_button') %>
<%= f.label("value_4", content_tag(:span, '4'), {:class=>"rating", :id=>"4"}) %>
<%= radio_button_tag("review[rating]", 4, :class => 'rating_button') %>
<%= f.label("value_5", content_tag(:span, '5'), {:class=>"rating", :id=>"5"}) %>
<%= radio_button_tag("review[rating]", 5, :class => 'rating_button') %> <br>
<p>title: <br>
<%= f.text_field :title %></p><br>
<%= submit_tag %>
<% end %>
<% end %>
application controller
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
helper_method :reviewed?
protected
def reviewed?
true
end
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
I cant figure out what the reviewed? helper should be to allow me to do this, any help is greatly appreciated!
edit
I've added the has_reviewed helper to the application controller, it now shows this error:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Extracted source (around line #79):
76: <%= render :partial => 'reviews/review', :collection => @venue.reviews %>
77: </div>
78:
79: <% if reviewed? %>
application controller
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
helper_method :current_user
helper_method :has_reviewed
helper_method :reviewed?
protected
def reviewed?
Review.has_reviewed(@current_user.id, venue.id)
end
def has_reviewed
!Review.where(:user_id=>user,:venue_id=>venue).blank?
end
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
another edit
I've changed the reviewed? helper method to:
def reviewed?
if current_user
Review.has_reviewed(@current_user.id, @venue.id)
else
nil
end
end
but it gives undefined method `has_reviewed' for # error
schema
A venue has many reviews
A user has many reviews
A review belongs to a user and a venue
the routes looks like this:
App::Application.routes.draw do
resources :sessions
resources :users
resources :venues do
resources :reviews
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您按照 Wes 的建议进行更改,
reviewed?
方法将在数据库中查找该用户是否进行了评论。评论表需要有一列用于显示进行评论的用户及其评论的地点。
所以代码看起来像这样...
编辑以反映最近
在控制器中
添加的模式在用户模型中...
基本上我认为 has_reviewed 在用户模型中更好,因为用户 has_many 评论,然后它可以检查如果用户已经查看了给定的地点。
我假设模型评审有一个名为场地 ID 的外键,因为评审属于场地,这将是标准的事情。
Presuming you make the change as suggested by Wes, the
reviewed?
method would look in the database to see if this user has made a review.The reviews table will need to have a column for the user that made the review and the venue it reviewed.
So the code would look something like this...
EDITED to reflect schema recently added
In the controller
In the User model...
Basically I think the has_reviewed is better off in the User model as the user has_many reviews, and then it can check if the user has reviewed the given venue.
I am presuming that the Model Review has a foreign key to venue called venue_id, as a Review belongs_to a Venue and that would be the standard thing.