How to pass a parameter to controller action from view in rails through link_to helper

前端 未结 1 1133
走了就别回头了
走了就别回头了 2021-02-06 03:58
<%= link_to \"Connect\", {controller:\"home\", action:\"connectTo\"}, id: \"btny\" %>

This is my link_to helper in view.

I w

1条回答
  •  执笔经年
    2021-02-06 04:27

    1. Do not use camel case in variable names and method names in Rails. That's not a convention and will bite you later.

    2. Use named path when you can, instead of manually assign controller and action.

    For your question, suppose your named path is home_connect_to_path, then

    link_to "Connect", home_connect_to_path(foo_param: 'bar_value')
    

    The link will look like

    http://localhost:3000/home/connect_to?foo_parms=bar_value
    

    Then get it in controller

    def connect_to
      foo = params[:foo_param] # 'bar_value'
    

    0 讨论(0)
提交回复
热议问题