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})%>