Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.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 ActiveRecord::RecordNotFound位于ReviewController#创建/无法';找不到没有ID的评论_Ruby On Rails - Fatal编程技术网

Ruby on rails ActiveRecord::RecordNotFound位于ReviewController#创建/无法';找不到没有ID的评论

Ruby on rails ActiveRecord::RecordNotFound位于ReviewController#创建/无法';找不到没有ID的评论,ruby-on-rails,Ruby On Rails,我正在努力将评论发布到链接到用户的个人资料页面。我有一个有点复杂的路线,我知道,这不是最好的办法走这条路,但现在这些路线在我的每一个观点,所以改变他们将是一个恐怖对我来说,因为我是一个初学者,创造我现在已经花了我两个星期。这是路线 Rails.application.routes.draw do devise_for :user, controllers: { omniauth_callbacks: 'users/omniauth_callbacks', registration

我正在努力将评论发布到链接到用户的个人资料页面。我有一个有点复杂的路线,我知道,这不是最好的办法走这条路,但现在这些路线在我的每一个观点,所以改变他们将是一个恐怖对我来说,因为我是一个初学者,创造我现在已经花了我两个星期。这是路线

    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 %>
这是我的管理员:

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-ReviewsController
以防万一,这是我的个人资料控制器

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
class ProfilesController
我现在还没有找到答案,所以请帮忙

在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
的输出通常比发布路由文件以进行调试更有用。而且,当你开始学习的时候,一旦你学到了一些教训,就不要害怕重新开始

before_action :set_review, only: :new