params.merge instead of :overwrite_params gives different URLs - ruby-on-rails

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"

Related

Internationalized I18n URL helpers

Is it possible to have URL helpers in Rails behave differently for different locales, eg.
<%= link_to "Something", example_path %>
in English would go to site.com/something, and in another language to site.com/lang/blahblah
Currently, my routes are defined like
scope '(:locale)', :locale => /otherlang/ do
get '/' => 'home#show'
get 'otherlang-about' => 'about#show'
get 'otherlang-something/:id' => 'example#show'
end
get 'about' => 'about#show'
get 'something/:id' => 'example#show'
root 'home#show'
Yes it is possible, We are using this gem https://github.com/enriclluelles/route_translator in my company to get different routes_url for each languages but still pointing to the same controller#method.
You just need to define a route.yml file to define the translation for each routes.

How to create an arbitrary link in Rails?

link_to and helpers use the names of my models and their IDs while I want to have a couple of different, arbitrary, variables in my link. I have no problems to route them, I actually keep the default routing as well, but I suddenly stuck that I cannot easily generate an arbitrary link. For instance I want to have a link like ":name_of_board/:post_number", where :name_of_board and :post_number are variables set by me, and when I use link_to I get instead "posts/:id", where "posts" it's the name of the controller. While it's not hard to use an arbitrary id like
link_to 'Reply', :controller => "posts", :action => "show", :id => number
I cannot get how I can get rid of "posts". So, is there an easy way to generate a link by variables or to convert a string into link? Sure, I can add other queries to the line above, but it will make the link even more ugly, like "posts/:id?name_of_board=:name_of_board".
You can create additional routes for your posts resource in your routes.rb, or make standalone named routes:
resources :posts do
get ':name_of_board/:id' => 'posts#show', as: :with_name_of_board
end
get ':name_of_board/:id' => 'posts#show', as: :board
Now this
#name_of_board = "foo"
#post_id = 5
link_to 'Reply', posts_with_name_of_board_path(#name_of_board, #post_id)
link_to 'Reply', board_path(#name_of_board, #post_id)
would link to /posts/foo/5 and /foo/5 respectively.
You should first edit your route entry, for example a classic show route is the follow:
get "post/:id" => "post#show", :as => :post
# + patch, put, delete have the same link but with different method
And you can call it with the following helper
link_to "Show the post", post_path(:id => #post.id)
You can edit or create a new entry in the routes, applying the parameters you want to use, e.g.:
get "post/:id/:my_platform" => "post#show", :as => :post_custom
Then
link_to "Show the post with custom", post_custom_path(:id => #post.id, :my_platform => "var")
Finally, the link generated for this last entry is for example:
"/post/3/var"
Even in this situation, you can add some other params not defined in the routes, e.g.:
link_to "Show post with params", post_custom_path(:id => #post.id, :my_platform => "var", :params1 => "var1", :params2 => "var2")
=> "/post/3/var?params1=var1&params2=var2"
RoR match your variable defined in the routes when you render a link (remember that these variables are mandatory), but you can ever add other vars that come at the end of the url ("?...&..")

How can I include unix path information in a rails link_to

I have an application which needs to handle path information (it will be working with files on the server). Here is how I'm trying to do the route.
match "viewfile/file=:vFile" => "home#viewfile"
and here is how I'm trying to link to the file
link_to("file",
{ :controller => "home", :action => "viewfile", :vFile => "/this/is/a/test" })
This, however, throws errors and does not work.
How can I do this?
link_to("file", { ... url_encode("/this/is/a/test/") })
Is more likely to work.
the problem is the routing does not know which route to match because multiple routes could match your controller/action/vFile combination, so name the route like:
match "viewfile/file=:vFile" => "home#viewfile", :as => 'viewfile'
now you can use the viewfile_path helper
link_to 'file', viewfile_path("/this/is/a/test")
PS: I don't know if it works with the equal (=) sign in the url, what definitely works is:
match "viewfile/file/:vfile" => "home#viewfile", :as => 'viewfile'
link_to 'file', viewfile_path("/this/is/a/test")
but give it a try...

Marking a string as HTML safe

I am trying to build my first Rails application and I'm using Ryan Heath's navigation_helper plugin to give me the current class in my navigation. I built my named routes as follows:
match 'games' => 'games#index', :as => :games
match 'new' => 'games#new', :as => :new
match 'previous' => 'games#previous', :as => :previous
match 'settings' => 'settings#index', :as => :settings
Then in my application_layout I added the following code
<%= navigation([:games, :new, :previous, :settings]).html_safe %>
From what I know of Rails the html_safe should force HTML to be rendered properly, but instead what I get is this:
<ul class="navigation">["<li class=\"current\"><a href=\"/games\">Games</a></li>", "<li class=\"\"><a href=\"/new\">New</a></li>", "<li class=\"\"><a href=\"/previous\">Previous</a></li>", "<li class=\"\"><a href=\"/settings\">Settings</a></li>"]</ul>
So am I doing something wrong or is the plugin doing something wrong? I know that the plugin was written back in 2.x days which from what I know handled HTML a bit differently, but I just don't know enough.
https://github.com/priceflex/navigation_helper/commit/ad7bf45db1845e9299e9da39cf214866b608dd47 try to use this fork wich fix issues for rails3
You can use raw() method to avoid escaping:
<%= raw(navigation([:games, :new, :previous, :settings])) %>

Optional path prefix persistence, using Sven Fuchs' routing filter

I made my routes recognize optional path prefixes, but now I want route generation to remember them without me specifying them each time. I'm using the solution presented here:
Creating routes with an optional path prefix
Here are some examples:
Let's say I'm here: { path => "/", :contoller => 'welcome', :action => 'index', :locale => 'en' } then route generation works like this:
events_path #=> "/en/events"
event_path(1) #=> "/en/events/1"
This is exactly what I want, and everything's great.
Now let's consider I'm here: { path => "/fr", :contoller => 'welcome', :action => 'index', :locale => 'fr' } then route generation works like this:
events_path #=> "/en/events"
events_path(1) #=> "/en/events/1"
This is not helping me at all. What it would be natural to have is events_path to remember params[:locale] and generate "/fr/events". Is there any way I can achieve this?
Unless I'm misunderstanding what you're saying the desired behaviour is exactly the one I've written routing_filter for :)
Try using the provided locale filter by installing the plugin and simply adding map.filter(:locale) to your routes.
If that does not help, please email me or send me a message on github.

Resources