简体   繁体   English

如何将ID传递给控制器​​中的动作-Ruby on Rails

[英]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:

My table 我的桌子

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.

相关问题 如何将参数传递给Rails中的ruby中的控制器动作 - how to pass a params into a controller action in ruby on rails 在Ruby on Rails中,如何将控制权传递给另一个控制器中的操作? - In Ruby on Rails, how do I pass control to an action in another controller? 如何在Ruby on Rails中将GET参数传递给下一个控制器动作? - How to pass GET parameters to next controller action in Ruby on Rails? 如何从控制器Ruby On Rails中传递表中的用户ID - How to pass user id in table from controller Ruby On Rails Ruby on Rails-通过另一个控制器传递ID - Ruby on Rails - Pass ID from another controller 如何覆盖 gem 控制器操作 Ruby on Rails? - How to override gem controller action Ruby on Rails? 如何在Ruby on Rails中自定义观察者以执行Controller的操作 - How to customize an Observer to a action of the Controller in Ruby on Rails Ruby on Rails-想要添加自定义动作,但控制器正在寻找ID参数 - Ruby on rails - wanting to add custom action, but controller is looking for ID parameter 如何使用页面ID /名称加载正确的控制器操作? (Ruby on Rails 4) - How do I use a page id/name to load the correct controller action? (Ruby on Rails 4) 如何在Rails 5的控制器动作中传递值? - How to pass value in controller action in rails 5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM