抱歉,标题含糊。
我想在帖子/评论类型系统上启用评论(在我的情况下,遇到障碍)。
我有一个snags表,一个snag_comments表,并且有很多/属于snag和snag注释之间的关系。
在每个障碍的显示页面上,我都有一个表单呈现障碍评论。当我提交表单时,它会重定向到根URL,而不会将任何内容保存到snag_comments表中。
这是我得到的所有信息:
Started POST "/snag_comments" for ::1 at 2019-05-07 12:53:16 +0100 Processing by SnagCommentsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"yBefFfnI289UXqQYzF37DBRGQSk2Uqu41/OQO/mnET9aj2S3OqjLmSGtgyyZkTDz0aSdKzSPofQNeSzx/UH3ng==", "snag_comment"=>{"snag_id"=>"183", "content"=>"sdfgfsdgsdfgds"}, "commit"=>"Send"} Resident Load (0.3ms) SELECT "residents".* FROM "residents" WHERE "residents"."id" = $1 ORDER BY "residents"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]] Redirected to http://localhost:3000/ Filter chain halted as :redirect_residents rendered or redirected
所以我的参数正在经历。我不知道为什么它没有保存到数据库中。
在我的障碍控制器中,我有:
def show
@snag_attachments = @snag.snag_attachments.all
@snag_comment = SnagComment.new
@snag_comments = SnagComment.where(snag_id: @snag.id)
end
还有我的障碍评论控制器(在我弄清楚此损坏的部分时,该控制器目前尚不完整):
class SnagCommentsController < ApplicationController
skip_authorization_check
before_action :set_parent
def create
@snag_comment = SnagComment.new(snag_comment_params)
if @snag_comment.save
redirect_to snags_path
end
end
private
def set_parent
@parent = @snag
end
def snag_comment_params
params.require(:snag_comment).permit(:content, :image, :snag_id,
:commenter, :first_name, :last_name)
end
end
呈现的表单(views / snag_comments / _form):
<div class="snag-comment-form">
<%= simple_form_for snag_comment, url: snag_comments_path do |f| %>
<% if snag_comment.errors.any? %>
<div class="submission-errors">
<ul>
<% snag_comment.errors.full_messages.each do |error_msg| %>
<li>
<%= fa_icon "exclamation-circle" %>
<%= error_msg %>
</li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :snag_id, value: @snag.id %>
<%= f.input :content, label: false, as: :text, placeholder: t(".content"), maxlength: 1000, required: true, class: "snag-comment-content" %>
<div class="snag-comment-submit">
<%= f.submit t(".send"), class: "btn branded-btn send" %>
</div>
<% end %>
</div>
调用表单的位置(视图/房主/快照/显示):
<%= render 'snag_comments/form', snag_comment: @snag_comment %>
我需要将障碍注释添加到房主模块之外,因为管理模块也需要访问障碍注释(admin和房主可以对相同障碍进行注释)。
我不知道为什么表单要重定向而不保存。我尝试删除任何重定向,仅指定应保存该表单,但它仍重定向到根url而不保存,因此在传递参数与实际保存之间必须写一些东西。
答案 0 :(得分:0)
您似乎错过了如何设置嵌套资源的最基本基础-嵌套路线:
resources :snags do
resources :snag_comments
end
这将使“父子”关系成为RESTful路由的一部分,并非常清楚VS发生了什么,隐式地在表单主体中传递了该信息。
class SnagCommentsController < ApplicationController
# ...
before_action :set_snag
# POST /snags/:snag_id/snag_comments
def create
@snag_comment = @snag.comments.new(snag_comment_params)
if @snag_comment.save
redirect_to @snag, notice: 'Comment created'
else
render :new
end
end
private
def set_snag
@snag = Snag.find(params[:snag_id])
end
def snag_comment_params
params.require(:snag_comment)
.permit(:content, :image,:commenter, :first_name, :last_name)
end
end
要创建发布到嵌套路线的表单,请使用数组:
def show
@snag_attachments = @snag.snag_attachments
@snag_comment = @snag.comments.new
@snag_comments = @snag.comments
end
<%= simple_form_for([@snag, @snag_comment]) do |f| %>
# ...
<% end %>
确保删除父ID的输入。明确将其作为分段传递到URL中。