Some usages of link_to and button_to

Source: Internet
Author: User
Tags locale

The page originally designed a button, the function is to jump to the index method when pressed, and then implement a function. The key is to pass a parameter Show_all with a value of true.
The index method implements different logic by determining whether it contains this parameter, similar to the following:

If Params[:show_all]
      @products = product.all
    else
      @products = product.where ("locale = =?", I18n.locale.to_ s) End
    

The button is generated by the following code:

<%= button_to (' Show All Products ", Store_index_path (Show_all:true), method:: Get)%>

The actual HTML code generated is as follows:

<form class= "button_to" method= "Get" action= "/cn/store/index?show_all=true" ><input "type=" Submit "value=" Show All Products "/></form>

Exasperating is that the code Show_all parameters are not passed, in the Rails s console is always seen/cn/store/index behind? show_all=true this! When you change the Button_to method parameter to: Post, Show_all parameters can be passed to the background normally, but I do not want to post the way Oh!
Then decided to use Link_to first try:

<%= link_to (t (". show_all_html"), Store_index_path (Show_all:true), class: ' Locale ')%>

The generated code is as follows:

<a class= "locale" href= "/cn/store/index?show_all=true" > Show All Products </a>

Note that the link URL section is identical to the one generated by Button_to! When you click the link, the parameters are passed correctly.
Is it necessary to use link_to to pass arguments without button_to: Get? The answer is NO! After some search, the button_to is implemented using the following methods:

<%= button_to (' Show All Products ', Store_index_path,method:: get,params:{show_all:true})%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.