I am learning Ruby on Rails and i did this scaffolding command:
rails g scaffold Alex
and it ran and created all the resources for me. So I tried to make a link from my index page to the alex page like this:
<%= link_to "Alex Link", alex_path(@alex) %>
(I am still not sure what that @alex part is, but it was in the other examples so I tried to have that there)
and in my routes.rb this code was created:
resources :alexes
get "home/index"
and when I tried to load the link, it gave me this error:
No route matches {:action=>"show", :controller=>"alexes"}
Just in case, here is the output from rake routes:
alexes GET /alexes(.:format) alexes#index
POST /alexes(.:format) alexes#create
new_alex GET /alexes/new(.:format) alexes#new
edit_alex GET /alexes/:id/edit(.:format) alexes#edit
alex GET /alexes/:id(.:format) alexes#show
PUT /alexes/:id(.:format) alexes#update
DELETE /alexes/:id(.:format) alexes#destroy
home_index GET /home/index(.:format) home#index
root / home#index
test POST /test(.:format) tests#create
new_test GET /test/new(.:format) tests#new
edit_test GET /test/edit(.:format) tests#edit
GET /test(.:format) tests#show
PUT /test(.:format) tests#update
DELETE /test(.:format) tests#destroy
What is wrong with the way that I made the link and how can I make it hit the controller before it goes to the view?
Thanks!
alex_pathis for showing a specificAlexobject. To use it,@alexneeds to be an instance of anAlexobject loaded by your controller.You say you want to link to the “Alex page”, which makes me think you want the listing of all
Alexobjects, or theindexaction of yourAlexController. If this is the case, you should usealexes_pathinstead ofalex_path(@alex).If you actually do want to link to a single specific
Alex, you’ll need to load an instance from the database:As an aside, you also ask:
Your controller will always be hit before the views are rendered. It is impossible for a view to be rendered without an action being invoked.