gpt4 book ai didi

ruby-on-rails - ActionController::UrlGenerationError in Valuations#new

转载 作者:数据小太阳 更新时间:2023-10-29 08:45:49 28 4
gpt4 key购买 nike

enter image description here

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

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

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

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

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

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

路线

  resources :valuations do
resources :comments
end

评论 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

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

enter image description here

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

最佳答案

当你有这样的嵌套资源时,用于创建评论的 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 中内置的)

关于ruby-on-rails - ActionController::UrlGenerationError in Valuations#new,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29157795/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com