在ReviewsController#create中的ActiveRecord :: RecordNotFound /在没有ID的情况下找不到评论

时间:2019-11-23 20:48:50

标签: ruby-on-rails

我正在努力将评论发布到链接到用户的个人资料页面上。我知道我的路线有点复杂,这不是最好的主意,但就目前而言,我认为这些路线都是我的观点,因此更改路线对我来说是一个恐怖,因为我是初学者并创造我现在已经花了两个星期。这是路线。

    Rails.application.routes.draw do
    devise_for :user, controllers: { omniauth_callbacks: 'users/omniauth_callbacks', registrations: "users/registrations" }
    resources :users do
        resources :profiles do
            resources :reviews, only: [:new, :create]
        end
    end
    root 'home#index'
end

好吧,对于评论,我有一个表格,该表格反映了这种复杂的路线并可以在页面上显示:

<div class="submit-review">
  <%= form_for([@user, @profile, @review], :url => user_profile_reviews_path(@user, @profile)) do |f| %>
    <label for="review">How was your experience?</label><br>
    <%= f.label :rating %>
    <%= f.select :rating, options_for_select([["Please select one", ""], 5, 4, 3, 2, 1]) %>

    <%= f.text_area :content, placeholder:"Please enter your feedback here" %>
    <%= f.submit "Submit your review", class: "btn btn-default" %> <br><br>
  <% end %>

但是,单击“提交”按钮会出现错误:

ActiveRecord::RecordNotFound in ReviewsController#create
Couldn't find Review without an ID
Extracted source (around line #35):

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

end

这是我的评论控制者:

class ReviewsController < ApplicationController
  before_action :set_profile
  before_action :set_review, only: [:new, :create]

  def new
    @review = Review.new
  end

def create
    @profile = Profile.find(params[:profile_id])
    @review = @profile.reviews.build(review_params)
    @review.user_id = current_user.id

    if @review.save
        redirect_to @profile
    else
        redirect_to @profile, notice: "Error saving"
    end
end

  private

  def review_params
    params.permit(:content, :rating)
  end

def set_profile
    @profile = Profile.find(params[:profile_id])
  end

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

end

以防万一,这是我的个人档案控制器

class ProfilesController < ApplicationController
      before_action :set_profile, only: [:show, :edit, :update, :destroy]

      def index
        @profiles = Profile.all
      end

      def show
        @user = User.find(params[:user_id])
        @profile = Profile.find(params[:id])
        @reviews = Review.where("profile_id = ?", params[:id])
      end

      def new
        @user = User.find(params[:user_id])
      end

      def edit
        @profile = Profile.find(params[:id])
      end

      def create
        @profile = current_user.build_profile(profile_params)
        respond_to do |format|
          if @profile.save
            format.html { redirect_to user_profile_path(current_user.id, current_user.profile.id), notice: 'Profile was successfully created.' }
            format.json { render :show, status: :created, location: @profile }
          else
            format.html { render :new }
            format.json { render json: @profile.errors, status: :unprocessable_entity }
          end
        end
      end

      def update
        respond_to do |format|
          if @profile.update(profile_params)
            format.html { redirect_to user_profile_path(current_user.id, current_user.profile.id), notice: 'Profile was successfully updated.' }
            format.json { render :show, status: :ok, location: @profile }
          else
            format.html { render :edit }
            format.json { render json: @profile.errors, status: :unprocessable_entity }
          end
        end
      end

      def destroy
        @profile.destroy
        respond_to do |format|
          format.html { redirect_to profiles_url, notice: 'Profile was successfully destroyed.' }
          format.json { head :no_content }
        end
      end

      private
        def set_profile
          @profile = Profile.find(params[:id])
        end

        def profile_params
          params.permit(:about, :rating, :avatar)
        end
end

到目前为止,我还没有找到答案,所以请帮忙!

1 个答案:

答案 0 :(得分:1)

在set_review中,您要做

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

这是通过查看当前页面的URL来获取有关记录的ID(在本例中为Review)。由于这是一个创建操作,因此在数据库或URL中不存在该记录的ID(尚未创建)。当您对set_review的调用查找ID的URL时,将引发此错误。

如果您更改

before_action :set_review, only: [:new, :create]

成为

before_action :set_review, only: :new

它应该使您克服此错误。另外,为了将来参考,运行rake routes的输出通常比发布路由文件进行调试更有用。同样,当您开始学习时,请不要害怕从头开始。