passing array parameters to path - ruby-on-rails

I need to pass parameters to a path so that the path looks like the following:
http://localhost/submission_app/submissions?search_submission_type=ISH&submission_status_arr[]=51
I tried
submissions_path(:search_submission_type => "ISH", :submission_status_arr[] => 51 )
But am getting wrong number of arguments (0 for 1..2) error message on my view page.
I then tried:
submissions_path("search_submission_type=ISH&submission_status_arr[]=51")
But this one gives me the following url (Note the dot instead of &amp before the argument)
http://localhost/submission_app/submissions.search_submission_type=ISH&submission_status_arr[]=51
How do I need to pass the parameters so that I get the correct format for the url?
Your suggestions are most appreciated. Thank you

Rails uses parameter[]=value to signify that parameter should be considered an array.
You just need to pass an array to the path helper to get rails to generate the path for you.
submissions_path(:search_submission_type => "ISH", :submission_status_arr => [51] )

Correct syntax is
submissions_path(:search_submission_type => "ISH", :submission_status_arr => [51] )

Try this:
submissions_path(:search_submission_type => "ISH", "submission_status_arr[]" => 51)

Related

How to handle similar urls in rails 4 explicitly

i am trying to make more semantic url like below:-
//with one params
get 'location/:location' => 'home#show_by_location', as: :show_by_location
//with two params
get ':location/:name/' => 'home#show_details' , as: :show_details
they are almost similar but they are not working as second url never gets called as rails think they are same and hence calls only first one(as expected).So how can i treat both of them differently.
Thanks in advance.
Swap the order and use a constraint to tell rails that :location cannot be the string 'location':
get ':location/:name/' => 'home#show_details', constraints: { location: /^(location)/ }, as: :show_details
get 'location/:location' => 'home#show_by_location', as: :show_by_location

add get parameter to helper methods

I added the following line in config/routes.rb file :
get '/movies/similar', :contoller => 'movies', :action => 'similar'
I see that there is a helper method with name 'movies_similar_path'. I want to add a get parameter to 'movies_similar_path' with name 'director' so I can get '/movies/similar?director=someone'
How can I do it?
No need to define parameter in the helper, you can assign it in the link_to.
link_to "Similar", movies_similar_path(:director => #director.id)
noticed the typo in 'controller' in your routes line?
What you want, you do in the form of page you are calling by adding a hidden field, or putting the extra fields in the 'link_to'. Like "link_to "simsforthisdirector", movies_similar_path(#movie), :director=>#director.name
See: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
Especially the examples will help you out.
If you are using rails 3 then you can declare route as -
get '/movies/similar(/:director)', :to => 'movies#similar'
and while using you can use it as -
movies_similar_path
or
movies_similar_path(#director.name)

How to map an optional URL parameter to params[:something] in Rails 3 "routes.rb"?

I have the following line in my config/routes.rb:
match "/assets/:id" => "assets#my_action"
I would like to change it so that both:
/aasets/id
and
/aasets/id/download
will be mapped to assets#my_action, and in case the URL ends with /download I'll have a sign inside my_action (say, params[:something] won't be nil).
How could I do this ?
You can do it in 1 line with :download as an optional parameter e.g.:
match '/assets/:id(/:download)' => "assets#my_action"
Then within your controller params[:download] could be used to check what was after the id in the URL, if anything.
You can simply match both routes to the same controller.
match "/assets/:id" => "assets#my_action"
match "/assets/:id/:downloadparams" => "assets#my_action"
Your check for params[:downloadparams] will handle the rest.
EDIT: Oh "download" won't be a variable right? You could try something like
match "/assets/:id/:action" => "assets#my_action"
Check if params[:action] == "download" and ignore the parameter if it isn't.

Rails: Opposite of Hash#to_param

If I convert a hash to a query string, how can I convert it back again?
{:filters => {:colour => ['Red', 'Blue'], :size => 'Medium'}}.to_param
=> "filters[colour][]=Red&filters[colour][]=Blue&filters[size]=Medium"
Rails appears to do this automatically when it populates the params hash, but is it possible to call this method directly?
Thanks.
You're looking for Rack::Utils.parse_nested_query(query), which will convert it back into a Hash. You can get it by using this line:
require 'rack/utils'
query_string = "filters[colour][]=Red&filters[colour][]=Blue&filters[size]=Medium"
CGI::parse(query_string)

Rails routes match full hostname with multiple period in between

I'm trying to match a URL something like
http://example.com/this.is.my.full.hostname/something/else
apparently when I pass the param in the routes.rb file it doesn't recognize this parameter
my code says the following
match '/:computer_hostname/somethingelse' => 'test#info'
any ideas what's the right way to achieve the URL I wanted above ? Or is it even possible ? I know period is allowed character in URL but does it allow more than one ?
I think the constraints method/option will help you out. Try something like the following:
match ':hostname/something/else' => 'test#info',
:constraints => {:hostname => /[A-Za-z0-9\._\-]+/}
If you're doing multiple matches all with the same :hostname segment, then you can wrap them in a constraints method call:
constraints(:hostname => /[A-Za-z0-9\._\-]+/) do
match ':hostname/something/else' => 'test#info'
match ':hostname/foo/bar' => 'test#foo'
end

Resources