Hello again stackoverflowers!
I want to create links but not using the default :id attribute, I would like for example
article_path(#article)
to generate
/articles/example-article
instead of the default
/articles/1
Bonus, I would like to make articles the default controller. I achieved this using:
root 'articles#index'
get ':name' => 'articles#show', as: 'article'
This works fine, but article_path still generates the id link
Try the friendly id gem:
https://github.com/norman/friendly_id
It is a powerful gem, I used in my old project.
have a look the following railscast to learn more about it
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid?view=asciicast
Related
I have a route with a namespace
namespace :publishers do
resources :authors
get 'books' :to => 'books'
get 'books/custom_report/:id', :to => "curriculos#custom_report"
end
Often I will have to make links in my application and I know than it`s possible to use a alias for routing like this:
<%= link_to "Books", publishers_books_path %>
I call that publishers_books_path a alias route, does this is the correct name?
Furthermore, I still not able to understand the logic with this alias naming because i can`t use for a new or a custom action like this
link_to 'Show the report', publishers_books_custom_report_path(params[:id])
I'm always get a error of undefined_method for publishers_books_custom_report_path
So there`s some questions
First of all whats it`s the correct name of this feature in RoR?
How I can use the custom_report as aliases to link_to? And also if i need to use some basic operations like new, update, insert?
Can someone give me the link to the documentation to really understant that feature?
First of all whats it`s the correct name of this feature in RoR?
The docs use "path helper" and "named route helpers" interchangeably.
How I can use the custom_report as aliases to link_to?
Use rails route or visit /rails/info/routes in your dev server to get a list of all your routes, their helpers, and controller actions.
Apparently it is publishers_path which doesn't seem right. You can fix this with an as.
get 'books/custom_report/:id', to: "curriculos#custom_report", as: :books_custom_report
And also if i need to use some basic operations like new, update,
insert?
A get declares just that one specific route. If you need all the operations on a model, declare it as a resource.
namespace :publishers do
resource :authors
resource :books
get 'books/custom_report/:id', to: "curriculos#custom_report", as: :books_custom_report
end
Can someone give me the link to the documentation to really understand that feature?
Rails Routing From The Outside In.
I'm trying to write a simple CMS in rails that will allow text urls rather than the standard /posts/show/<id>.
I have a model that has a title and body, I would like to replace spaces in the title and use it as the url.
I also want to have the url at the root level, not past /pages/show.
Take a look at this gem friendly_id, is used for create url from fields of the model storing it on database. Otherwise, you can create the route that you want example get 'pages/show', home#index and pointing wherever you want.
While the friendly_id gem is really nice, it's quite heavy for such a simple need. You might consider a lighter alternative approach : https://github.com/johnotander/urls_for_humans
You can override your rails urls by setting them like this:
get 'posts' => 'posts#index'
In your routes.
But since you want to format the url for the show action (which depends on a specific id for each item in posts) your best bet is to use the awesome friendly_id gem.
See: https://github.com/norman/friendly_id
Some resources:
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
http://blog.teamtreehouse.com/creating-vanity-urls-in-rails
I have figured out how to do what I wanted to do.
I am setting a route to the root url with a parameter like this (config/routes.rb):
get ':title', to: 'pages#show', as: :page
Now I have a pages controller that was generated by rails generate scaffold Page title:string body:text
which has a function set_page which I changed to do this (app/controllers/pages_controller.rb):
def set_page
#page = Page.find_by("title" => params[:title])
end
I'm new to Rails, and I'm setting up models/controllers for Course and some other models.
When I visit the /courses/show URL in my browser I get the following error:
Couldn't find Course with 'id'=
Screenshot here.
Here's the relevant line from my rake routes and routes.rb:
rake routes
courses_show GET /courses/show(.:format) courses#show
config/routes.rb
get 'courses/show'
You have specified the four routes without any :id parameter, I don't know why you would expect them to have an :id parameter.
I'd recommend that you read the Rails guide on routing and also read the comments in the generated config/routes.rb, in that file you'll see comments like this:
# Example of regular route:
# get 'products/:id' => 'catalog#view'
So, extrapolating that to your example you might end up with:
get 'courses/:id' => 'courses#show'
The example that follows that one shows how to add a named route helper using the :as option:
get 'courses/:id' => 'courses#show', as: :courses_show
Something you'll also see when you read the guide or the comments is that you can use the resources helper to create standard restful routes.
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
From the looks of some railscasts (this one in particular), it seems like there is some autogeneration of "*_path" variables that not happening for me. in this rails cast, the edit_mutliple_products_path seems to be generated automagically (i don't normally like using that word). When I follow through the same steps and try to access a similar path, i get this:
undefined local variable or method `edit_multiple_distributions_workflows_path' for #<#<Class:0x132b18a68>:0x132af3290>
This is rails 2.X. Rails routes changed in Rails 3. in order to get this route add below to routes.rb:
resources :products do
collection do
post 'edit_multiple'
put 'update_multiple'
end
end
You will be able to access this paths with
edit_multiple_products_url
edit_multiple_products_path
update_multiple_products_url
update_multiple_products_path
instead of edit_multiple_distributions_workflow_path. Btw where did you get this path from? I did not see it in the railscast.
In the given tutorial, which looks like it's from an older Rails, this is the line which would generate the path methods:
map.resources :products, :collection => { :edit_multiple => :post, :update_multiple => :put }
In rails 3, you can see its usage in the docs here: http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default