在Valuations#new中的ActionController :: UrlGenerationError

在此处输入图像描述

我读过与UrlGenerationError有关的其他SO文章, UrlGenerationError文章似乎指向一个单词的单一化或多元化,但我认为这不是问题所在。

当我从valuations / _form.html.erb中删除时,它可以正常工作:

   

使用:name:tag_list提交:tag_list ,readd

   

然后刷新。 什么是零交易?

路线

  resources :valuations do resources :comments end 

comments_controller

 class CommentsController < ApplicationController before_action :load_commentable before_action :set_comment, only: [:show, :edit, :update, :destroy] before_action :logged_in_user, only: [:create, :destroy] def index @comments = @commentable.comments end def new @comment = @commentable.comments.new end def create @comment = @commentable.comments.new(comment_params) if @comment.save @comment.create_activity :create, owner: current_user redirect_to @commentable, notice: "comment created." else render :new end end def edit @comment = current_user.comments.find(params[:id]) end def update @comment = current_user.comments.find(params[:id]) if @comment.update_attributes(comment_params) redirect_to @commentable, notice: "Comment was updated." else render :edit end end def destroy @comment = current_user.comments.find(params[:id]) @comment.destroy @comment.create_activity :destroy, owner: current_user redirect_to @commentable, notice: "comment destroyed." end private def set_comment @comment = Comment.find(params[:id]) end def load_commentable resource, id = request.path.split('/')[1, 2] @commentable = resource.singularize.classify.constantize.find(id) end def comment_params params.require(:comment).permit(:content, :commentable) end end 

valuations_controller

 class ValuationsController < ApplicationController before_action :set_valuation, only: [:show, :edit, :update, :destroy] before_action :logged_in_user, only: [:create, :destroy] def index if params[:tag] @valuations = Valuation.tagged_with(params[:tag]) else @valuations = Valuation.order('RANDOM()') end end def show @valuation = Valuation.find(params[:id]) @commentable = @valuation @comments = @commentable.comments @comment = Comment.new end def new @valuation = current_user.valuations.build @commentable = @valuation @comments = @commentable.comments @comment = Comment.new end def edit end def create @valuation = current_user.valuations.build(valuation_params) if @valuation.save redirect_to @valuation, notice: 'Value was successfully created' else @feed_items = [] render 'pages/home' end end def update if @valuation.update(valuation_params) redirect_to @valuation, notice: 'Value was successfully updated' else render action: 'edit' end end def destroy @valuation.destroy redirect_to valuations_url end private def set_valuation @valuation = Valuation.find(params[:id]) end def correct_user @valuation = current_user.valuations.find_by(id: params[:id]) redirect_to valuations_path, notice: "Not authorized to edit this valuation" if @valuation.nil? end def valuation_params params.require(:valuation).permit(:name, :private_submit, :tag_list, :content, :commentable, :comment) end end 

评论/ _form.html.erb

   

Please correct the following errors.

Comment

在此处输入图像描述

非常感谢您的参与。

当你有这样的嵌套资源时,用于创建注释的url看起来像/valuations/123/comments其中123是评估的id – 如果没有评估id,则无法生成此url。

在您的Valuations#new页面上,估值(即@commentable )是未保存的对象,因此它还没有id,因此错误的@commentable错误。 另外一个表单嵌套在另一个表单中是无效的html并且会导致奇怪的行为。 另一方面,在您的展示页面上,估值确实有一个ID,所以事情应该按原样运作。

如果您想一次创建一个评估及其初始评论,那么您应该使用accepts_nested_attributes_forfields_for将评论的字段添加到您的评估表单中(还有其他方法,但accepts_nested_attributes_for是内置于rails中的内容)