开发者

routing problem

开发者 https://www.devze.com 2023-02-11 21:53 出处:网络
i have two identically controllers routed in the same way : resources :profile resources :friends here are the controllers

i have two identically controllers routed in the same way :

resources :profile
resources :friends

here are the controllers

class ProfileController < ApplicationController



def index
@text = "profile"
end
def show
end
def new 
end
def create
end
def edit
end
def update
end
def destroy
end

end





class FriendsController < ApplicationController


def index
@text = "friends"
end
def show

end
def new 
end
def create
end
def edit
end
def update
end
def destroy
end

end

but when i want to define a menu in the view layout , a problem occurs for the profile controller but not also for the friends controller . Here is the code that generates the error :

<ul id="menu">

<li>
<%= link_to "Friends",friend开发者_开发技巧s_path %>
</li>
<li>
<%= link_to "Profile", profile_path %>
</li>


</ul>

and the error is :

No route matches {:action=>"show", :controller=>"profile"}

Why is this happening if the controllers and views are identical ?


It's related to the fact that you've given your Profile controller a singular name and route. You can run rake routes to find out what the route helpers are named. Look for GET /profile, it might be something like index_profile_path or profile_index_path

Edit: more specifically, the error is because it's expecting by default for profile_path to be the helper for showing a certain instance, e.g. profile_path(@profile)


try using

<%= link_to "Profile", profiles_path %>

The difference is in the variable name profile s _path. As the path to the index url should be in plural.

You can also see all the routes if your application by running rake routes - it's a live saver to debug routes problems.

0

精彩评论

暂无评论...
验证码 换一张
取 消