ActionController::估值中的UrlGenerationError#new

2024-02-12

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

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

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

提交 _form:name & :tag_list, readd

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

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

routes

  resources :valuations do
    resources :comments
  end

评论控制器

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

估值控制器

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

<%= 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 %>

非常感谢您的参与。


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

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

如果您想一次性创建评估及其初始评论,那么您应该使用accepts_nested_attributes_for http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for and fields_for http://guides.rubyonrails.org/form_helpers.html#nested-forms将评论字段添加到您的评估表单中(还有其他方法,但是accepts_nested_attributes_for是内置于 Rails 中的)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ActionController::估值中的UrlGenerationError#new 的相关文章

随机推荐

  • 如何将 Firestore 查询转换为 Javascript 数组

    我正在尝试导出一个 firestore 函数 该函数执行查询并返回包含该查询中的对象的数组 我正在尝试从文档的子集合中获取数据 并获取返回的文档对象数组以呈现给客户端 我已经尝试了以下方法 但它不起作用 例如 对象返回空白 我认为这与承诺处
  • Scala 中函数组合的简洁语法?

    我正在学习 Scala 并遇到了以下任务 如果字符串为空则返回 null 否则将其大写 Apache Commons 中有两个函数组合在一起可以解决这个问题 在 Haskell 中我会这样写 upperCaseOrNull StringUt
  • Android 新手相机方向

    我正在使用相机工作面临相机方向问题 我在互联网上找到了一些答案 我认为这个答案可能最适合我 谁能给我详细的指导吗如何添加这个答案 https stackoverflow com questions 3841122 android camer
  • PG gem 不会安装在 Rails 应用程序中:Gem::Ext::BuildError: ERROR: 无法构建 gem 本机扩展

    我正在尝试将 Rails 应用程序部署到 Heroku 当我添加pggem 到我的 gemfile 并运行bundle install我收到错误 An error occurred while installing pg 1 1 3 and
  • android 可以像整数数组一样存储可绘制的 id 吗?

    我想要一个drawableid 整数值数组 我可以像这样存储integer array in res values XXX xml通过使用integer array标签 下面是声明的整数数组strings xml
  • Jupyter 笔记本内嵌图像中的光标位置和像素值

    我使用 Python 2 7 x 和 Jupyter Notebook matplotlib 和 pylab 后端以及内联标志 pylab inline 在活动单元格下方打印图像 我希望能够将光标移动到图像上并知道它的位置和像素值示例可以是
  • CSS 100% 宽度但避免滚动条

    这可能是一个已经解决了几十次的问题的变体 但 CSS 真的让我觉得自己像个傻瓜 我正在尝试构建一个可以通过多种方式定位和调整大小的小部件 这是一个非常简单的布局 固定高度的页眉 固定高度的页脚以及占用剩余空间的正文 整体宽度和高度各不相同
  • Crontab 无法在 Windows 上的 Ubuntu 上使用 Bash

    我正在尝试安排一个 bash 脚本在 Windows 10 中的 Windows 上的 Ubuntu 上使用 Bash 运行 每次我编写 cron 时 我都会在终端中收到以下错误消息 crontab installing new cront
  • wicket:child 标签可以嵌套在页面上的另一个组件下吗?

    在 Wicket 1 4 中 我试图允许子页面更改父页面中标签上的 CSS 类 我一直这样做 这种情况的奇怪之处在于我想要定位的标签包装子页面标记 这是我尝试过的简化片段 父页面 html div div
  • 简单登录unity c#脚本

    应该很简单 但仍然找不到直接答案 如何使用 C Unity 脚本记录简单的消息 我试过这个 Debug Log Hello Log 它不起作用 或者我没有找对地方 Debug Log Hello Log 可以在控制台选项卡 为了让您能够看到
  • 如何防止 Silverlight RIA 实体在我准备好之前附加到数据上下文

    我有一个用于简单 TODO 列表的 Silverlight 4 应用程序 我遇到的问题是数据绑定正在连接我的关系TODO对象 这会导致 RIA 数据上下文将其添加到DataContext TODOs在我想要它之前列出它 我想将该对象视为新的
  • 指针数组作为函数参数

    我有一个关于 C C 中数组和指针的基本问题 假设我有 Foo fooPtrArray 4 如何通过fooPtrArray变成一个函数 我努力了 int getResult Foo fooPtrArray failed int getRes
  • PostgreSQL 比较两个 jsonb 对象

    With PostgreSQL v9 5 http www postgresql org the JSONB https stackoverflow com questions 22654170 explanation of jsonb i
  • 刷新表 - 访问被拒绝

    我需要备份数据库 但是在备份之前尝试刷新表时出现此错误 这是什么意思RELOAD特权 在 phpmyadmin 中找不到任何 RELOAD 权限 Error Access denied you need the RELOAD privile
  • 禁用/隐藏“下载 React DevTools...”

    如何完全禁用或隐藏 持久 控制台消息 Download the React DevTools for a better development experience在开发过程中 从 React 16 2 0 开始 您可以在 webpack
  • C++ 类似 C# 中的 Vector 类

    C 的类似类是什么std vector in C 我想要一个类 它在内部保存一个内部数组并支持在后面插入O 1 time 这是一个包含一些内容的列表C C 容器是roughly彼此等价 不是完全替代 std vector http en c
  • 如何在 R 编程中从数据框创建表格图像

    如何将数据框中的数据转换为表格的图形 我的数据在dataframe并想将其转换为图形 以便我可以使用 Power BI 软件显示它 e g x lt data frame Sex c M M F F F Color c brown blue
  • ImportError:无法使用 Brew 导入名称 HTTPSHandler [重复]

    这个问题在这里已经有答案了 我在设置 python 环境时遇到了一些麻烦 我安装了 pythonbrew但是当我尝试使用 easy install 或 pip 安装某些东西时 我收到此错误 在本例中我尝试使用 pip 安装软件包 我正在使用
  • 今天扩展在 iOS 8.1.2 上启动之前崩溃了

    我一直在制作一个今日扩展 可以从提要中下载文章并显示最新的文章 整个事情在 iOS 8 上运行良好 在 iOS 8 1 上仍然运行 然后是 iOS 8 1 2 我们开始抱怨今天的扩展不再运行 我尝试在 iOS 8 1 2 设备上进行调试 在
  • ActionController::估值中的UrlGenerationError#new

    我读过其他相关文章UrlGenerationError似乎指向一个单词的单数或复数 但我认为这不是问题所在 当我从valuations form html erb中删除时它起作用 提交 form name tag list readd 然后