Passing parameters from view to controller

前端 未结 2 1096
礼貌的吻别
礼貌的吻别 2020-12-03 08:21

I got a bit of a newbie question. I\'m trying to pass a variable from my view to my controller. Is there anyway my method in my controller can receive variables from my view

2条回答
  •  春和景丽
    2020-12-03 08:41

    You can add information to the params hash right through the link_to. I'm not sure exactly what you are trying to do but I did something like this recently to add the type of email I wanted when I link to the new email

    <%= link_to 'Send Thanks', new_invoice_email_path(@invoice, :type => "thanks") %>
    

    Now my params looks like:

    {"type"=>"thanks", "action"=>"new", "controller"=>"emails", "invoice_id"=>"17"}
    

    I can access the type via the params

    email_type = params[:type]
    

    Instead of a string, if you pass in the instance variable @rela you will get the object_id in the params hash.

    Per the comment below, I'm adding my routes to show why the path new_invoice_email_path works:

    resources :invoices do
      resources :emails
    end
    

提交回复
热议问题