无法向某个地点提交评论 - Rails 目录网站应用

Can't submit a Review into a Place - Rails Directory Website app

我正在学习 Rails 教程,其中我必须创建一个目录网站,用户可以在其中提交评论。

现在,事情是这样的:

我可以注册用户并且可以登录。登录后,我还可以在数据库中创建一个新位置。我也可以访问我创建的每个地方的网页。

不过我有一个问题。在每个地点页面上,我无法提交对该地点的评论。

所有地点都有一个地址、一个 phone 号码、一个网站、一个描述和一张带有该地点位置的地图。在同一页面上,我有一个提交评论的表单,但是,一旦我点击创建评论按钮,什么都没有发生。我在页面上看不到评论,当我转到 rails 控制台时,我可以肯定地看到没有创建任何评论。

在我的app/views/reviews/_form.html.erb里面我有这个代码:

  <div class="form-inputs">
    <%= f.input :content, required: true %>
    <%= f.hidden_field :place_id, required: true, value: @place_id %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

在我的app/views/reviews/_review.html.erb里面我有这个代码:

<p>
    <%= gravatar_tag review.user.email, size: 20 %><%= review.content %> by <%= review.user.name %>
</p>

在我的app/views/places/show.html.erb里面我有这个代码:

<div class="row">
  <div class="col-md-3">
    <h3><%= @place.name %></h3>
    <p><strong>Adress</strong><%= @place.address %></p>
    <p><strong>Phone</strong><%= @place.phone %></p>
    <p><strong>Website</strong><%= @place.website %></p>
    <p><strong>Description</strong><%= @place.description %></p>
    <div id="map-canvas" style = "width:230px; height:230px"></div>
  </div>

  <div class="col-md-9">
    <h3>Reviews by People</h3>
    <% if current_user %>
    <h5>New Review</h5>
    <%= render 'reviews/form' %>
    <h5>All Reviews</h5>
    <%= render @reviews %>
    <% end %>
  </div>

</div>

在我的 places_controller.rb 我有:

class PlacesController < ApplicationController
  before_action :authenticate_user!, only: [:new, :edit, :create, :update, :destroy]
  before_action :set_place, only: [:show, :edit, :update, :destroy]

  # GET /places
  # GET /places.json
  def index
    @places = Place.all
  end

  # GET /places/1
  # GET /places/1.json
  def show
    @reviews = @place.reviews
    @review = Review.new
  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 = current_user.places.new(place_params)

    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

此外,到目前为止...这是我的 reviews.controller.rb 代码:

class ReviewsController <ApplicationController

  before_action :authenticate_user!
  before_action :set_review, only: [:edit, :update, :destroy]

  def edit
  end

  def create
    @review = current_user.reviews.new(review_params)

    respond_to do |format|
      if @review.save
        format.html { redirect_to place_path(@review.place) , notice: 'Review was successfully created.' }
      else
        format.html { render :new }
      end
    end
  end

  def update
    respond_to do |format|
      if @review.update(review_params)
        format.html { redirect_to place_path(@review.place), notice: 'Review was successfully updated.' }
      else
        format.html { render :edit }
      end
    end
  end


  def destroy
    @review.destroy
    respond_to do |format|
      format.html { redirect_to place_path(@review.place), notice: 'Review was successfully destroyed.' }
    end
  end

  private 

  def set_review
    @review = Review.find(params[:id])  
  end

  def review_params
    params.require(:review).permit(:content, :place_id)
  end


end

为什么我无法保存某个地方的评论?

你的表格应该是这样的:-

<div class="form-inputs">
    <%= f.input :content, required: true %>
    <%= f.hidden_field :place_id, required: true, value: @place.id %>
</div>

您正在控制器中设置实例变量@place。所以你需要在渲染视图中使用@place.id。你没有在任何地方设置@place_id