I have the following loop in my view
<% #posts.each do |post| %>
<%= link_to post do %>
Some html
<% end %>
<% end %>
The above code will generate link as localhost:3000/posts/sdfsdf-sdfsdf
But I would like to have the link as localhost:3000/sdfsdf-sdfsdf
Here is my route
resources :posts, except: [:show]
scope '/' do
match ':id', to: 'posts#show', via: :get
end
You could do this:
#config/routes.rb
resources :posts, path: "" #-> domain.com/this-path-goes-to-posts-show
--
Also, make sure you put this at the bottom of your routes; as it will override any preceding routes. For example, domain.com/users will redirect to the posts path unless the posts path is defined at the bottom of the routes.rb file
--
friendly_id
In order to achieve a slug-based routing system (which works), you'll be best suited to using friendly_id. This allows the .find method to look up slug as well as id for extended models:
#app/models/post.rb
Class Post < ActiveRecord::Base
extend FriendlyID
friendly_id :title, use: [:slugged, :finders]
end
This will allow you to use the following in your controller:
#app/controllers/posts_controller.rb
Class PostsController < ApplicationController
def show
#post = Post.find params[:id] #-> this can be either ID or slug
end
end
you need to tell routes what the name of the path gonna be.
in routes.rb you can do something like:
get '/:id', constraints: { the_id: /[a-z0-9]{6}\-[a-z0-9]{6}/ }, to: 'posts#show', as: :custom_name
after that when you run 'rake routes' you will see:
Prefix Verb URI Pattern Controller#Action
custom_name GET /:id(.:format) post#show {:id=>/[a-z0-9]{6}\-[a-z0-9]{6}/}
Now that you have the prefix verb, you can use it to generate the link:
<%= link_to 'Show', custom_name_path( post.id ) do %>
Some html
<% end %>
Related
I've created a controller Pages and some actions for simple pages (contact us, for instance), then I went to routes.rb and created a route to allow users to go directly to /contactus, instead of /pages/contactus.
How can I point link_to to the action, but still getting the right route url?
get :contact_us, to: 'pages#contact_us'
or
get :contact_us, controller: :pages, action: :contact_us
this will generate path contact_us_path or url contact_us_url
HEARE MORE ABOUT ROUTES IN RAILS
#config/routes.rb
resources :pages, path: "", only: [] do #-> has to be above everything in routes file
collection do
get :contact_us #-> url.com/contact_us
get :about #-> url.com/about
end
end
root ...
You'd link to it as follows:
<%= link_to "Contact", pages_contact_us_path %>
You can do this:
get '/contactus', to: 'pages#contactus'
Your link can be:
<%= link_to "Contact Us", contactus_path %>
For more information, see: http://guides.rubyonrails.org/routing.html#connecting-urls-to-code
This is the syntax for a simple route using the contactus action in the pages controller:
get '/contactus' => 'pages#contactus'
or if you want a simpler name for your path:
get '/contactus' => 'pages#contactus', as: :contact
I know it is a typical problem. But I was just trying the routing in rails and I get this error:
uninitialized constant UsersController
And I don't know well where is the problem.
The resume of my app is a simple application of a library, where users can book books and see the books they've booked.
Here is my routes.rb
Brickstest2::Application.routes.draw do
resources :books
root "pages#home"
get "home", to: "pages#home", as: "home"
get "inside", to: "pages#inside", as: "inside"
devise_for :users
namespace :admin do
root "base#index"
resources :users
end
resources :books do
get :book_a_book, :as => "reserve"
end
resources :users do
get :booked_books, :as => "reserved", :on => :member
end
end
Actually When I do rake routes I get:
reserved_user_path
GET /users/:id/booked_books(.:format) users#booked_books
And in users_controllers.rb I have:
def booked_books
#user = User.first(params[:user_id])
#return unless #user == current_user
#books = Books.where(:user = #user)
end
The link to booked_books:
<%= link_to "My books", user_reserved_path(:user_id => current_user.id)%>
And finally my users/booked_books.html.erb:
<h1>My books</h1>
<%= #books.each do |book|%>
<p><%= book.name %>
<% end %>
Thanks in advance.
Have you declared a controller for your User resource that subclasses ApplicationController?
# app/controllers/users_controller.rb
class UsersController < ApplicationController
Note that the precise naming convention for a plural resource route for the User model is UsersController (note the s). I'm guessing that you've named your controller class UserController, which won't work for your use-case.
UPDATE:
Since you've declared a controller namespace, you'll want to declare your controller class like so:
# app/controllers/admin/users_controller.rb
class Admin::UsersController < ApplicationController
This should resolve any uninitialized constant UsersController errors involving your namespace.
Currently I have a route that looks like this:
resources :posts
I want to override the 'show' action so that I can display a url like this:
posts/:id/:slug
I am currently able to do this by adding a custom match route:
resources :posts
match 'posts/:id/:slug' => 'posts#show'
However, when I use the link_to helper, it does not use my custom show route.
<%= link_to 'show', post %> # renders /posts/123
How can I define my show route so that I can still use the link_to helper?
Update: As you can read in the following answers, you can override the route to the 'show' action, but it's probably more work than it's worth. It's easier to just create a custom route:
# config/routes.rb
match 'posts/:id/:slug' => 'posts#show', as: 'post_seo'
# app/views/posts/index.html.erb
<%= link_to post.title, post_seo_path(post.id, post.slug) %>
You have two routes which point to posts#show (you should be able to confirm this by running rake routes), and your link is using the wrong one.
When you call link_to('show', post) the URL of the link is generated by calling url_for(post) which (eventually, after passing through several other methods on the way) calls post_path(post). Since the route to posts#show that was created by your call to resources(:posts) is named post, that is the route that post_path generates.
You also currently have inconsistent routes for the show, update and destroy actions which will probably cause you problems later on.
You can fix this by changing your routes to the following:
resources :posts, :except => ['show', 'update', 'destroy']
get 'posts/:id/:slug' => 'posts#show', :as => 'post'
put 'posts/:id/:slug' => 'posts#update'
delete 'posts/:id/:slug' => 'posts#destroy'
Unfortunately you still can't use link_to('show', post) just yet, because it relies on being able to use post.to_param as the single argument needed to build a path to a post. Your custom route requires two arguments, an id and a slug. So now your link code will need to look like this:
link_to 'show', post_path(post.id, post.slug)
You can get around that problem by defining your own post_path and post_url helpers in app/helpers/posts_helper.rb:
module PostsHelper
def post_path(post, options={})
post_url(post, options.merge(:only_path => true))
end
def post_url(post, options={})
url_for(options.merge(:controller => 'posts', :action => 'show',
:id => post.id, :slug => post.slug))
end
end
Which means we're finally able to use:
link_to 'show', post
If that all seems like too much work, a common alternative is to use URLs that look more like posts/:id-:slug, in which case you can stick with the standard RESTful routes and just override the to_param method in your Post class:
def to_param
"#{id}-#{slug}"
end
You'll also need to do a little bit of work splitting up params[:id] into an ID and a slug before you can look up the relevant instance in your show, edit, update and destroy controller actions.
resources :posts, except: :show do
get ":slug" => :show, as: "", on: :member
end
and define helper
def post_path post
"/posts/#{post.id}/#{post.slug}"
end
db/migrate/add_slug_to_articles.rb
add_column :articles, :slug, :string
add_index :articles, :slug
models/article.rb
class Article < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
def should_generate_new_friendly_id?
new_record?
end
end
Or...
class Article < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :history
end
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
https://github.com/norman/friendly_id
I have a has many through association.
Firms have many Users through Follows.
I want Users to be able to Follow Firms. - I am using Devise for the users.
I have the following action in my firms controller.
def follow
#firm.users << current_user
end
in my routes.rb
resources :firms do
post :follow, on: :member
end
and in my firms view
<%= link_to "Follow", follow_firm_path(#firm), method: :post %>
However when I keep getting the following Routing Error in the browser
No route matches {:action=>"follow", :controller=>"firms"}
Rake Routes confirms the following
follow_firm POST /firms/:id/follow(.:format) firms#follow
Any ideas what the problem may be?
Many thanks
Edit: Controller code
class FirmsController < ApplicationController
before_filter :authenticate_user!, :except => [:show, :index]
def index
#firm_names = Firm.all.map &:name
direction = params[:direction]
direction ||= "ASC"
#firms = Firm.order("name #{direction}")
respond_to do |format|
format.html # index.html.erb
format.js
end
end
def follow
#firm.users << current_user
end
I am using the follow action in a partial in the index view.
everything looks good and this should work perfectly. Except that I see a typo in the following line
<%= link_to "Follow", follow_firm_path(#firm), method: :post %>
after the :method there should an => not a : . this will make the link a get request not a post request, that might be the issue, try using a simple link and replace post will get in your routes.rb just to test if the issue is arising due to this.
you can also test route methods from the console
rails c
app.follow_firm_path(2)
I noticed you also have an error in your routes, there should be an => not a : after :on
resources :firms do
post :follow, :on => member
end
You should define methods like this...
resources :firms do
collection
post :follow, on: :member
end
end
I think if this method does not create anything its type should be get.
Try it
Given the following models:
class Blog < ActiveRecord::Base
has_many :posts
end
class SiteBlog < Blog
end
class ProjectBlog < Blog
end
class Post <ActiveRecord::Base
belongs_to :blog
end
And the following routes:
resources :blogs do
resources :posts
end
In say a form partial, the following will work fine if #blog is a Blog:
form_for [#blog, #post] ...
However, if #blog is a ProjectBlog or SiteBlog, it bombs since it will be looking for a URL helper such as project_blog_posts.
I guess something like this would solve this:
[:project_blogs, :site_blogs].each |blogs| do
resources blogs do
resources :posts
end
end
I'm wondering whether there's a way to use the routes for subclassed models (e.g. ProjectBlog) to use the routes of the parent model (Blog). The "as" option only deals with the last object passed like [#blog, #post] to form_for.
Update
As requested below, here are the routes:
resources :blogs, only: [:show] do
resources :posts, only: [:new, :create, :edit, :update]
end
blog_posts POST /blogs/:blog_id/posts(.:format) posts#create
new_blog_post GET /blogs/:blog_id/posts/new(.:format) posts#new
edit_blog_post GET /blogs/:blog_id/posts/:id/edit(.:format) posts#edit
blog_post PUT /blogs/:blog_id/posts/:id(.:format) posts#update
blog GET /blogs/:id(.:format) blogs#show
Update 2:
The tip from an answer below:
form_for [#blog, #post], url: blog_posts_path(#blog, #post) do |f|
This works for "new" actions only, for "edit" actions, I'd get - as expected - a bad URL:
params[:action] # => "edit"
blog_posts_path(#blog, #post) # => "/blogs/publikationsreihe-tafelrunde/posts.5"
So the "if" I mentioned would fix this:
form_for [#blog, #post], url: params[:action]=='new' ? blog_posts_path(#blog, #post) : blog_post_path(#blog, #post) do |f|
But this looks incredibly clumsy, there must be a better way.
Easily solvable by passing the resource url to the form:
<%= form_for [#blog, #post], :url => blog_posts_path(#blog, #post) do |f| %>
...
<%- end %>