0

In my application, users submit reviews under brands. Here's the association:

class Brand < ActiveRecord::Base
has_many :reviews

class Review < ActiveRecord::Base
belongs_to :brand

Right now I have a partial defined to show reviews as such:

_showreview.html.erb

<% review.each do |review| %>
<div class="col-sm-4">
<div class="thumbnail">
<%= image_tag review.first_photo %>
<div class="caption">
<h4><%= link_to review.title, review %></h4>
<h6><strong><%= link_to ("WRITTEN BY " + review.user.username.upcase), review.user %></strong></h6>
<%= review.description.truncate_words(60, omission: '...') %>
<br>    
<br>
<%= link_to review.brand.label, review.brand, :class => 'btn btn-sm btn-lake' %>
</div>
</div>
</div>

And I render it like this by passing an instance variable in:

<%= render "showreview", review: @top_reviews %>

It works as expected.

Now I wish to reuse the partial for brands/show.html.erb

What I want to do now is retrieve all the reviews that belong to a brand and display it.

show_controller.rb looks like this

 def show
  @brand = Brand.find(params[:id])
 @reviews =Review.find(params[:id])
 end

I tried to use this but it doesn't work. What is the correct way to do this?

<%= render "/reviews/showreview", review: '@brand.reviews' %>
4
  • You can specify the partial view lie <%= render "brands/showreview", review: your reviews variable %> Commented Mar 15, 2017 at 9:02
  • Do you want to show single review ? Commented Mar 15, 2017 at 9:03
  • No I want to show all the reviews that belong to a brand. Thanks. Commented Mar 15, 2017 at 9:06
  • I added answer. please accept it. if it is working Commented Mar 15, 2017 at 9:10

3 Answers 3

2
<%= render "/reviews/showreview", review: @brand.reviews %>

But please, rename review to reviews. It's more convenient name of this variable.

Sign up to request clarification or add additional context in comments.

2 Comments

He don't want to do this
Try like this <%= render partial: "/reviews/showreview", locals: { review: @brand.reviews } %>
0

Your method will be like this

def show
  @brand = Brand.find(params[:id])
end

and in your view

 <%= render "reviews/showreview", review: @brand.reviews %>

Comments

0

firstly, you should use a plurality for the variable review instead.

reviews.each do |review|

it makes your code readability. then try placing review: '@branch.review' by reviews: @branch.reviews and make sure that you did pass the variable @branch from your controller

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.