Setting additional parameters for the Ruby on Rails root on routes. Rb

I am using the static_pages controller to serve up all the content of my static page. I want to be able to pass parameters to this static page content to open multiple modal fields for login and popup messages.

root 'static_pages#home'

      

How can i do this?

+3


source to share


2 answers


You can do this in a view using Javascript if certain parameters exist.

For example in home.html.erb

<% if params[:your_param] == true %>
  <script type="text/javascript">
    // fire modal
  </script>
<% end %>

      

And then just add your parameter to the query string:



http://www.yoursite.com/?your_param=true

      

No need to modify the routes file. Modals are related to the view, so why not keep the logic in the view?

As far as your comment is about conditional redirection sounds like a job for the controller. You also have access to the parameters in the controller, so you can redirect as needed based on the parameters passed to the action.

+3


source


One way to do it that I used routes.rb

is this:

 # config/routes.rb
 get 'posts/:id(/:opt1(/:opt2(/:opt3)))' => 'posts#show'

      

Then we debug the parameters in the view

 # app/views/posts/show.html.erb
 <%= params %>

      



If url posts/1/A/B/C

This shows:

 {"controller"=>"posts", "action"=>"show", "id"=>"1"
  "opt1"=>"A", "opt2"=>"B", "opt3"=>"C"}

      

The parentheses in the route are optional and are treated as parameters for the non-parenthesis portion of the route.

+2


source







All Articles