Rails url_for from string with parameters - ruby-on-rails

I've got a site-local tinyurl I'm trying to make, so I need to show the full path of a URL that doesn't correspond to a controller action, and this doesn't work:
url_for("tiny/#{identifier}", :only_path => false)
because the url_for that takes a string doesn't then take any parameters.
How can I accomplish this?
Edit as per comment:
config/routes.rb:
get 'tiny/:id' => "original_controller#show", :constraints => {:id => /\d+/}
get 'tiny/:name' => "original_controller#by_name"
rake:
GET /tiny/:id(.:format) original_controller#show {:id=>/\d+/}
GET /tiny/:name(.:format) original_controller#by_name

Related

params.merge instead of :overwrite_params gives different URLs

In my old Rails 2.3 application I had something like:
link_to 'Portuguese', url_for(:overwrite_params => { :lang => 'pt' })
which was returning me URLs formatted as:
.../pt
Now that I've upgraded to Rails 3.0 :overwrite_params doesn't work any more, they say params.merge can be used instead to have the same result.
This is true, the page I land to is the same, yet
link_to 'Portuguese', params.merge(:lang => 'pt')
gives me URLs the kind of:
.../?lang=pt
How could I maintain the same URLs as before?
you can use a match statement. In routes.rb file you need to mention something like
get "sign_up/:lang" => "users#new"
I tried this in console
1.9.3-p429 :001 > include Rails.application.routes.url_helpers
1.9.3-p429 :011 > url_for(:only_path => true, :controller => 'users', :action = 'new' , :lang=>'pt')
=> "/sign_up/pt"
In this manner you will pass the parameter lang as '/pt' and not '/?lang=pt'
I had this horrible-looking workaround working:
after defining a matching route
match '(/:lang)', to: 'home#index', as: :home
I create the link to it as:
link_to "Portuguese", URI(request.referer).path + "pt"

Rails custom routes with params

I am trying to display params in my url so i have added
patient_record_path(:limit => 10)
I am now trying to correctly route this.
Currently i am getting the error
No route matches {:action=>"show", :controller=>"patient_record", :limit=>10}
I am currently using the route
match 'patient_record/show&limit', :to => 'patient_record#show'
You should not add the limit to your route. Just simple define your route like this:
match 'patient_record/show', :to => 'patient_record#show', :as => 'patient_record_show'
A better solution however would be
resources :patient_records
This would create the following path helpers:
patient_records_path => "/patient_records" => 'patient_record#index'
new_patient_record_path => "/patient_records/new" => 'patient_record#new'
edit_patient_record_path(:id) => /patient_records/:id/edit => 'patient_record#edit'
patient_record_path(:id) => "/patient_records/:id" => 'patient_record#show'
Update: wrong use of path helper
I have looked at your question again and found another bug: the path helper for show needs a record. The correct use is:
# path to show
patient_record_path(#patient_record, :limit => 10)
# path to index
patient_records_path(:limit => 10)

rails sunspot gem friendly SEO url's?

How does one get SEO friendly url's in sunspot?,
The method of search form is GET as suggested from the rails docs, but now i have a very long query string that looks terrible, is it possible to have it something like
/search/param1/bla/param2/bla
instead of the long
?search&param1=somevalue&param2=someval2
You could modify your search route to include the params. Something like this within your routes.rb:
match '/search/:param1/:param2' => 'search_controller#search_action', :as => :search_with_params, :via => :get
Then a user would visit:
/search/value1/value2
Which gives you params[:param1] and params[:param2] to access the values from the url.
If those 2 params are not required for all searches I believe you would also need a route just for the search action:
match '/search' => 'search_controller#search_aciton', :as => :search, :via => :get

Github like routes in Rails

Using github like chain routes in rails
I have URLs similar to this:
'localhost:3000/document_managers/[:module_name]'
'localhost:3000/document_managers/[:module_name]/1/2/3/.' # can be any level deep
Here is the route definition for them:
map.connect '/document_managers/:module',
:controller => "document_managers",
:action => :new_tree,
:module => ["A","B","C"]
map.connect '/docuemnt_managers/:module/*path',
:controller => "document_managers",
:action => "new_tree",
:module => ["A","B","C"]
Here is the problem:
The idea that module name value can't be anything except from the
given above array i.e("A","B","C") like at any time the URL must be something like
localhost:3000/document_managers/A/1 or
localhost:3000/document_managers/B/221/1 or
localhost:3000/document_managers/C/121/1
but that not the case even though
localhost:3000/document_managers/D/121/1 is treated as valid url
and module is set to D even though the "D" is not in listed array
above
I want the the URL localhost:3000/document_managers/A to
also redirect to same action i.e new_tree if the extra parameter isn't
provided as in the URL contain extra parameters
localhost:3000/document_managers/C/121/1 then the URL is redirected
appropriately to the desired controller and action but if the URL only
contain the path until the module name the Rails return a routes
ActionController::UnknownAction I don't know why as I have already
defined the controller and action.
In Rails 3.1, you can do this in your routes file to get what you want:
match '/document_managers/:module',
:controller => "document_managers",
:action => :new_tree,
:constraints => {:module => /[ABC]/}

url_for generates a url with current path inserted

I am generating a url in my controller accessed from the relative path "/hangouts/test", for a url on an external site (facebook). I want to use url_for and pass in params using hashes so it can escape them. The URL I want is this:
http://www.facebook.com/connect/prompt_permissions.php?api_key=6aca22e72866c7eaaedfb15be69c4b93&...
Using this, however:
url_for(:host => "www.facebook.com/connect/prompt_permissions.php?", :api_key => Facebooker.api_key, :next => test_hangouts_url, :cancel => root_url, :ext_perm => "publish_stream")
I instead get my current path of /hangouts/test thrown in there:
http://www.facebook.com/connect/prompt_permissions.php/hangouts/test?api_key=6aca22e72866c7eaaedfb15be69c4b93&...
As you can see, I don't want "/hangouts/test" to be in there - played a bit with the options in the API docs but stumped, anybody know how to use url_for without it inserting the current path? Thanks!
You shouldn't be using the hash form of url_for to generate links outside of your application.
Instead you should just be using the string version form:
url_for "http://www.facebook.com/connect/prompt_permissions.php?api_key=6aca22e72866c7eaaedfb15be69c4b93&next=#{test_hangouts_url}&cancel=#{root_url}&ext_perm=publish_stream"
url_for will use the current action controller/action/id unless the hash given to url_for contains one of those arguments. url_for will then generate a url from a route that matches the arguments given in the hash. The arguments to url_for used in the question generates a valid url, because it can match a route using the current controller/action. You will not be able to generate the url you want with the hash form of url for without matching a route. Providing a controller in the hash you give url_for should match the default routes that rails generates for you when you create an application, but that's not very DRY.
The better solution is to use a named route:
map.prompt_fb_permissions "/connect/prompt_permissions.php",
:host => "www.facebook.com", :controller => nil, :action => nil
then you can use the following in place of url_for whenever you want to generate this this url.
prompt_fb_permissions(:api_key => Facebooker.api_key, :next => test_hangouts_url,
:cancel => root_url, :ext_perm => "publish_stream")
You left out the controller parameter, which is why it's automatically adding your controller path ("/hangouts/test"), to your base host.
Try this:
url_for(:host => "www.facebook.com", :controller=> "/connect/prompt_permissions.php", :api_key => Facebooker.api_key, :next => test_hangouts_url, :cancel => root_url, :ext_perm => "publish_stream")

Resources