How to change default path/url in routes in Rails 4 - ruby-on-rails

I'm working on a simple reservation application. Here are my routes
resources :users do
get 'reservations', on: :member
end
resources :listings do
resources :reservations
end
When I try to make a reservation, action reservations#new takes me to reservations_path . Of course I'm getting error as this path doesn't exist. I'd like action new to take me to listing_reservations_path instead. I was hopping it will be done automatically since resources :reservations is in nested resources. I read about routes and tried many things but can't find any working way of doing it. Is it possible?

You seem to be unclear on the nature of routes. The action reservations#new exists independently from any route. A route is just a way to map a URL path to a controller and action. If you are trying to do something like:
redirect_to controller: :resources, action: :new
You will have trouble, as all of your routes require some context. Instead, you need to provide whatever the URL helper you're using with a context:
redirect_to listing_reservations_path(#listing)
link_to "New Reservation", new_listing_reservation_path(#listing)
link_to "Reservation", [#listing, #reservation]

Related

"Create" route path helper

I find the resource route method quite convenient, but I totally hate that it does not create create and destroy path helpers.
I understand that writing
<% form_for(#object) %>
is supposed to automatically get the route name, and that we can play with arrays or symbols to automatically get the namespace/prefixes when they exist, but I have many routes with complicated scope definitions, and not being able to get create_xxx helpers totally annoys me
Is there no simpler solution than to write ? (I am trying to keep the default RESTful URLs while generating the helpers)
complicated_scope do
resources :my_resources, except: [:create, :destroy] do
post '', on: :collection, action: :create, as: 'create' # plus this generates a pluralized version, not very intuitive `create_complicated_scope_my_resourceS_path`
delete '', on: :member, action: :destroy, as: 'destroy'
end
end
EDIT. My example of 'somewhat complicated scope'
# Company access routes under /company/
namespace :company do
# I need a company id for all nested controllers (this is NOT a resource strictly speaking, and using resources :companies, only: [] with 'on: :collection' doesn't generate appropriate urls)
scope ':company_id' do
# Company administrators
namespace :admin do
# There is a lot of stuff they can do, not just administration
namespace :administration do
# There are several parameters grouped in different controllers
resources :some_administrations do
... # finally RESTful actions and others here
end
end
end
end
end
Resourceful routing does create create and destroy helpers, but they're implied by the type of HTTP request being made (POST and DELETE respectively) so the routing helper methods should work fine with the code you've provided.
Suppose you have the following route definition:
complicated_scope do
resources :my_resources
end
end
As a simple example, in the case of delete, you could use a named route like so:
link_to "Delete [resource]", complicated_scope_resource_path(id: #my_resource.id), method: :delete
Since the HTTP verb disambiguates the controller action this helper method routes to the destroy method of the controller.
Alternatively, you should be able to use the array syntax as well.
link_to "Delete [resource]", [:complicated_scope, #my_resource], method: :delete
The same goes for forms:
<%= form_for [:complicated_scope, #my_resource] do |f| %>
If #my_resource is a new object (not persisted), as in the case of a new action this would be equivalent to sending a post request to /complicated_scope/my_resource with the form params going in the body of the request.
Alternatively if #my_resource exists, as in the case of an edit action, the above would be equivalent to sending a PUT/PATCH which will route to the update action of your controller with /complicated_scope/my_resource/:id/update.

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.

Rails Routing: Remove the edit suffix to url

I am creating a website with a blog module. A blog post can either be a draft or published.
A published post can no longer be edited, and a draft cannot be viewed (only edit)
I currently have a resource defined as
resources :posts, :path => "blog" do
collection do
get 'drafts'
end
end
I can access the drafts list using blog/drafts, creating new ones posts using blog/new, and editing drafts through blog/:id/edit.
However, I'd like new drafts to be created using blog/drafts/new and edited using blog/drafts/:id
I need to define the new, create, edit and update routes to use this new scheme. The new and create routes seem quite simple. However I do not know how to handle the edit route in order to remove the action name part.
Also, while looking at the default routes definition, I found in actionpack-3.2.9/lib/action_dispatch/routing/mapper.rb the following :
member do
get :edit if parent_resource.actions.include?(:edit)
get :show if parent_resource.actions.include?(:show)
[...]
end
I do not understand how rails differentiates the :edit and the :show routes, and map the urls accordingly.
Thanks
You can use the following routes. Keep in mind that it requires different file hierarchy, rake routes should be your friend in this.
namespace :blog do
resources :drafts, :controller => :posts, only: [:new, :edit]
resources :posts, only: :show
end

Trying to create a method in rails controller

I'm still trying to get the hang of rails and i'm trying to create a simple app with a form where i can enter the data and then submit it and it will be stored in the db. I got this very simple by starting a new project and then running:
$ rails generate scaffold RSVP firstName:string lastName:string
Now i want to redirect to a thank you page after adding a new record via the form.
I've manually added the method below to my rsvps_controller.rb file:
# GET /rsvps/thank_you
def thank_you
respond_to do |format|
format.html # thank_you.html.erb
format.json { render json: #rsvps }
end
end
This line is in my routes file:
resources :rsvps
My question is, when i run rake routes, i don't see a route for my thank_you method. Shouldn't my :rsvps resource pick up my thank_you route and how does the routes know which controller method are which http calls(get, post, put, etc)?
In order to get a route that will hit that action in your controller you should have in your routes.rb file something like:
resources :rsvps do
member { get :thank_you }
end
or
resources :rsvps do
collection { get :thank_you }
end
it depends for if you want to access the resource you've just created or not.
You can take a look # http://guides.rubyonrails.org/routing.html it should help you understating the routing mechanism better.
Adding on to what wlad said, the resources :rsvps things in your routes.rb file creates a set of default routes that are going to be needed by most models. Things like index, new, create, or show. The reason your thank_you action isn't showing up in rake routes is because thank_you isn't one of the actions that were so common that they needed to be included out of the box without extra code.
If you are going to need to load a rsvp model on the thank you page to display any data in that model then you will need to use a member route. The :id in the route will be there because this is a resources member route and has to be associated with a particular resource. There has to be something in the url to know what to load.
resources :rsvps do
member { get :thank_you } #GET /rsvps/:id/thank_you(.:format)
end
If you just want a generic route that points to that controller action then you can use something like this:
match "/rsvps/thank_you" => "rsvps#thank_you", as: "rsvp_thank_you"
You can add more actions to any controller but rails will not treat this functions as actions unless you specify them in routes file. It will be treated as just another function in controller class created by user.
so if you want to add the thank_you function as action you need to add this to routes file.
There are multiple ways of doing so as others have explained in their answers.
adding-more-restful-actions
Using member and collection inside resources.
Use member when you want the function to be used only with the some model object.
eg: preview for photo id 1
GET /photos/:id/preview
In our example you member if you want such route and functionality.
GET /rsvps/:id/thank_you
Note :id in params is needed when you specify it as a member action.
resources :rsvps do
member do
get :thank_you #GET /rsvps/:id/thank_you(.:format)
end
end
Use collection if you want to call the action directly like
GET /rsvps/thank_you(.:format)
in resources
resources :rsvps do
collection do
get :thank_you #GET /rsvps/thank_you(.:format)
end
end
You need to specify the type of action (GET|POST) while adding it to routes.
In our example thank_you has been specified as a GET action. You can choose either.
You can create you own preety_urls or non restful routes using match.
(This will also require to have the action defined in resources block).
check out more on match here http://guides.rubyonrails.org/routing.html#non-resourceful-routes
I suggest you to go through this awesome documentation created by the rails team.(once more ;) ) http://guides.rubyonrails.org/routing.html
Cheers.
As you have said you just want to show a thank you page for each rsvp so a member route should be used. like this
resources :rsvps do
member get :thank_you
end
You should use collection of thank_you when you want to show all or some specific collection of thank_you.
when you include this and run rake routes again you will see the new http action there.

Why isn't my 'join' action working, it says the action 'show' can't be found

In my UserController I have:
def join
end
I have a join.html.erb in my /views/user/ folder.
My routes has a :
resources :user
When I go to:
http://localhost:3000/user/join
I get:
The action 'show' could not be found for UserController
Re: why isn't the join action found?
To answer your specific question, what's happening is that you want to have an action "join" for your User model.
Your problem is that you haven't defined a route matching the url http://localhost:3000/user/join
The line resources :user in your routes file only defines routes for the seven standard rest verbs/actions:
index, new, create, show, edit, update, destroy
See: http://apidock.com/rails/ActionController/Resources/resources
Added: to fix, you'll need to add an explicit or generic route. Routing docs
Added: Re: why am I seeing the error message re show? To be ultra-precise, the route selector "GET /usr/:id" (created by your resource call) is being used to select the SHOW action for the User resource. The :id value is being set to "join". Since you don't have a Show method defined in your controller, that's the error that you're seeing.
You're using resources, but have a non-REST action, so you need to add the join action to the route with the appropriate HTTP verb:
map.resources :users, :member => { :join => :get }
Place:
def show
end
in your UserController.
To be certain:
app/controllers/users_controller.rb
def join
end
app/views/users/join.html.erb
config/routes.rb
resources :users

Resources