Question

I am a newbie to rails, and I've been pulling out my hair trying to figure out how to render my "logs" on my "reviews" page (which is now the index page).

I keep on getting a nomethoderror even when I've defined logs in my review controller.

Reviews controller

ReviewsController < ApplicationController 
  def index
    @reviews = Review.all.order('created_at DESC').paginate(:page => params[:page], :per_page => 2)
  end

  def show
    @logs = @logs.all
  end


  def new
    @review = current_user.reviews.build
  end

  def edit
  end

  def create
    @review = current_user.reviews.build(review_params)
      if @review.save
        redirect_to @review, notice: 'review was successfully created.' 
      else
      render action: 'new' 
      end
  end

  def update
      if @review.update(review_params)
       redirect_to @review, notice: 'Review was successfully updated.' 
      else
       render action: 'edit' 
      end
  end

  def destroy
    @review.destroy
      redirect_to reviews_url 
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
  def set_review
      @review = Review.find(params[:id])
    end

  def correct_user
      @review = current_user.reviews.find_by(id: params[:id])
      redirect_to reviews_path, notice: "Not authorized to edit this review!" if @review.nil?
    end

    # Never trust parameters from the scary internet, only allow the white list through.
  def review_params
      params.require(:review,).permit(:description, :attachment)
    end

my reviews/index.html/erb

<div id='reviews' class='transitions-enabled'>
    <% @reviews.each do |review| %>

  <div class='box panel panel-default'>

  <div class='panel-body'>
      <p><%= review.description %></p>
      <p><strong><%= review.user.name if review.user %></strong></p>
      <%= link_to Time.now.strftime("%m/%d/%Y"), review %>
      <% if review.user == current_user %>

  <div class='actions'>
        <%= link_to edit_review_path(review) do %>
        <span class="glyphicon glyphicon-edit"></span> Edit
        <% end %>
        <%= link_to review, method: :delete, data: { confirm: 'Are you sure?' } do %>
        <span class="glyphicon glyphicon-trash"></span>Delete
        <% end %>
  </div>
        <% end %>
        </div>
      </div>
    <% end %>
  </div>

  <%= render template:"logs/show" %>

  <%= link_to new_review_path do %>
  <span class="glyphicon glyphicon-pencil"></span>New Review
  <% end %>
Was it helpful?

Solution

When you render the logs/show template in your "index" controller action, it doesn't go through the "show" action, it just renders the show view. So in order for the logs/show template to show your logs, you also need to initialize your @logs variable in the controller, like this:

def index
  @reviews = Review.all.order('created_at DESC').paginate(:page => params[:page], :per_page => 2)
  @logs = @logs.all
end

That way, when the logs/show view template is called, it has a @logs array to work from.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top