Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I only display an add review form if a user hasn't already submited a review?
    primarykey
    data
    text
    <p>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.</p> <p>This is what I have now:</p> <p><b>add review form on venue show page</b></p> <pre><code>&lt;% if reviewed? %&gt; &lt;%= form_for [@venue, @review], :class =&gt; 'rating_ballot' do |f| %&gt; &lt;%= f.label("value_1", content_tag(:span, '1'), {:class=&gt;"rating", :id=&gt;"1"}) %&gt; &lt;%= radio_button_tag("review[rating]", 1, :class =&gt; 'rating_button') %&gt; &lt;%= f.label("value_2", content_tag(:span, '2'), {:class=&gt;"rating", :id=&gt;"2"}) %&gt; &lt;%= radio_button_tag("review[rating]", 2, :class =&gt; 'rating_button') %&gt; &lt;%= f.label("value_3", content_tag(:span, '3'), {:class=&gt;"rating", :id=&gt;"3"}) %&gt; &lt;%= radio_button_tag("review[rating]", 3, :class =&gt; 'rating_button') %&gt; &lt;%= f.label("value_4", content_tag(:span, '4'), {:class=&gt;"rating", :id=&gt;"4"}) %&gt; &lt;%= radio_button_tag("review[rating]", 4, :class =&gt; 'rating_button') %&gt; &lt;%= f.label("value_5", content_tag(:span, '5'), {:class=&gt;"rating", :id=&gt;"5"}) %&gt; &lt;%= radio_button_tag("review[rating]", 5, :class =&gt; 'rating_button') %&gt; &lt;br&gt; &lt;p&gt;title: &lt;br&gt; &lt;%= f.text_field :title %&gt;&lt;/p&gt;&lt;br&gt; &lt;%= submit_tag %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p><b>application controller </b></p> <pre><code>class ApplicationController &lt; 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 </code></pre> <p>I cant figure out what the reviewed? helper should be to allow me to do this, any help is greatly appreciated!</p> <p><b>edit</b></p> <p>I've added the has_reviewed helper to the application controller, it now shows this error:</p> <pre><code>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: &lt;%= render :partial =&gt; 'reviews/review', :collection =&gt; @venue.reviews %&gt; 77: &lt;/div&gt; 78: 79: &lt;% if reviewed? %&gt; </code></pre> <p><b>application controller</b></p> <pre><code>class ApplicationController &lt; 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=&gt;user,:venue_id=&gt;venue).blank? end private def current_user @current_user ||= User.find(session[:user_id]) if session[:user_id] end end </code></pre> <p><b>another edit</b></p> <p>I've changed the reviewed? helper method to:</p> <pre><code> def reviewed? if current_user Review.has_reviewed(@current_user.id, @venue.id) else nil end end </code></pre> <p>but it gives undefined method `has_reviewed' for # error</p> <p><b>schema</b></p> <p>A venue has many reviews</p> <p>A user has many reviews</p> <p>A review belongs to a user and a venue</p> <p>the routes looks like this:</p> <pre><code>App::Application.routes.draw do resources :sessions resources :users resources :venues do resources :reviews end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload