make pretty url with routes.rb - ruby-on-rails

I would like to do something to this effect, I believe:
map.connect 'show/:company_name/:id',
:controller => 'companies',
:action => 'show'
Basically, each time the show action is called, I would like it to take the company_name param and place it into the url as such (show/:company_name/:id)
However, it seems I am using old (rails 2.x routing api) and cannot use map.connect without getting an error. How can I upgrade this?
Is there some way to do this with "match"?
Thanks!
===================
This is the error I see when I try to use map.connect:
undefined local variable or method `map' for #<ActionDispatch::Routing::Mapper:0x103757458>

I think your routes lack a "/" symbol in the first line.
Try this:
match '/show/:company_name/:id' => 'companies#show'
You can check your routes path with command rake routes.
--
Besides, the show action is the default RESTful method in Rails. I'll suggest you change a equivalent word, and reserve "show" action for future or other situation.
In Rails convention, you can write resources :companies, and the path will be /companies/:id using show action.
Some adjustment, in app/models/company.rb
def to_param
self.name
end
So your url will look like http://yourdoamin.com/companies/37signals.
In app/controllers/companies_controller.rb
#company = Company.find_by_name(params[:id])

If I'm understanding your goal, try
match 'companies/show/:company_name/:id' => 'companies#show'

Related

Changing the params in URL on rails

I want to change the :id param on the URL. I added to my routes.rb file something like:
match "articles/:name/edit", :to => 'articles#edit', :as => 'edit_article'
Thinking that :name would be readed by the server as params[:name] later for me in rails. I edited my article controller definition for edit so:
def edit
#article = Article.find(params[:name])
end
I get always the error couldn't find article with id=test and I was wondering why "id" instead of :name? I tried also changing match to get but I got the same.
I have also the default resources :articles still in my routes.rb file, don't know if there's something like a double rule working there.
The whole thing is that instead of ID numbers I would use names in my URL —not just the edit one, with the show method I could handle it, but not with edit/update/delete.
I was reading about routing but I can't figure out what I am doing wrong.
By default, find search by id.
You should replace it with find_by_name.
Advice: use friendly_id

Do you have to define controller helpers to get paths for new routes in rails?

I have a Customer model and I want his controller to repond to a find method
I added this in my routes.rb file:
match 'customers/find/:name' => 'mymodel#find' resources :customers
In my controller I have something like this:
def find
#customers = Customer.fin_all_by_name(params[:name])
end
in my views, when I need to create a link for that resource I'm using this:
= link_to 'Find By Name', :controller => "customers", :action => "find", :name => #customer.name
now, I'm trying integration tests with cucumber and I have a problem: I have to create a step definition in my customer_step.rb file for customers having same name:
when /^customers having same name as "(.*)"/
url_encode('/customers/find/' + $1)
now that line doesn't work, it says undefined method `url_encode'
I need to encode that string because if the name contains a space I get obvious errors.
I'm new to ruby and rails and I think I'm missing something here.
Am I following the right pattern to accomplish this search?
Should I define an helper method in my controller to generate search urls?
Is it correct that line I have in my _step.rb file?
I don't want urls to be like this: customers/find?name=test
but: customers/find/test
I just sorted it out, I slightly modified my match expression and added the :as parameter
and this gave me the possibility to call find_path() helper method
match 'customers/find/:name' => 'customers#find', :as => :find
Is this correct?
Using :as should indeed create a route helper for you. If you want to get a list of your matched routes, to which controller/action they route, and the name of the route helper, use rake routes in console.

Ruby on rails paths and routes

I'm trying to get the hang of basic Rails routing.
I have a model called page which I generated with a scaffold.
I have added a method called addchild which I would like to access through
'pages/addchild/:id'
So far so good. However, I want to set up a link to this method like so:
<%= link_to 'Add child page', addchild_page_path(page) %>
Passing the ID of the current page as a parameter.
When I load my index view (where the link is), I get the following message:
undefined local variable or method `addchild_page_path' for #<ActionView::Base:0xb67797d0>
Have I misunderstood how the path/link_to method works?
My routes file looks like this:
map.resources :pages
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
Any advice would be greatly appreciated.
Thanks.
You need to add a route to it to be able to use the named path methods.
Since you mentioned you used scaffolding, you probably have the route setup as a resource, so all that you need to do is add the method:
map.resources :pages, :member => {:addchild => :get}
Would give you an addchild_pages_path (and the actual created path would look like /pages/:id/addchild
You then use it like this: addchild_pages_path page, don't call the id method directly since it is not resourceful (you won't use the to_param in the page class, which you might want to do later).
If you really want the url to show up as /pages/addchild/:id (which I don't recommend) you can add
map.addchild_page "/pages/addchild/:id", :controller => :pages, :method => :addchild
before the map.resources :pages row in your routes.rb, and then use the path method as above.

quick rails named routes question

In my view I specify a named route like this:
show.html.erb ->
seo_path(:id => "45")
Now in my routes I define like so:
routes.rb ->
map.seo "/pages/:id/:permalink", :controller => "pages", :action => "show"
Below is the error message I am getting. Apparently the diff is the id, although I don't know why.
Update:
I am getting this as my error:
seo_url failed to generate from {:controller=>"pages", :id=>"45", :action=>"show"}, expected: {:controller=>"pages", :action=>"show"}, diff: {:id=>"45"}
Why not using the to_param method?
class YourModel << AR::Base
def to_param
"#{id}-#{title.parameterize}"
end
Thus getting routes like
/pages/1-here-is-my-converted-article-title-to-permalink
Besides that, it would seems Rails isn't recognizing your route. Have you restarted your web server? Try sending only a :permalink attribute to see how it goes.
I don't fully understand your question.
It looks like id is already specified in your named route, and in your view. It looks like you need to also specify permalink in your view. Are you getting an error?

Link_to action in nested controller?

So I've created an action, lets call it 'raise' in the controller 'elevator'. 'elevator' is nested in 'building'.
What routes should I create, or what link_to url can I make so
/buldings/2/elevators/4/raise will work?
Thanks,
Elliot
If you really want to nest them like that, here's the route. You didn't specify a Rails version, so this is something that will work with recent versions, as opposed to just in 2.3+.
map.resources :buildings do |buildings|
buildings.resources :elevators, :member => {:up => :put}
end
Note the name change for your action. Please don't name an action "raise." That's a method in Kernel. You're going to give someone an aneurism when they try to debug your code.
You'll end up with a path helper that looks like this.
up_building_elevator_path(:building_id => 2, :elevator_id => 4)
After setting up the routes, you can see all of the routes available to your elevators by running:
rake -T | grep elevator

Resources