Home How to use Rails link_to to POST to the controller and have an HTML class attribute?
Reply: 2

How to use Rails link_to to POST to the controller and have an HTML class attribute?

Mark Kadlec
1#
Mark Kadlec Published in 2018-02-07 02:40:55Z

I've looked up the link_to in the Apidock but simply can't get the proper Rails syntax to create the following:

<a href="/benefits/create" class="btn btn-primary">All Changes Complete</a>

I have:

<%= link_to t('view.benefits.all_changes_complete'), { controller: "benefits", action: "create", method: :post }, { class: "btn btn-primary"} %>

The above syntax kind of works, but the method: :post is being added to the href and clicking the link does a GET, resulting in the following:

<a class="btn btn-primary" href="/benefits/create?method=post">All Changes Complete</a>
fool-dev
2#
fool-dev Reply to 2018-02-12 03:42:20Z

Simply try this.

Controller is benefits then if you run the command rake routes then it will be show looks like this

routes.rb

resources :benefits

Prefix Verb     URI Pattern                  Controller#Action
benefits GET    /benefits(.:format)          benefits#index
         POST   /benefits(.:format)          benefits#create

Create action is benefits_path when it going to post method than link_to will be

<%= link_to "All Changes Complete", benefits_path, method: :post,  class: "btn btn-primary"%>

It will generate HTML looks like

<a rel="nofollow" data-method="post" href="/benefits" class="btn btn-primary">  All Changes Complete</a>

After clicking then it action looks like

POST   /benefits(.:format)          benefits#create

Then if you need send parameters then link_to looks like

<%= link_to "All Changes Complete", benefits_path(param1: 'value1', param2: 'value2'), method: :post, class: "btn btn-primary" %>

It will generate looks like

<a rel="nofollow" data-method="post" href="/benefits?param1=value1&param2=value2" class="btn btn-primary">All Changes Complete</a>

For receiving parameters

params[:param1]
params[:param2]

That's it

Hope it helps

BKSpurgeon
3#
BKSpurgeon Reply to 2018-02-07 03:38:21Z

When to use get request

  • Use the get request if you are not changing state. if you are basically displaying information.
  • Do not use the get request to create records. It is not a standard rails practice. You will not be protected from cross site request forgery if you use a simple link which will create records from a get request.

When to use post

Use post if you are creating new records ( or changing records via patch).

  • If you are not doing a get request then you'll basically need to use a form (however you solve the issue). Something like this:

form_tag({:controller => "benefits", :action => "create"}, :method => "post", :class => "btn btn-primary")

The purpose

  • to indicate the user has reviewed the form? This sounds like a retrieval of information so you could use a get request via a named route.
  • you could also use a helper method to supply the same information without using a named route.

  • you could also use a check box on the original form that the user must tick to indicate that they have read the form etc prior to submitting the form.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.313014 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO