Sunday, 18 August 2013

Rails4: Link to A Controller

Rails4: Link to A Controller

I have a User's Show Page where
1.) Routes
get 'users/:id' => 'user#show', as: :user
2.) user_controller.rb
class UserController < ApplicationController
before_filter :authenticate_user!, only: :show
def show
@user = User.find_by_name(params[:id]) # for name instead of id
@listings = @user.listings
end
end
and i can link to it via "current_user".
I wanted to create a Shop Controller, so i followed the same steps. I
Generated a Shops Controller and modified the routes and controller as
follow:
1.) Routes
get 'users/:id' => 'user#show', as: :user
get 'shop/:id' => 'shop#show', as: :shop
2.) shop_controller.rb
class ShopController < ApplicationController
before_filter :authenticate_user!, only: :show
def show
@user = User.find_by_name(params[:id]) # for name instead of id
@listings = @user.listings
end
end
This Works only if im at the User's Page (localhost:3000/users/test) and
then click the link to the controller. then it switches to
(localhost:3000/shop/test).
If i try to click the link anywhere else im getting

The link is ->
<li><%= link_to "My Shop", :controller => "shop", :action => "show" %></li>
I'm fairly new to Rails, if someone could enlighten me it would be very
nice :)

No comments:

Post a Comment