ActionController::UrlGenerationError in Valuations#new [英] ActionController::UrlGenerationError in Valuations#new

查看:61
本文介绍了ActionController::UrlGenerationError in Valuations#new的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

当我从估价/_form.html.erb 中删除时它有效:

<%= render "comments/comments" %><%= 渲染评论/表单"%>

提交带有 :name 的 _form &:tag_list,已读

<%= render "comments/comments" %><%= 渲染评论/表单"%>

然后刷新.零时的交易是什么?

路线

 资源:估价做资源:评论结束

comments_controller

class CommentsController <;应用控制器before_action :load_commentablebefore_action :set_comment, only: [:show, :edit, :update, :destroy]before_action :logged_in_user, only: [:create, :destroy]定义索引@comments = @commentable.comments结尾定义新@comment = @commentable.comments.new结尾定义创建@comment = @commentable.comments.new(comment_params)如果@comment.save@comment.create_activity :创建,所有者:current_userredirect_to @commentable,注意:评论已创建."别的渲染:新结尾结尾定义编辑@comment = current_user.comments.find(params[:id])结尾定义更新@comment = current_user.comments.find(params[:id])如果@comment.update_attributes(comment_params)redirect_to @commentable,注意:评论已更新."别的渲染:编辑结尾结尾销毁@comment = current_user.comments.find(params[:id])@comment.destroy@comment.create_activity :销毁,所有者:current_userredirect_to @commentable,注意:评论已销毁."结尾私人的def set_comment@comment = Comment.find(params[:id])结尾def load_commentable资源,id = request.path.split('/')[1, 2]@commentable = resource.singularize.classify.constantize.find(id)结尾定义注释参数params.require(:comment).permit(:content, :commentable)结尾结束

valuations_controller

class ValuationsController <;应用控制器before_action :set_valuation, only: [:show, :edit, :update, :destroy]before_action :logged_in_user, only: [:create, :destroy]定义索引如果参数[:tag]@valuations = Valuation.tagged_with(params[:tag])别的@valuations = Valuation.order('RANDOM()')结尾结尾高清秀@valuation = Valuation.find(params[:id])@commentable = @valuation@comments = @commentable.comments@comment = Comment.new结尾定义新@valuation = current_user.valuations.build@commentable = @valuation@comments = @commentable.comments@comment = Comment.new结尾定义编辑结尾定义创建@valuation = current_user.valuations.build(valuation_params)如果@valuation.saveredirect_to @valuation,注意:'值已成功创建'别的@feed_items = []呈现页面/主页"结尾结尾定义更新如果@valuation.update(valuation_params)redirect_to @valuation,注意:'值已成功更新'别的渲染动作:'编辑'结尾结尾销毁@valuation.destroy重定向_到评估_url结尾私人的def set_valuation@valuation = Valuation.find(params[:id])结尾定义正确_用户@valuation = current_user.valuations.find_by(id: params[:id])重定向到评估路径,注意:如果@valuation.nil,无权编辑此评估"?结尾定义估值参数params.require(:valuation).permit(:name, :private_submit, :tag_list, :content, :commentable, :comment)结尾结束

comments/_form.html.erb

<%= form_for [@commentable, @comment] do |f|%><% if @comment.errors.any?%><div class="error_messages"><h2>请更正以下错误.</h2><ul><% @comment.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>

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

<div class="america2"><%= button_tag(type: 'submit', class: "btn") do %><span class="glyphicon glyphicon-plus"></span>评论<%结束%>

<%结束%>

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

解决方案

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

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

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

I've read other SO articles relating to UrlGenerationError's which seem to point to singularization or plurization of a word, but I don't think that's the issue here.

It works when I remove from valuations/_form.html.erb:

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

Submit the _form with :name & :tag_list, readd

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

and then refresh. What's the deal when nil?

routes

  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

comments/_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 %>

Thank you so much for your time.

解决方案

When you have a nested resource like that, the url for creating a comment looks like /valuations/123/comments where 123 is the id of the valuation - without a valuation id this url cannot be generated.

On your Valuations#new page, the valuation (i.e. @commentable) is an unsaved object, so it has no id yet, hence the error about a missing valuation_id. In addition having one form nested within another is invalid html and will lead to odd behaviour. On your show page on the other hand, the valuation does have an id and so things should work as they are.

If you want to create a valuation and its initial comment(s) in one go then you should use accepts_nested_attributes_for and fields_for to add the fields for the comments to your valuation form (There are other ways, but accepts_nested_attributes_for is what is built into rails)

这篇关于ActionController::UrlGenerationError in Valuations#new的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆