add custom path to resources route (only for one action) - ruby-on-rails

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

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]

Rails how to make example.com/post/1 to example.com/blog/post/1

I have a blog with root
root 'posts#index'
And works best with example.com/ to example.com/posts
But what I want is something like this:
example.com/blog/posts/1.
I've tried creating blog Controller and add
resources :blog do
resources :posts
end
But this is making my routes to blog/:id/posts/:id
If you don't have the relationship between the post and the blog as you mentioned, rails gives you the freedom to declare routes as our own.
so, to make the route example.com/posts/1 to, example.com/blog/posts/1, just add a custom route at the last.
get '/blog/posts/:id', to: :show, controller: 'posts'
what this does is over rides the previous route and make this route final.
Now type rake routes and it will give the last route for you as,
GET /blog/posts/:id(.:format) posts#show
Now you can access using,
example.com/blog/posts/1
Reference for rails routing
Just to expand upon #Sravan answer. If you have multiple routes that will start with /blog/ you might want to check Rails guide on routing.
You can add something along the lines of
scope '/blog' do
resources :posts
resources :users
resources :images
end
Which will create corresponding routes under /blog/.
namespace :blog do
resources :posts
resources :users
resources :images
end
And your controller with namespace will look like this: Blog::PostsController

Rails 4: Nesting resource, change URI but not controller

I am trying to place my tasks into my dashboard/admin routes, for example rather than todos/, todos/new, etc. I would like dashboard/todos/, dashboard/todos/new etc. etc.
I have tried to do that here like so;
namespace :dashboard do
resources :todos
end
Though this also changes the controller, which I don't want - I only want to nest the todos inside of the dashboard controller
Can anyone point me in the right direction please?
You can use scope or path as described in the Rails Guides
scope '/dashboard' do
resources :todos
end
or
resources :todos, path: '/dashboard/todos'
This will generate the routes with the path /dashboard/todos which maps to todos_controller
If you want to route /admin/todos to TodosController you can use scopeinstead of namespace:
scope '/admin' do
resources :todos
end
You could check the rails documentations about this point : http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

make a custom url for create action with RESTful routing

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.

Named route in Rails 4

I am playing with Rails 4 in a test application. I have an arbitrary URL that isn't standard like a resources :foo type URL. Ideally the end result I'd like is to be able to go to:
/contests/:id/enter
In views, it would be great if I can then set a link using a named helper such as:
edit_contests_enter(:id)?
What would be the best way to define the route above so I can use the helper path with an arbitrary URL like the one above? It doesn't necessarily have to be edit_contests_enter(:id) but as long as the helper path leads to the URL as suggested above, that would be fantastic.
I assume that your contest is a resource, and when your visitor goes to /contests/:id/enter you want them to create an object user <=> contest. Let's call it participation.
Now participation is exactly like any other resource in your Rails app, so you'd have a routes.rb file looking like
resources :contests do
resources :participations
end
You don't want people to do anything other than create a participation, like edit or destroy them. And perhaps you want a nice URI like /contests/:id/enter. All you have to do is
resources :contests do
resources :participations, :only => [:new, :create]
get "enter" => "participations#new"
end
Doing such will give you a routes helper named contest_enter. In your participations#new form, you'll POST as usual to /contests/:id/participations.
If you have a resources block for :contests, you could just define a new "member" route on the ContestsController using:
resources :contests do
member do
get :enter
end
end
And that would automatically generate you a named member route, the name of which you could find by running rake routes.

Resources