[英]How to pass id to an action in the controller - Ruby on Rails
I am creating reports with the axlsx gem and I need to pass the id of the event that I select to generate its report, but I do not know how to get that id in the action of my controller that I created. 我正在使用axlsx gem创建报告,我需要传递选择生成事件报告的事件的ID,但是我不知道如何在我创建的控制器的操作中获取该ID。
I am created this table where I must obtain the id that I want to send to the action: 我创建了此表,在其中必须获取要发送给操作的ID:
This is the code of my table, Here I try to pass the id, but I do not know how to get it in the controller: 这是表的代码,在这里我尝试传递id,但是我不知道如何在控制器中获取它:
def data
events.map do |event|
[
event.name,
(
link_to(
raw("<button id='button' class='btn btn-default'><i class='fa fa-file-text' style='cursor: pointer;'></i></button>"),
assistants_report_admin_report_assistants_by_events_path(event.id, format: :xlsx)
)
)
]
end
end
The action on my controller: 我的控制器上的操作:
def assistants_report
@event_report = Event.where(id: "I need the id here!")
respond_to do |format|
format.xlsx {render xlsx: 'report_all_assistants', :filename => "aa.xlsx", layout: false, disposition: "attachment"}
end
end
routes.rb: route.rb:
resources :report_assistants_by_events do
collection do
get "assistants_report"
end
end
You need to use member
instead of collection
in your routes.rb
, then in your controller, you can access id by params[:id]
. 您需要在
routes.rb
使用member
而不是collection
,然后在控制器中,可以通过params[:id]
访问id。
You also need to replace assistants_report_admin_report_assistants_by_events_path
with your new path after changing from collection
to member
. 从
collection
更改为member
后,还需要用新路径替换assistants_report_admin_report_assistants_by_events_path
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.