Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get URL parameters in Rails?

When a POST request is processed, params[:id] returns the value of "id" stored in the form that was posted.

However I want to get the value stored in the url (query string).

def some_action
  id = params[:id] # Gets id from here: /some_action?id=[VALUE] only when request.post? is false
  if request.post?
  # Do some stuff, can't get the id value from the url
  ...
end

So how to get the value from the URL in a POST request?

like image 624
Alex Avatar asked Oct 30 '10 12:10

Alex


People also ask

How do I get the current URL in Ruby?

You should use request. original_url to get the current URL. For Rails 3: You can write "#{request.

What is strong parameters in Rails?

Strong Parameters, aka Strong Params, are used in many Rails applications to increase the security of data sent through forms. Strong Params allow developers to specify in the controller which parameters are accepted and used.


1 Answers

it's the same actually as GET parameters. To prove that, I have a search form that posts one parameter to the same action :

def invite

    if request.post? 

        search= [params[:search]]
        #...

    end
end

And in the view - maybe your problem comes from this

<% form_tag do %>
        <%= text_field_tag :search, params[:search] %>
        <%= submit_tag "Rechercher" %>
<% end %>
like image 150
Marcel Falliere Avatar answered Sep 18 '22 18:09

Marcel Falliere