如果用户尚未提交评论,如何才能仅显示添加评论表单?

发布于 2024-11-01 05:59:15 字数 3342 浏览 0 评论 0原文

您好,我是一个超级初学者,正在开发我的第一个应用程序,我有一个场地表,用户可以对场地添加评论。我希望能够在用户提交评论后向他们隐藏评论表单,以阻止他们提交更多评论。

这就是我现在所拥有的:

在场地展示页面上添加审核表格

<% 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱给你人给你 2024-11-08 05:59:15

假设您按照 Wes 的建议进行更改,reviewed? 方法将在数据库中查找该用户是否进行了评论。

评论表需要有一列用于显示进行评论的用户及其评论的地点。

所以代码看起来像这样...

编辑以反映最近

在控制器中

def reviewed?
  if current_user
    @current_user.has_reviewed(@venue.id)
  else
    nil
  end
end

添加的模式在用户模型中...

class User < ...
  has_many :reviews

  def has_reviewed(venueid)
    reviews.exists?(:venue_id => venueid)
  end

  ...
end

基本上我认为 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

def reviewed?
  if current_user
    @current_user.has_reviewed(@venue.id)
  else
    nil
  end
end

In the User model...

class User < ...
  has_many :reviews

  def has_reviewed(venueid)
    reviews.exists?(:venue_id => venueid)
  end

  ...
end

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.

百变从容 2024-11-08 05:59:15
<% unless reviewed? %>
[...]
<% end %>
<% unless reviewed? %>
[...]
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文