rails, adding file in app/views - ruby-on-rails

I created a new file in app/views/students called courses.html.erb
Then I try to reference it at app/views/students/show.html.erb:
<%= link_to 'courses', courses_student_path(#student) %>
However I am getting
undefined method `courses_student_path' for #<#:0x1052d1648>
What step did I miss?

Note that you never link to views. It is always some action in some controller which in turn renders that view. In this case your action is courses in students controller and you need to create a route for it.
Assuming you already had :students resource defined in config/routes.rb:
resources :students do
get 'courses', :on => :member
end
This will give you urls like students/1/courses and route helpers courses_student_path and courses_student_url.
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

Related

Rails route pointing to wrong place

In attempts to make my application more clean I have decided to create an imports controller and a views folder along with that instead of creating an import.html.erb view and an import method in my users controller.
My goal is to make the url: http://10.0.0.7/accounts/1/users/import
However, as one might think, this is directing to the show page and is thinking that import is the users id. How can I create the route so that it does not think that the word import is actually the users id?
Parameters shown in error page: {"account_id"=>"1", "id"=>"import"}
In my routes file I have this route which takes care of the
resources :accounts do
resources :users do collection { post :import, :controller => "imports", :action => "users" } end
end
I have also tried this route.
resources :accounts do
post :import, controller: 'imports', action: 'users'
end
There's a few issues I see here.
First, #accounts is nil. This is probably because it's in a partial, and you need to pass in variables into the partial.
<%= render partial: 'admin_sidebar', accounts: #accounts %>
And then use the variable accounts in the partial. Also, why is users in the URL? Your basic routing should have done this just fine.
I'd suggest a route like:
post 'account_users/:id', to: 'imports#users', as 'account_users'
This says - if a post request to URL /account_users/# send to imports controller and use the users method.

Rails redirection to show when redirected to other action in same class

My Task is to submit a form to place_order action inside Checkout controller.
This is how I wrote form in my view file i.e
<%= form_for (#order), url: {action: "place_order"} do |f| %>
It does reach inside this method and as I save object i want to redirect to some other method in the same class. This method name is thank_you. My code looks like this inside place_order method
if #order.save
redirect_to :action => 'thank_you'
else
...
end
But it redirects to show method of this class. If I change redirect to other class, it redirects fine but on other action of same controller, it always redirects to show.
Here is how I defined my routes
resources :checkout
resources :photos
devise_for :users
resources :carts
post 'checkout/place_order'
match 'checkout/thank_you', to: 'checkout#thank_you', via: [:get]
I need some expert opinion on this. Please help.
Move your thank_you route above resources :checkout.
From Rails guides:
Rails routes are matched in the order they are specified, so if you
have a resources :photos above a get 'photos/poll' the show action's
route for the resources line will be matched before the get line. To
fix this, move the get line above the resources line so that it is
matched first.

Is there an easy way to route an entire controller to a site root?

I know how to route a page to a site root, and how to individually specify routes for every single page and action, but I feel like I am creating unnecessary work for myself at times by doing this. Is it possible to make it so that example.com/method always equates to some_controller#method?
Something like:
root "some_controller#index"
controller :some_controller do
get "/:method" => :method
end
instead of
root "some_controller#index"
controller :some_controller do
get "/" => :index, as: :index
get "/contact" => :contact, as: :contact
get "/photos" => :photos, as: :photos
...
end
or is that the best way?
You're pretty close already:
controller :some_controller do
get ":controller/:action"
end
:controller/:action will dynamically match the :action segment to a method of that name on the controller referenced by the symbol passed to the controller method.
I would be careful about security here. Take note on what methods your controller inherits from its parent class (probably ApplicationController), as any public methods will be accessible using this routing style.

Calling a custom action within a nested resource (Rails 3)

I'm trying to call a custom controller action shuffle for a resource that is nested within another resource. I can't seem to get the method call right.
routes.rb
resources :templates do
resources :items
end
match "/templates/:template_id/items/shuffle" => "items#shuffle"
I have a link in my items#index view:
<%= link_to 'Shuffle', shuffle_template_items_path(#template) %>
When I click on the link, I get the following error:
undefined method `shuffle_template_items_path' for #<#<Class:0x42577c8>:0x3e77578>
I have also tried <%= link_to 'Shuffle', template_items_shuffle_path(#template) %> and that did not work.
How do I correctly call this custom action?
You probably want this:
resources :templates do
resources :items do
get :shuffle, :on => :collection
end
end
If you want your custom action to have a name, you need to provide it:
match "/templates/:template_id/items/shuffle" => "items#shuffle", :as => :suffle_template_items
I think the best way to write shuffle is in collection as per the documentation of Rails Routes:
So it would looks like this:
resources :templates do
resources :items do
collection do
get :shuffle
end
end
end
when you try rake routes you will find shuffle_template_items GET /templates/:template_id/items/shuffle(.:format) items#shuffle.

Rails: restful resource routing with action_controller_path

I am putting a randomize def in my controller and would like to access it with a restful route. The route should be accessed with the following:
<%= link_to "Randomize", random_reader_path %>
But I cannot figure out how to get this route to appear in rake routes or configure it correctly in my routes.rb file.
The random method will do the same thing as index only provide a random page content #variable
Currently I have my reader Controller as
resources :reader
in my routes.rb
Add more RESTful actions!
resources :reader do
get 'random', on: :collection
end
The route will be random_readers_path, though.

Resources