Rails nested dynamic page based on ID - ruby-on-rails

Basic question — I'm trying to create a new view that would appear at a URL like this:
http://localhost:3000/students/4/profile
Currently I have show.html.erb which would go here:
http://localhost:3000/students/4
What do I need to put in my routes.rb (which is currently what's below) to allow me to create a custom page like that?
resources :students

From the Routing Section of the Rails docs: you can use the member method within a resource block to define a member route/action
resources :students do
member do
get :profile
end
end
This would define a students/:id/profile route that would map to a profile method on your students controller.

Related

How to set child index path without specific parent id in Rails?

I have route.rb like this:
resources :companies do
resources :company_jobs
end
And rails routes:
company_company_jobs GET /companies/:company_id/company_jobs(.:format) company_jobs#index
I need to add a link to show all company_jobs model without specific company_id like this:
Any one how can I config route to do this? Thank you so much!
What your routing implies is that you want all company_jobs that belong to company identified by :id. Assuming that you have a model association set up between the two models, this would be referenced as <%= link_to company_company_jobs(#company) %> - rails will fill in the id for you.
If instead, you just want to include all jobs no matter which company, you could change your routes to:
resources :companies do
collection do
get :company_jobs
end
resources :company_jobs
end
This will create a new route companies_company_jobs_path company#company_jobs
For this route to work, you will need to add the following to companies_controller.rb
def company_jobs
end

ROR: Routes issue - Survey gem

The Survey gem is not creating the routes on my Rails app so I am wondering what to put in the routes.rb file?
I ran
rails generate survey:install
then ran
rails generate survey routes namespace:survey
and does not work.
I'm using Rails 4.2.1
The controllers are at controllers/survey/attempts_controller.rb and controllers/survey/surveys_controller.rb
The views are at survey/attempts/ and survey/surveys/
How should I put in the routes for these? Thanks.
Survey uses a standard CRUD interface, so you should be able to add resources named after the survey model you've created.
i.e. resources :surveys
If this doesn't do the trick, you can see a basic routing setup in the survey demo app here: https://github.com/runtimerevolution/survey-demo/blob/master/config/routes.rb
Let me know if this helps!
Its okay if it didn't add routes to your routes.rb file. You can add it yourself. And since you have it namespaced within Survey for your controllers you have to namespace in your routes file too.
namespace :survey do
resources :surveys
resources :attempts
get 'survey_details' => 'surveys#get_details' #this request will be handled by get_details method of SurveysController
end
Since it uses basic Create Read Update Delete in its controller, resources :surveys will take care of these. For any additional routes you want to define for your controllers make sure to put them inside the namespace block.

Passing on Rails model to child controller

I have the following url /purchases/3/payments/new, which I get in the purchases controller by
link_to 'pay', purchase_path(purchase)+new_payment_path
Now in the payments controller I would need to have the purchase object, or its id at least from where it was invoked.
I tried using a param, but is there any other way ?
Thanks
Using params makes sense.
You should be able to get the purchase ID like this in the payments controller:
params[:purchase_id]
However, need to setup your routes in a specific way to do this:
resources :purchases do
resources :payments
end
Then you can create the link in the view like this:
link_to 'pay', new_purchase_payment_path(purchase)
Have a look at these docs too: http://guides.rubyonrails.org/routing.html#nested-resources
Routes
I immediately highlighted this as a problem:
purchase_path(purchase)+new_payment_path
This is really bad (configuration over convention) - you'll be much better using the actual path helper to load the resource you need (keeps it DRY & conventional)
--
Nested
As mentioned by Jon M, your solution will come from the use of nested resources:
#config/routes.rb
resources :purchases do
resources :payments # -> domain.com/purchases/:purchase_id/payments/new
end
This will allow you to use the route path as described by Jon.
--
Controller
in the payments controller I would need to have the purchase object,
or its id
By using nested resources, as described above, the route will hit your payments controller and have the following param available:
params[:purchase_id]
Note the naming of the param (only the nested resource is identified with params[:id]), as demonstrated in the docs: /magazines/:magazine_id/ads/:id
I would recommend using the following code in your controller:
#app/controllers/payments_controller.rb
class PaymentsController < ApplicationController
def new
#purchase = Purchase.find params[:purchase_id]
end
end

ruby on rails routing helper: how to create select_clients_path(client)

I have client standard resource with CRUD, but I would like to make extension with action select, so that I can have select_clients_path(client).
In clients_controller I have created action select, but I dont know how to create correct routing rule
for now I have created:
resources :clients do
get 'select'
end
but this generates /clients/select.2 but i would like somthing like /clients/select/2 or /clients/select?id=2
thank you
Dorijan
resources :clients do
collection do
get :select
end
end
will create a 'clients/select' route, to which you can pass parameters like '?client_ids=2...' and work with several client records.
alternatively,
resources :clients do
member do
get :select
end
end
will create a 'clients/:id/select' route to work with a single client record
Take a look at http://guides.rubyonrails.org/routing.html#adding-more-restful-actions for more about this functionality, but those blocks will get you pretty far.

Rails routes, how to specify this member proper

I currently have some routes that look like the following:
resources :contests do
member do
get :enter
end
end
This allows me to do an HTTP GET on a URL that looks like /contests/5/enter. Now, a user can go in, fill in some forms, and be able to submit an entry to the contest. So I'd also like to be able to POST to this URL. I tried doing the following:
resources :contests do
member do
get :enter
post :enter
end
end
This posts to the same controller#action as the GET member that I have specified, so it's not really intuitive. I'd like to be able to direct it to a separate action if at all possible. What's the best way of doing that? I am using Ruby on Rails 4 beta currently.
** UPDATE **
I tried the following but I get an ArgumentError exception when I start the server:
resources :contests do
member do
get :enter
post :enter => "contests#create_entry"
end
end
You can do something like this:
resources :contests do
member do
get :enter
post '/enter', to: "contests#create_entry", as: "create_entry"
end
end
However i agree with Ola Tuvesson, you should definitely create a new controller and routes for entries, even though you may not have a model, similiar to how you often have a session controller for login and logout. Something like this:
resources :contests do
resources :entries, only: [:new, :create]
end
You can specify the controller/action you want to point a route at.
get :enter => "controller#get_enter"
post :enter => "controller#post_enter"
I would suggest you make the entries for a contest a child model of contests. That would give you all the CRUD methods on entries and your routes would simply be:
resources :contests do
resources :entries
end
A GET of /contests/5/entries/new will give you the form for adding an entry and when this POSTs to /contests/5/entries it would create a new entry. It also makes it easy to list all entries for a competition etc. You can easily create the controller, model and the associated views with the scaffold generator, for example:
rails g scaffold Entry contest:references name:string email:string
The references column type tells the generator to link Contests to Entries in a one to many relationship. Job done.
Edit:
If you still want to rename your routes, here's how:
http://guides.rubyonrails.org/routing.html#overriding-the-named-helpers
HTH

Resources