Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Pagesdashboard中的命名错误_Ruby On Rails_Ruby_Ruby On Rails 3_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails Pagesdashboard中的命名错误

Ruby on rails Pagesdashboard中的命名错误,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,我正在为我的应用程序创建一个仪表板,但是当我想显示评论时,我得到了NoMethodError,尽管我已经在控制器中定义了变量。我在查看仪表板页面时遇到错误。这是我的密码 pages\u controller.rb 查看\u controller.rb places_controller.rb /页面/dashboard.html.erb 以下是一个屏幕截图: 以前有人有过这个问题吗 您遇到的问题是该位置为零。 您可以像这样添加一个try方法 审查没有关联的位置。因此,您应该在尝试渲染它之前实现一

我正在为我的应用程序创建一个仪表板,但是当我想显示评论时,我得到了NoMethodError,尽管我已经在控制器中定义了变量。我在查看仪表板页面时遇到错误。这是我的密码

pages\u controller.rb

查看\u controller.rb

places_controller.rb

/页面/dashboard.html.erb

以下是一个屏幕截图:


以前有人有过这个问题吗

您遇到的问题是该位置为零。 您可以像这样添加一个try方法


审查没有关联的位置。因此,您应该在尝试渲染它之前实现一个检查

试试这个:

<td><%= review.try(:place).try(:name) %></td>

请显示完整的堆栈跟踪以及从何处获得错误。如果有位置,为什么要尝试?@MZaragoza我相信屏幕截图显示的错误表明没有位置?位置为nil,并且没有nil类的名称。因此,您只需要1个try review.place.try:name就可以了,因为即使是nil值,也总会有一个位置
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
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
<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>
<td><%= review.try(:place).try(:name) %></td>