【问题标题】:ActionController::UrlGenerationError in Valuations#new估值中的 ActionController::UrlGenerationError#new
【发布时间】:2015-05-23 08:19:22
【问题描述】:

我读过其他与 UrlGenerationError 相关的 SO 文章,这些文章似乎指向单词的单数化或复数化,但我认为这不是问题所在。

当我从估值/_form.html.erb 中删除时它起作用:

<%= render "comments/comments" %>
<%= render "comments/form" %>

使用:name:tag_list 提交_form,已读

<%= render "comments/comments" %>
<%= render "comments/form" %>

然后刷新。零时有什么关系?

路线

  resources :valuations do
    resources :comments
  end

cmets_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

cmets/_form.html.erb

<%= form_for [@commentable, @comment] do |f| %>
  <% if @comment.errors.any? %>
    <div class="error_messages">
      <h2>Please correct the following errors.</h2>
      <ul>
      <% @comment.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

<div class="america">

  <div class="form-group">
    <%= f.text_area :content, rows: 4, class: 'form-control', placeholder: 'Enter Comment' %>
  </div>

  <div class="america2">
  <%= button_tag(type: 'submit', class: "btn") do %>
  <span class="glyphicon glyphicon-plus"></span> Comment
  <% end %>

</div>

<% end %>

非常感谢您抽出宝贵时间。

【问题讨论】:

  • 您的实际错误统计表明valuation_id 参数为零。您需要将其包含在内,以便它知道 cmets 的估值。
  • 你好@帕特里克!我在哪里/如何包含它?
  • 在此之前,您能解释一下这是怎么发生的吗?比如,用户的步骤是什么。他们是否正在尝试为估值添加评论?
  • 嗯,有一件事(不相关)是您在 show 中的 show 操作中的 valuations_controller.rb 是多余的,因为您调用 before_action 来设置实例变量。
  • 那是因为@commentable 为零。您已将其设置为 @valuation,它也必须为零。使用@commentable 实例变量的目的是什么?这是一个教程还是你正在关注的东西?

标签: ruby-on-rails ruby model-view-controller partials actioncontroller


【解决方案1】:

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

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

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

【讨论】:

  • 错误消失了,但是当我点击“添加评论”时,什么也没有出现。我需要将_comments_fields.html.erb_form_fields.html.erb 添加到cmets 文件夹或估价文件夹吗?为了安全起见,我两者都做了。我还将使用多态 cmets 为我的其他控制器视图执行此操作,所以如果字段进入每个相应的视图中不会有很多重复吗?非常感谢弗雷德里克的帮助!
  • @Anthony 您应该能够像分享其他部分一样分享 cmets_fields 部分。最好开一个新问题来讨论“添加评论”问题
  • 好主意弗雷德里克。这是第 2 轮。stackoverflow.com/questions/29197689/…。非常感谢您的帮助 =]
猜你喜欢
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
  • 2016-10-30
  • 2015-12-30
  • 2018-05-12
  • 1970-01-01
  • 2016-02-25
  • 1970-01-01
相关资源
最近更新 更多