我正在为我的应用创建一个仪表板,但是当我想显示评论时,我得到了NoMethodError,尽管我已经在控制器中定义了变量。我在查看仪表板页面时收到错误。这是我的代码
pages_controller.rb
class PagesController < ApplicationController
before_action :authenticate_user!, only: [:dashboard]
def about
end
def help
end
def contact
end
def dashboard
@user = current_user
@places = @user.places
@reviews = @user.reviews
end
end
reviews_controller.rb
class ReviewsController < ApplicationController
before_action :set_review, only: [:edit, :update, :destroy]
before_action :set_place
before_action :authenticate_user!
# GET /reviews/new
def new
@review = Review.new
end
# GET /reviews/1/edit
def edit
end
# POST /reviews
# POST /reviews.json
def create
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.place_id = @place.id
@review.save
redirect_to place_path(@place)
end
# PATCH/PUT /reviews/1
# PATCH/PUT /reviews/1.json
def update
respond_to do |format|
if @review.update(review_params)
format.html { redirect_to @review, notice: 'Review was successfully updated.' }
format.json { render :show, status: :ok, location: @review }
else
format.html { render :edit }
format.json { render json: @review.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reviews/1
# DELETE /reviews/1.json
def destroy
@review.destroy
respond_to do |format|
format.html { redirect_to reviews_url, notice: 'Review was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_review
@review = Review.find(params[:id])
end
def set_place
unless @place = Place.where(id: params[:place_id]).first
redirect_to places_path, flash: {alert: "Place doesn't exists"}
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def review_params
params.require(:review).permit(:comment,:rating)
end
end
places_controller.rb
class PlacesController < ApplicationController
before_action :set_place, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user! , except: [:index,:show]
# GET /places
# GET /places.json
def index
@places = Place.all
end
# GET /places/1
# GET /places/1.json
def show
@reviews = Review.where(place_id: @place.id)
end
# GET /places/new
def new
@place = Place.new
end
# GET /places/1/edit
def edit
end
# POST /places
# POST /places.json
def create
@place = Place.new(place_params)
@place.user_id = current_user.id
respond_to do |format|
if @place.save
format.html { redirect_to @place, notice: 'Place was successfully created.' }
format.json { render :show, status: :created, location: @place }
else
format.html { render :new }
format.json { render json: @place.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /places/1
# PATCH/PUT /places/1.json
def update
respond_to do |format|
if @place.update(place_params)
format.html { redirect_to @place, notice: 'Place was successfully updated.' }
format.json { render :show, status: :ok, location: @place }
else
format.html { render :edit }
format.json { render json: @place.errors, status: :unprocessable_entity }
end
end
end
# DELETE /places/1
# DELETE /places/1.json
def destroy
@place.destroy
respond_to do |format|
format.html { redirect_to places_url, notice: 'Place was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_place
@place = Place.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def place_params
params.require(:place).permit(:name, :address, :description, :phone, :website)
end
end
/pages/dashboard.html.erb
<div class="container">
<div class="row">
<div class="col-md-2">
</div>
<div class="col-md-5">
<h3>My Places</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Created</th>
<th></th>
</tr>
</thead>
<tbody>
<% @places.each do |place| %>
<tr>
<td><%= place.name %></td>
<td><%= time_ago_in_words(place.created_at) %> ago</td>
<td><%= link_to "Edit", edit_place_path(place) %>|<%= link_to "Destroy", place_path(place), method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to "New Place", new_place_path %>
</div>
<h3>My Reviews</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Place</th>
<th>Created</th>
<th></th>
</tr>
</thead>
<tbody>
<% @reviews.each do |review| %>
<tr>
<td><%= review.place.name %></td>
<td><%= time_ago_in_words(review.created_at) %> ago</td>
<td><%= link_to "Edit", edit_review_path(review) %>|<%= link_to "Destroy", review_path(review), method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
这是一个截图:
以前有人有这个问题吗?
答案 0 :(得分:0)
你遇到的问题是这个地方是零。 你可以添加一个像这样的尝试方法
<td><%= review.place.try(:name) %></td>
答案 1 :(得分:0)
该评论没有相关的地方。因此,您应该在尝试渲染之前执行检查。
试试这个:
<td><%= review.try(:place).try(:name) %></td>