我想通过一个按钮从[
{
"id": 1,
"name": "apple"
},
{
"id": 2,
"name": "banana"
},
{
"id": 3,
"name": "orange"
},
]
操作向update
操作发送数据。
因此,如果用户点击取消按钮;
new
和请求控制器;
<%= button_to "Cancel", {:controller => :requests,
:action => 'update', :user_id =>received.user.id, :id => received.id }, :value => "Cancelled" %>
The value has to be sent to the `update` action. But instead I receive an error;
Started POST "/user/295/requests/29" for ::1 at 2015-10-17 12:35:30 +0300
ActionController::RoutingError (No route matches [POST] "/boats/295/requests/29"):
基本上,想要发送文本数据来更新请求控制器中的操作。
答案 0 :(得分:1)
Arup的回答将让button_to
为你工作。
为了给你一些代码,你需要以下内容:
<%= button_to "Cancelled", requests_path(received.id, received.user.id), method: :put %>
关于代码本身,你最好这样做:
<%= button_to "Cancelled", requests_path(received.id, received.user.id), method: :put, {params: {situation: "Cancelled"}} %>
#app/controllers/situations_controller.rb
class SituationsController < ApplicationController
def update
@situation = Situation.find params[:id]
@situation.update situation_params
end
private
def situation_params
params.require(:situation).permit(:situation)
end
end
这样您就可以更新@situation
以获得“取消”的“情况”。
我会推荐几个修复:
您应该将“情境”列重命名为status
(以便拨打@situation.status
您应该使status
列更少依赖于文本 - 它应该具有严格的规范,可能会使其成为boolean
或其他内容。这样您就可以拨打@situation.active?
您应始终尽可能使用Rails path helpers。发送控制器/操作信息的哈希很好,但最好使用内置路径以获得更好的约定