I'm setting up my website such that
www.website.com/articles/2013/07 will list all articles from 2013 in July and
www.website.com/articles/2013 will list all articles in 2013
www.website.com/articles/2013/07/title-of-article will list just the specific article
However, I am able to get only the first and last of the above 3 to work. Entering the url
www.website.com/articles/2013 is not working properly. Specifically, I get a noMethodError in Articles#show which doesn't make sense to me because I have matched the route to Articles#index.
Can someone please explain what's going on here? I don't see where I am making a mistake.
Routes:
match "/articles/:year", :to => "articles#index",
:constraints => { :year => /\d{4}/ }, :as=> :posts_year
match "/articles/:year/:month", :to => "articles#index",
:constraints => { :year => /\d{4}/, :month => /\d{1,2}/ }, :as => :posts_month
match "/articles/:year/:month/:slug", :to => "articles#show",
:constraints => { :year => /\d{4}/, :month => /\d{1,2}/, :slug => /[a-z0-9\-]+/ }, :as => :posts_date
and in my model I have:
def year
created_at.year
end
def month
created_at.strftime("%m")
end
Do you have resources :articles in your routes file? If you do, that explains the error. Move resources :articles below the routes you mentioned in your question and everything should work fine. Alternatively you could change it to:
resources :articles, :except => :show
Guessing that if you've defined articles as a resource, then the default show/:id route is taking precedence over your :posts_year route. Maybe try resources :articles, :except => [:show]
Related
I need to figure out how to properly use routes to create a url structure like so:
items/page/2
items/expired/page/2
I have items/page/2 working and then I have this which I want to to correct:
items/expired?page=2
I am using Kaminari to provide pretty url structure for rails 4.2 with a concern.
https://github.com/amatsuda/kaminari/#creating-friendly-urls-and-caching
My controller has two actions: index and expired
My views under items are index.html.haml and expired.html.haml
routes.rb
concern :paginatable do
get '(page/:page)', :action => :index, :on => :collection, :as => ''
end
concern :expired_paginatable do
get '(page/:page)', :action => :expired, :on => :collection, :as => ''
end
get 'items/expired', to: "items#expired", :concerns => :expired_paginatable
resources :items, :concerns => :paginatable
my views both have:
= paginate #items
I know I do not need two concerns but thought I would try it.
I ended up changing my resources block to this:
resources :items do
collection do
get 'expired/page/:page', :action => :expired
get :expired
end
concerns :paginatable
end
dropping:
concern :expired_paginatable do
get '(page/:page)', :action => :expired, :on => :collection, :as => ''
end
get 'items/expired', to: "items#expired", :concerns => :expired_paginatable
resources :items, :concerns => :paginatable
I keep getting strange errors because of the way my routes.rb file is organized. The latest one is that some function cannot find action "show" in model Relations controller (the action is obviously there). I guess this is because I am adding some custom actions via collection and something about the order in which the routes are declared is messed up.. Can somebody please have a look at this and say what is wrong?
YApp::Application.routes.draw do
require 'resque/server'
match 'login' => 'user_sessions#new', :as => :login
match 'logout' => 'user_sessions#destroy', :as => :logout
match '/get_idx', :to => 'nodes#get_idx'
resource :relations do
collection do
post 'this_relation'
post "iframize"
end
end
resource :words do
get 'page/:page', :action => :index, :on => :collection
collection do
get 'front'
get 'index'
end
end
resource :recommendations do
collection do
get 'find_votes'
end
end
get "connotation/create"
get "connotation/edit"
get "connotation/update"
root :to => "words#front", :as => :homepage
resources :users, :user_sessions, :relations, :evaluation, :phrases, :metawords, :nodes, :recommendations, :words
mount Resque::Server.new, :at => "/resque"
match 'about' => 'words#index' , :as => :about
match 'contact' => 'keywords#index' , :as => :contact
end
You might have an issue with resource :relations. Rule of thumb is: if you use the plural resources, then the name of the resource must also be plural (i.e. :relations), if you use resource, in singular, than you should use singular for the resource name too (i.e. :relation).
Other possible problems: your indentation is off. Maybe it's just a copy-paste issue, but check it nonetheless, because you might have some unexpected nesting going on.
Also inspect rake routes CONTROLLER=relations. Compare that to the log of the failed request and see if every parameter matches up.
I'm working on a project and trying to render some images sitting under the public directory in one of my show views. I'm able to access these images just fine on my index view, but it's not quite so easy for show views it would seem.
So lets say in my view I have something like the following:
<%= "<img src=\"images/16x16/actions/filesave.png\" class=\"icon" title=\"save\">"%>
This would work in my index view perfectly fine, but for some strange reason I get this routing error in my show view:
ActionController::RoutingError (No route matches [GET]
"/uploads/images/16x16/actions/filesave.png"):
I noticed that for some peculiar reason it's injecting the "/uploads/" route right before the "/images..." this is the cause of my problem and I can't figure out why or how to stop it. This only happens with my show views.
Now there's a lot going on in my routes.rb file, I know it's ugly but I do plan on going in there and cleaning it up when I get the chance.
resources :upload_images
get "upload_image/new"
get "upload_image/index"
get "upload_image/show"
get "upload_image/delete"
resources :help_categories
resources :global_configs
resources :competitions
match '/teams/register', :controller => 'teams', :action => 'register'
match '/teams/invite_users', :controller => 'teams', :action => 'invite_users'
match '/teams/view_invitations', :controller => 'teams', :action => 'view_invitations'
match '/teams/ignore', :controller => 'teams', :action => 'ignore'
match '/teams/leave_team', :controller => 'teams', :action => 'leave_team'
resources :teams
resources :competitions do
resources :matches
end
resources :registers
resources :players do
collection do
post :edit_individual
put :update_individual
get :results
end
end
resources :tournaments
resources :matches
resources :upload_categories
resources :uploads, :except => [:new]
match '/download/:id' => 'uploads#download'
devise_for :users do
match 'logout' => 'devise/sessions#destroy'
end
resources :users, :except => [:new] do
member do
get 'upload_files'
get 'delete_files'
end
end
resources :games
devise_for :videolinks
resources :topics do
collection do
get "mark_all_viewed"
end
member do
get 'show_new'
end
end
resources :posts do
member do
get 'quote'
get 'topic'
end
end
resources :forums do
member do
get 'confirm_delete'
end
end
resources :blog_entries, :except => [:index]
resources :categories
resources :videolinks
resources :competition_games
resources :competitions
resources :news
resources :events
match 'uploads/find_uploads' => 'uploads#find_uploads'
match 'uploads/add_upload_image' => 'uploads#add_upload_image'
match 'forum_root' => 'forums#index'
match 'upload_root' => 'uploads#index'
match 'user' => 'forums#index'
match 'news_root' => 'news#index'
match 'topic_post' => 'forums#index'
match 'quote_post' => 'forums#index'
match 'new_upload' => 'forums#index'
match 'videolinks/:id', :to => 'videolinks#show'
match 'register' => 'users#sign_up'
match 'login' => 'users#sign_in'
match 'users/find_users' => 'users#find_users'
match '/users/get_states/:country' => 'users#states'
match '/ban/:username' => 'users#ban'
match '/ban_user/:username' => 'users#ban_user'
match ':username' => 'users#show'
match ':username/edit' => 'users#edit'
match ':username/delete_files_all' => 'uploads#index'
match ':username/delete_files' => 'users#delete_files'
match ':username/upload_files' => 'users#upload_files'
match ':username/password/edit' => 'users#editpass'
match ':username/edit_file/:id' => 'uploads#edit'
match '/maketakeadmin/:username' => 'users#maketakeadmin'
match ':username/destroy' => 'users#destroy'
root :to => "home#index"
resources :categories do
member do
get 'confirm_delete'
end
end
Another developer worked on the upload section of this application and it uses paperclip. By default it saves uploads in the public directory and we didn't want that so he told me he did some weird hotfix to save uploads to a private directory off of the root of the application called "uploads". Not sure if this might have anything to do with it.
Needed a forward slash at the start of the path.
I think you should use something like this:
<%= image_tag "/images/16x16/actions/filesave.png", class: "icon", alt: "save" %>
I just recently started programming in Ruby on Rails, and I was wondering if some of you could look over my routes.rb file that I am using so far and tell me if I am over thinking this.
I am aware of the whole RESTful approach in RoR and I am trying to stick to it, but I am not sure if I am on track. So far my application only has the following functionality:
User registration
User activation (via email link)
User can request activation to be resent
User log in
User log out
User requests password reset (gets an email)
Basic UCP (change email and password)
I am using a lot of redirect_to *_url and *_path, so I want a lot of named routes. I am trying to explicitly declare only routes that are allowed. Thanks for your input.
MyApp::Application.routes.draw do
get 'home' => 'pages#index', :as => 'home'
get 'testing' => 'pages#testing', :as => 'testing'
get 'register' => 'users#new', :as => 'register'
post 'users/create'
resources :users, :only => [
:new,
:create
]
get 'activation' => 'activations#new', :as => 'activation'
get 'activate/:token' => 'activations#activate', :as => 'activate'
post 'activations/edit'
resources :activations, :only => [
:new,
:activate,
:edit
]
get 'login' => 'sessions#new', :as => 'login'
get 'logout' => 'sessions#destroy', :as => 'logout'
get 'sessions/destroy'
resources :sessions, :only => [
:new,
:create,
:destroy
]
get 'forgot_password' => 'resets#new', :as => 'forgot_password'
post 'resets/create'
get 'activate_password/:token' => 'resets#activate', :as => 'activate_password'
put 'save_password' => 'resets#save', :as => 'save_password'
resources :resets, :only => [
:new,
:create,
:activate,
:save
]
get 'ucp' => 'ucp#show', :as => 'ucp'
post 'ucp_update' => 'ucp#update', :as => 'ucp_update'
resources :ucp, :only => [
:show,
:update
]
root :to => 'pages#index'
end
When you use resources, it automatically makes named routes for you. I won't go through your entire routes file, but one example:
get 'activation' => 'activations#new', :as => 'activation'
get 'activate/:token' => 'activations#activate', :as => 'activate'
post 'activations/edit'
resources :activations, :only => [
:new,
:activate,
:edit
]
Could be:
resources :activations, :only => [:new, :edit] do
get 'activate', :on => :member
end
which will produce new_activation_path, edit_activation_path, and activate_activation_path
Go to the Rails Routing Guide for a lot of cool stuff you can do in routes. For example, if you want to use "register" instead of "new" for your Users paths:
resources :users, :only => [:new, :create], :path_names => [:new => 'register']
Pretty simple question, but I can't seem to figure out how to do this, even after scouring the Rails Routing guide.
Assume Topics has nested resource Posts.
The Posts for a Topic are all listed in Posts#index (/topics/:topic_id/messages). Topics#show does not serve any purpose. I would like Posts#index to be retrieved when the request is for /topics/:topic_id, without having to stick a redirect in the Topics controller.
Thank you!
UPDATE
I was able to get the desired result with this:
routes.rb
match 'forums/:forum_id' => 'topics#index', :as => 'forum_topics', :via => :get
match 'topics/:topic_id' => 'messages#index', :as => 'topic_messages', :via => :get
resources :forums, :shallow => true, :except => :show do
resources :topics, :shallow => true, :except => :show do
resources :messages
end
end
However, I'm not sure if this is the best method.
UPDATE 2
My method above breaks the other CRUD methods (like #create). Still looking for a solution to keep /messages out of the url.
add this route
get "/topics/:topic_id" => redirect("/topics/%{topic_id}/messages")
UPD
without redirecting:
get "/topics/:id" => "topic::Messages#index"
Or, if you're using shallow:
get "/topics/:id" => "messages#index"
resources :forums, :shallow => true, :except => :show do
resources :topics, :shallow => true, :except => :show do
resources :messages
end
end