I don't know if I'm being nit-picky with this one but how do you create a :new action member?
When I have
resources :posts, :except => [:show] do
get :another_new, :on => :collection
end
this gives me a plural one (in routes another_new_posts GET /posts/another_new(.:format)).
But when I use member, I do get singular but it wants an :id (in routes another_new_post GET /posts/:id/another_new(.:format)).
How do I create another new which would say in routes as another_new_post with /posts/new?
Place this as a standalone piece outside of your resources block:
match 'posts/another_new' => "posts#new", as: :another_new_post
Related
I'm having trouble defining a Rails 'new' route for a model that takes a param to another model to which it will be linked. We have a legacy URL structure in place, so unfortunately
a nested resource route won't work here.
I'd like to define the "create a new review" URL as /reviews/new/1234, where 1234 is the book_id that the soon-to-be-created Review should reference.
My routes (snipped for brevity) are defined as:
get '/reviews/:book_id' => 'reviews#index', :as => 'reviews_path'
get '/reviews/new/:book_id', :to => 'reviews#new', :as => 'new_review_path'
post '/reviews/:book_id' => 'reviews#create'
get '/reviews/:book_id/:id' => 'reviews#show'
get '/reviews/:book_id/:id/edit' => 'reviews#edit', :as => 'edit_review_path'
delete '/reviews/:book_id/:id' => 'reviews#destroy'
rake routes | grep review returns:
reviews_path GET /reviews/:book_id(.:format) reviews#index
new_review_path GET /reviews/new/:book_id(.:format) reviews#new
POST /reviews/:book_id(.:format) reviews#create
GET /reviews/:book_id/:id(.:format) reviews#show
edit_review_path GET /reviews/:book_id/:id/edit(.:format) reviews#edit
DELETE /reviews/:book_id/:id(.:format) reviews#destroy
In my view template I have:
<%= link_to 'new review', new_review_path(book_id: #book.id) %>
which fails with:
ActionView::Template::Error (undefined method `new_review_path' for #<#<Class:0x007f818f7117c8>:0x007f818f70e208>):
For completeness, my Review model looks like:
class Review < ActiveRecord::Base
attr_accessible :book_id, :title, :content, :tags
belongs_to :book
end
Remove the _path from the end of the :as conditions on your routes. Right now it's looking for new_review_path_path.
Documentation is here for using as.
If in views/abouts/ I have "index.html.haml" and "history.html.haml".
How can I access to abouts#history which is a basic html page.
From log I get this error, I guess it is processing it as a show, what can I do?:
Processing by AboutsController#show as HTML
Parameters: {"id"=>"history"}
About Load (0.3ms) SELECT `abouts`.* FROM `abouts` WHERE (`abouts`.`id` = 0) LIMIT 1
ActiveRecord::RecordNotFound (Couldn't find About with ID=history):
routes.rb
scope() do
resources :abouts, :path => 'about-us' do
match 'about-us/history' => "about-us#history"
end
end
abouts_controller.rb
def history
respond_to do |format|
format.html
end
end
A few problems. First, you should be matching 'history' and not 'about-us/history' (the route is nested so the 'about-us/' part is automatically included). Second, you need to specify that the route should match the collection, not a member of the collection, with the :on => :collection option. Finally, you should be routing the match to 'abouts#history' and not 'about-us#history' (because the controller is named abouts regardless of what path string you use when routing).
So try this:
resources :abouts, :path => 'about-us' do
match 'history' => "abouts#history", :on => :collection
end
Also note that match will match all HTTP requests: POST as well as GET. I'd suggest using get rather than match, to narrow the HTTP request type to just GET requests:
resources :abouts, :path => 'about-us' do
get 'history' => "abouts#history", :on => :collection
end
Hope that helps.
I have a set of API routes in rails as follows
namespace "api" do
namespace "v1" do
resources :users do
resources :posts
resources :likes
...
end
end
end
So far, so good. I can GET /api/v1/users/fred_flintstone and retrieve all of the information for that user.
What I would like to do now is add the concept of "me" (ala facebook) such that if the user is authenticated (fred_flintstone), I can also do the following
GET /api/v1/me
GET /api/v1/me/posts
...
I require both sets of routes. So I want to achieve the same results either using GET /api/v1/me/posts OR GET /api/v1/users/fred_flintstone/posts.
I've been through the route tutorial and have googled so a pointer would be as much appreciated as a direct answer.
EDIT:
What I've done that has worked is pretty hacky. I've created a second set of entries in the routes table using a scope:
scope "/api/v1/me", :defaults => {:format => 'json'}, :as => 'me' do
resources :posts, :controller => 'api/v1/users/posts'
resources :likes, :controller => 'api/v1/users/likes'
...
end
And then I added a set_user method that tests for the presence of params[:user_id]. I'm really looking for a way to DRY this up.
What about leaving the routes the way they are in your post, and just solving this inside the controller?
Heres a before_filter that you could apply to all of the routes you have which pull a User from a :user_id.
# Set the #user variable from the current url;
# Either by looking up params[:user_id] or
# by assigning current_user if params[:user_id] = 'me'
def user_from_user_id
if params[:user_id] == 'me' && current_user
#user = current_user
else
#user = User.find_by_user_id params[:user_id]
end
raise ActiveRecord::RecordNotFound unless #user
end
Then in your controller functions you can just use the #user variable without having to worry about whether the user passed a user_id, or me.
Hope that helps! :)
EDIT:
Lemme take another shot, given your comments.
How about a function that lists all the resources you wish to access via both the standard routes and the /me route. Then you can just use the function in both the namespaces you require.
routes.rb
# Resources for users, and for "/me/resource"
def user_resources
resources :posts
resources :likes
...
end
namespace 'api' do
namespace 'v1' do
resources :users do
user_resources
end
end
end
scope '/api/v1/:user_id', :constraints => { :user_id => 'me' },
:defaults => {:format => 'json'}, :as => 'me' do
user_resources
end
# We're still missing the plain "/me" route, for getting
# and updating, so hand code those in
match '/api/v1/:id' => 'users#show', :via => :get,
:constraints => { :id => 'me' }
match '/api/v1/:id' => 'users#update', :via => :put,
:constraints => { :id => 'me' }
I want to add more methods to my rest.
Here is my routes.rb file:
resources :boards, :except => [:new, :create] do
get 'customize', :on => :member
get 'change_template', :on => :member
get 'all_walls', :on => :member
end
I am getting them in the following format:
change_template_board GET /boards/:id/change_template(.:format) {:action=>"change_template", :controller=>"boards"}
But I want them in this format:
/boards/:board_id/change_template/:id(.:format)
How can I do that?
I'm copying the first answer from this question. In your routes.rb, you can add a new route dooit to resource fifi by adding this to your routes.rb file:
resources :fifi do
member do
get :dooit
end
end
This will create the route dooit_fifi along with the standard fifi, fifi_index, new_fifi, and edit_fifi routes.
If you want to restrict the routes created, you can do something like this:
resources :fifi, only: [:show, :create, :destroy] do
member do
get :dooit
end
end
which will produce only the routes dooit_fifi, fifi, and fifi_index.
I am porting a Merb app to Rails 3. In Merb we could put an Identify block around a route to define how an :id route parameter was to be supplied, e.g.,
# this is a Merb route that I want to port to Rails 3 routing; I get everything except
# how to replicate the behavior of Merb's Identify block which doesn't require one to
# futz with overriding to_param on user; a user instance gets passed to the url builder
# ala url(:edit_password_reset, user) and this tells the router to use the
# reset_password_token method on user to supply the :id value for this one route
Identify User => :reset_password_token do
match("/reset-password/:id", :method => :get).to(:controller => "password_resets", :action => "edit").name(:edit_password_reset)
end
# and then later define more routes that use the user's id without a problem
# since to_param was not overridden on user; here I have already translated to
# Rails 3 and this works fine
controller :users do
get "/register", :action => "new", :as => "new_user"
get "/users", :action => "index", :as => "users"
get "/users/:id", :action => "show", :as => "show_user"
get "/users/:id/edit", :action => "edit", :as => "edit_user"
put "/users/:id", :action => "update", :as => "update_user"
post "/users", :action => "create", :as => "create_user"
end
In Rails, as in Merb, you can override to_param to provide an alternative id value for routes, but for a case where one time you want to use an id and another time you want to use a different method on the same object (as above), Identify is convenient. What is the Rails 3 equivalent? I looked through the Rails 3 source and tests and didn't see anything equivalent to Identify. Did I miss it?
I can refactor things and maybe should to not need it in this case, but still I would like to know if I missed something.
Thanks.
I came across the same problem; turns out the best way is to skip to_param entirely when calling a url or path. For instance:
# This will set params[:id] to #user.to_param
edit_password_reset_url(#user)
# This will set params[:id] to #user.reset_password_token
edit_password_reset_url(#user.reset_password_token)
In other words, to_param is only called when passing a record to the url helpers; if you pass it a string instead, it will just parse the string.