make a custom url for create action with RESTful routing - ruby-on-rails

In Rails 4 I'm trying to make a custom root for a particular action in the users controller, so i want my restful resources for users still like they are, but change only the url for the create action and make it to be for example /account/register. I'm trying do this as the follow but it seem not work :
resources :users, except: [:create] # first i eliminate creation of create root
resources :users, path: "account/register", as: :users, only: [:create] # then i just try to make a custom route for only create action
i want still using users_path and not change any of my routing helper in the view, please any idea ?

Try:
match '/account/register' => 'user#create', via: :post

How about:
post "/account/register" => "users#create", as: :users
This will create a route where /account/register points to UsersController#create. This can be referenced with users_path.

Related

Rails url generation error on a form_for nested route and custom route

So I have two models a booker and a booking_ticket. I have nested the routes of booking_ticket under booker, but I also want to create a new booking_ticket without a booker, so I created a custom route. My action works properly when I try to create using the nested route but not on my custom path.
my routes.rb
resources :bookers do
resources :booking_tickets
end
get '/booking_tickets/new', to: 'booking_tickets#new', as: 'new_booking_ticket'
This is the error I'm getting from rails when I use the custom path:
I don't understand where the error is coming from.
You need to create a custom route for create action, your current custom resource is for new action:
post '/booking_tickets/create', to: 'booking_tickets#create'
Or, since you seem to be using defaults, just replace your current custom route with:
resources :booking_tickets, only: [:new, :create]
Be sure to add it outside resources :bookers block:
resources :bookers do
resources :booking_tickets
end
resources :booking_tickets, only: [:new, :create]

add custom path to resources route (only for one action)

I have below resource route
resources :listings
This will have the path set to locahost:3000/listings/new
But how can I customise path to new action(only) to show like
locahost:3000/listings/create_your_listing
or even(better if it can be customised like this)
locahost:3000/owners/create_your_listing
Only new action of the resources should be like that and rest as normal.. How can I achieve this?
First,
resources :listings, except: :new
This will create listings resources except new.
next,
get "/owners/create_your_listing", to: "listings#new"
This will create the missing new route for your listing controller.
EDIT:
This may be the way to achieve custom path:(not tested)
scope(path_names: { new: 'create_your_listing'}) do
resources :listing, path: 'listings'
end
Reference

Is the order of the routes in the routes.rb important?

My routes.rb file looks like:
resources :contents, only: [:show]
get 'contents/by_hardware', to: 'contents#show_by_hardware'
With this setup I am not able to access the contents/by_hardware route.
But if I setup my routes.rb file in a different order, everthing works.
get 'contents/by_hardware', to: 'contents#show_by_hardware'
resources :contents, only: [:show]
Is the order in the routes.rb file important?
Yes, order matters very much.
It works like this: resources :contents, only: [:show] creates this route
content GET /contents/:id(.:format) contents#show
So when you request, for example, http://localhost:3000/contents/by_hardware, it is this route that matches this url. It invokes ContentsController#show action with params {'id' => "by_hardware"}. Your custom action is not considered, because matching route is already found.
Yes, order does matter. Instead of defining routes for the same controller at two different places, I would recommend you to define routes for the above scenario this way
resources :contents, only: [:show] do
get :show_by_hardware, on: :collection, path: :by_hardware
end
Hope that helps!
Yes it is important, the routes will be matched from top to bottom so you can move your route get 'contents/by_hardware', to: 'contents#show_by_hardware' above resource to fix your problem
yes. router will match first route from the top

Getting 'Unknown action in controller'

I'm getting this error :
"The action 'create' could not be found for ObjectController"
I know it should be obvious but I'm missing something, that's my controller :
class ObjectController < ApplicationController
def index
end
def create
end
end
And that is my routes :
Rails.application.routes.draw do
get 'object/index'
get 'object/create'
match ':controller(/:action(/:id))', :via => :get
resources :objets
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'object#index'
You probably want to scrap those routes and try something simpler like
resources :objects, only: [:get, :create, :show]
Then use
$ rake routes
To make sure your routes are as the should be. You will want a POST route to /objects to create a new object etc..
Ok that one was dumb, actually I had two directories and I wasn't modifying the right one, sorry about that...
Your routes could be greatly improved:
#config/routes.rb
Rails.application.routes.draw do
root 'objects#index'
resources :objects
--
Next, the "standard" way to achieve what you're looking for is to use the new action; IE not the "create" action. If you wanted to use the create path name (instead of new), you'll be able to define it in the path_names argument:
#config/routes
resources :objects, path_names: { new: "create", create: "create" } #-> url.com/objects/create
To understand why you should be using new instead of create, you should look up resourceful routing, and how it pertains to object orientated programming.
Finally, your controller should be named in the plural:
#app/controllers/objects_controller.rb
class ObjectsController < ApplicationController
...
end
Whilst you can call it whatever you like, Rails defaults to plural controller names, singular model names.

Rails routes change URL with ressources

I have the following in my routes:
resources :collection_pages, :only => [:show, :index]
Right now its going to wwww.mysite.com/collection_pages for index
and collection_pages/:id for show
I need it to go to /collections instead for index
and /collection/:id for show
I tried path: :collections , but its not behaving as I'd like.
What's the best way to achieve this using ressources?
Along with the SO answer #Tamer.Shlash notes, you could use custom matchers in your routes file:
match '/collection', to: 'collection_pages#index', as: 'collection_index'
match '/collection/:id', to: 'collection_pages#show', as: 'collection_show'
Adding the as: 'xxxx' to the match allows you to use friendly path and url helpers in your views:
redirect_to collection_index_path

Resources