form_with没有击中控制器的动作
form_with does not hit the controller's action
我尝试了 form_tag
和 form_with
- 结果是一样的,控制器的动作永远不会被触发。
# routes.rb
resources :products do
member do
patch :add_comment
end
end
# products_controller.rb
def add_comment
# !!! damn form_with never gets here!!!
product.add_comment!(params[:comment_id])
redirect_back(fallback_location: products_path)
end
# view
<%= form_with(url: add_comment_product_path, local: true) do |form| %>
<%= form.text_field :comment_id %>
<%= form.submit 'Add comment' %>
<% end %>
实际日志:
Started PATCH "/products/1"
Processing by ProductsController#update as HTML
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"token",
"products"=>{a_lot: :of_stuff},
"comment_id"=>"2",
"commit"=>"Add comment",
"id"=>"1"
}
预期日志:
Started PATCH "/products/1/add_comment?comment_id=2"
Processing by ProductsController#add_comment as HTML
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"token",
"comment_id"=>"2",
"id"=>"1"
}
编辑:
我认为这与这个 form_with
嵌套在更大的表单中有关,当我点击 Add comment
时它看起来会触发外部提交
你试试这个 -
# products_controller.rb
def add_comment
# You need add permitted for get parameters
params.permit(:comment_id)
product.add_comment!(params[:comment_id])
redirect_back(fallback_location: products_path)
end
# You can place this form anywhere in your application, but you need to specify product object and comment_id
<%- @product = Product.find(1) %>
<%= form_with(url: add_comment_product_path(@product, comment_id: 2), local: true, method: :patch) do |form| %>
<%= form.text_field :comment_id %>
<%= form.submit 'Add comment' %>
<% end %>
Started PATCH "/products/1/add_comment?comment_id=2" for 127.0.0.1 at 2018-10-05 22:01:37 +0600
Processing by ProductsController#add_comment as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"token", "comment_id"=>"2", "commit"=>"Add comment", "id"=>"1"}
add_comment_product PATCH /products/:id/add_comment(.:format) products#add_comment
您已将其声明为 成员 路由,但我没有看到您为 :id
路径助手传递任何值。
尝试将其更改为
<%= form_with(url: add_comment_product_path(product), local: true) do |form| %>
其中 product
是 Product
实例。
Rails 处理此问题的方法是将其作为单独但嵌套的资源 - 因为您实际上是在创建新资源(评论)而不是修改产品本身。
这也使您的代码符合单一职责原则 (SRP),因为每个控制器仅处理单一类型资源的 CRUD。
您可以 nest resources 通过嵌套调用 resources
:
resources :products do
resources :comments, shallow: true
end
然后设置 CommentsController 来处理 CRUD 评论:
class CommentsController < ApplicationController
before_action :set_comment, only: [:index, :new, :create]
# GET /products/:product_id/comments
def index
@comments = @product.comments
end
# GET /products/:product_id/comments/new
def new
@comment = @product.comments.new
end
# POST /products/:product_id/comments
def create
@comment = @product.comments.new(comment_params)
if @comment.save
redirect_to @product, success: 'Comment created'
else
render :new
end
end
# ...
private
def set_product
@product = Product.find(params[:product_id])
end
def comment_params
params.require(:comment)
.permit(:foo, :bar)
end
end
要将表单操作属性设置为指向嵌套路由,您只需使用数组或命名的 product_comments(product_id: @product.to_param)
路由助手。
<%= form_with(model: @comment, url: [@comment.product, @comment], local: true) do |form| %>
<%= form.submit 'Add comment' %>
<% end %>
由于产品 ID 通过 URI 传递,因此无需通过隐藏输入传递它。
I think it has something to do with the fact that this form_with is
nested into bigger form and it looks when I hit Add comment it
triggers the outer submit
您应该注意 HTML 标准(HTML5 和更早的 (x)HTML 标准)不允许嵌套表单元素,并且行为可能非常不可预测,因为未指定浏览器是否应使用嵌套表单的 action 属性或将事件冒泡到父表单元素,这在您的情况下很可能发生。参见:http://w3.org/TR/html5/forms.html
我只需要将嵌套表单从更大的表单中移出即可使其正常工作。
我尝试了 form_tag
和 form_with
- 结果是一样的,控制器的动作永远不会被触发。
# routes.rb
resources :products do
member do
patch :add_comment
end
end
# products_controller.rb
def add_comment
# !!! damn form_with never gets here!!!
product.add_comment!(params[:comment_id])
redirect_back(fallback_location: products_path)
end
# view
<%= form_with(url: add_comment_product_path, local: true) do |form| %>
<%= form.text_field :comment_id %>
<%= form.submit 'Add comment' %>
<% end %>
实际日志:
Started PATCH "/products/1"
Processing by ProductsController#update as HTML
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"token",
"products"=>{a_lot: :of_stuff},
"comment_id"=>"2",
"commit"=>"Add comment",
"id"=>"1"
}
预期日志:
Started PATCH "/products/1/add_comment?comment_id=2"
Processing by ProductsController#add_comment as HTML
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"token",
"comment_id"=>"2",
"id"=>"1"
}
编辑:
我认为这与这个 form_with
嵌套在更大的表单中有关,当我点击 Add comment
时它看起来会触发外部提交
你试试这个 -
# products_controller.rb
def add_comment
# You need add permitted for get parameters
params.permit(:comment_id)
product.add_comment!(params[:comment_id])
redirect_back(fallback_location: products_path)
end
# You can place this form anywhere in your application, but you need to specify product object and comment_id
<%- @product = Product.find(1) %>
<%= form_with(url: add_comment_product_path(@product, comment_id: 2), local: true, method: :patch) do |form| %>
<%= form.text_field :comment_id %>
<%= form.submit 'Add comment' %>
<% end %>
Started PATCH "/products/1/add_comment?comment_id=2" for 127.0.0.1 at 2018-10-05 22:01:37 +0600
Processing by ProductsController#add_comment as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"token", "comment_id"=>"2", "commit"=>"Add comment", "id"=>"1"}
add_comment_product PATCH /products/:id/add_comment(.:format) products#add_comment
您已将其声明为 成员 路由,但我没有看到您为 :id
路径助手传递任何值。
尝试将其更改为
<%= form_with(url: add_comment_product_path(product), local: true) do |form| %>
其中 product
是 Product
实例。
Rails 处理此问题的方法是将其作为单独但嵌套的资源 - 因为您实际上是在创建新资源(评论)而不是修改产品本身。
这也使您的代码符合单一职责原则 (SRP),因为每个控制器仅处理单一类型资源的 CRUD。
您可以 nest resources 通过嵌套调用 resources
:
resources :products do
resources :comments, shallow: true
end
然后设置 CommentsController 来处理 CRUD 评论:
class CommentsController < ApplicationController
before_action :set_comment, only: [:index, :new, :create]
# GET /products/:product_id/comments
def index
@comments = @product.comments
end
# GET /products/:product_id/comments/new
def new
@comment = @product.comments.new
end
# POST /products/:product_id/comments
def create
@comment = @product.comments.new(comment_params)
if @comment.save
redirect_to @product, success: 'Comment created'
else
render :new
end
end
# ...
private
def set_product
@product = Product.find(params[:product_id])
end
def comment_params
params.require(:comment)
.permit(:foo, :bar)
end
end
要将表单操作属性设置为指向嵌套路由,您只需使用数组或命名的 product_comments(product_id: @product.to_param)
路由助手。
<%= form_with(model: @comment, url: [@comment.product, @comment], local: true) do |form| %>
<%= form.submit 'Add comment' %>
<% end %>
由于产品 ID 通过 URI 传递,因此无需通过隐藏输入传递它。
I think it has something to do with the fact that this form_with is nested into bigger form and it looks when I hit Add comment it triggers the outer submit
您应该注意 HTML 标准(HTML5 和更早的 (x)HTML 标准)不允许嵌套表单元素,并且行为可能非常不可预测,因为未指定浏览器是否应使用嵌套表单的 action 属性或将事件冒泡到父表单元素,这在您的情况下很可能发生。参见:http://w3.org/TR/html5/forms.html
我只需要将嵌套表单从更大的表单中移出即可使其正常工作。