我是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 %>
答案 0 :(得分:0)
在“索引”控制器操作中渲染日志/显示模板时,它不会通过“显示”操作,而只是渲染show 视图。因此,为了使日志/显示模板显示您的日志,您还需要在控制器中初始化您的@logs变量,如下所示:
def index
@reviews = Review.all.order('created_at DESC').paginate(:page => params[:page], :per_page => 2)
@logs = @logs.all
end
这样,当调用logs / show视图模板时,它有一个@logs数组可供使用。