2012-03-12 115 views
-3

當我打開http://localhost:3000/users我得到以下路由錯誤:錯誤路由

No route matches {:action=>"req", :controller=>"friendship", :id=>"[email protected]"} 

如何解決這個問題?

內部的用戶/ index.html.erb:

<ul> 
    <% @users.each do |user| %> 
    <li> 
    <%= user.full_name %> 
    (<%= link_to "request friendship", 
       :controller => :friendships, 
       :action => :req, 
       :id => user.email %>) 
    </li> 
    <% end %> 
</ul> 

FriendshipsController

def req 
    @user = User.logged_in(session) 
    @friend = User.find_by_email(params[:id]) 
    unless @friend.nil? 
     if Friendship.request(@user, @friend) 
     flash[:notice] = "Friendship with #{@friend.full_name} requested" 
     else 
     flash[:notice] = "Friendship with #{@friend.full_name} cannot be requested" 
     end 
    end 

    redirect_to :controller => :users, :action => :index 
    end 

內的routes.rb:

resources :friendships 
+0

我想創建友誼model.when我打開http:// localhost:3000 /用戶它應該顯示所有註冊用戶和請求友誼鏈接在他們面前。點擊請求友誼它應該去友誼控制器請求action.but在點擊http:// localhost:3000/users – Nikita 2012-03-12 07:50:45

+0

時,我在用戶的index.html.erb文件上出現錯誤「feedbacks」資源有什麼意義?你正在使用非RESTful動作('friendships#req'),你的routes.rb應該定義一些路徑來處理你對friendhsips控制器的請求:'get'friendships /:id'=>'friendships#req' '。 – Jef 2012-03-12 08:06:00

+0

@ jef「反饋」資源誤寫了它..資源:友誼。 – Nikita 2012-03-12 08:35:38

回答

0

也許你想要做的事:

resources :friendships do 
    member do 
    get :req 
    end 
end 
0

它想...... 這種設置方式,Rails會試圖獲取資源 /友情/ REQ/ID 在那裏將期望ID告訴它,如果你想第1,第3,第22或REQ。

但你已經給它 :id =>「[email protected]」 Rails正在尋找一個數字,並且你給它一個字符串。