1

我是 Rails 的新手,我一直在努力弄清楚如何在我的“评论”页面(现在是索引页面)上呈现我的“日志”。

即使在我的审阅控制器中定义了日志,我仍然会收到 nomethoderror。

评论控制器

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

我的评论/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 %>
4

1 回答 1

0

当您在“索引”控制器操作中呈现日志/显示模板时,它不会通过“显示”操作,它只是呈现显示视图。因此,为了让 logs/show 模板显示您的日志,您还需要在控制器中初始化 @logs 变量,如下所示:

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

这样,当调用日志/显示视图模板时,它就有一个 @logs 数组可供使用。

于 2013-11-11T00:03:46.683 回答