使用来自另一个操作的按钮发送数据
Send data with button from another action
我想使用按钮将数据从 new
操作发送到 update
操作。
因此,如果用户点击取消按钮;
<%= 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"):
和请求控制器;
def update
@request = Request.find(params[:id])
puts situation = params[:situation]
if @request.update_attributes(situation)
flash[:success] = "Approved"
redirect_to new_user_request(current_user.id)
else
redirect_to new_user_request(current_user.id)
end
基本上,想发送一个文本数据来更新请求控制器中的操作。
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
以获得 "cancelled" 的 "situation"。
我会推荐几个修复方法:
您应该将 "situation" 列重命名为 status
(这样您就可以调用 @situation.status
你应该让你的 status
专栏减少对文本的依赖——它应该有一个严格的规范,也许让它成为 boolean
之类的。这将允许您调用 @situation.active?
您应该尽可能使用 Rails path helpers。发送 controller/action 信息的散列很好,但最好使用内置路径以获得更好的约定
我想使用按钮将数据从 new
操作发送到 update
操作。
因此,如果用户点击取消按钮;
<%= 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"):
和请求控制器;
def update
@request = Request.find(params[:id])
puts situation = params[:situation]
if @request.update_attributes(situation)
flash[:success] = "Approved"
redirect_to new_user_request(current_user.id)
else
redirect_to new_user_request(current_user.id)
end
基本上,想发送一个文本数据来更新请求控制器中的操作。
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
以获得 "cancelled" 的 "situation"。
我会推荐几个修复方法:
您应该将 "situation" 列重命名为
status
(这样您就可以调用@situation.status
你应该让你的
status
专栏减少对文本的依赖——它应该有一个严格的规范,也许让它成为boolean
之类的。这将允许您调用@situation.active?
您应该尽可能使用 Rails path helpers。发送 controller/action 信息的散列很好,但最好使用内置路径以获得更好的约定