2009-09-02 108 views
1

我有一個控制器「manage_links.rb」,允許用戶管理他們的鏈接,它們有相應的「鏈接」模型。我收到以下錯誤:Rails路由錯誤無法識別ID

ActionController::RoutingError in Manage_links#index 

Showing app/views/manage_links/index.html.erb where line #16 raised: 

edit_manage_link_url failed to generate from {:controller=>"manage_links", :action=>"edit", :id=>nil}, expected: {:controller=>"manage_links", :action=>"edit"}, diff: {:id=>nil} 
Extracted source (around line #16): 

13:  <td><%=h link.text %></td> 
14:  <td><%=h link.url %></td> 
15:  <td><%= link_to 'Show', manage_link_path(link.id) %></td> 
16:  <td><%= link_to 'Edit', edit_manage_link_path(link.id) %></td> 
17:  <td><%= link_to 'Destroy', manage_link_path(link.id), :confirm => 'Are you sure?', :method => :delete %></td> 
18: </tr> 
19: <% end %> 

我已經得到了下面一行在我的config/routes.rb文件:

map.resources :manage_links

這裏是我所得到的摘錄當我運行「耙路線」 :

  manage_links GET /manage_links(.:format) 
         POST /manage_links(.:format) 
     new_manage_link GET /manage_links/new(.:format) 
     edit_manage_link GET /manage_links/:id/edit(.:format) 
      manage_link GET /manage_links/:id(.:format) 
         PUT /manage_links/:id(.:format) 
         DELETE /manage_links/:id(.:format) 

,這裏是這是造成錯誤的完整代碼塊:

<% @links.each do |link| %> <tr> 
    <td><%=h link.text %></td> 
    <td><%=h link.url %></td> <td><%= link_to 'Show', manage_link_path(link.id) %></td> 
    <td><%= link_to 'Edit', edit_manage_link_path(link.id) %></td> <td><%= link_to 'Destroy', manage_link_path(link.id), :confirm => 'Are you sure?', :method => :delete %></td> </tr> 
<% end %> 

這裏是控制器代碼:

class ManageLinksController < ApplicationController 
before_filter :login_required  before_filter :find_user 

    # GET /links 
    # GET /links.xml  def index 
    @links = @user.links 

    respond_to do |format| 
     format.html # index.html.erb 
     format.xml { render :xml => @links }  end 
    end 
    # GET /links/1  # GET /links/1.xml  def show 
    @link = @user.links.find(params[:id]) 
    respond_to do |format| 
     format.html # show.html.erb 
     format.xml { render :xml => @link } 
    end  end 

    # GET /links/new 
    # GET /links/new.xml  def new 
    @link = @user.links.build 

    respond_to do |format| 
     format.html # new.html.erb   format.xml { render :xml => @link } 
    end 
    end 

    # GET /links/1/edit 
    def edit 
    @link = @user.links.find(params[:id])  end 

    # POST /links 
    # POST /links.xml 
    def create 

    @link = @user.links.build(params[:id]) 

    respond_to do |format| 
     if @link.save 
     flash[:notice] = 'Link was successfully created.' 
     format.html { redirect_to manage_link_url(@link) } 
     format.xml { render :xml => @link, :status => :created, :location => @link } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @link.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /links/1 
    # PUT /links/1.xml 
    def update 
    @link = Link.find(params[:id]) 

    respond_to do |format| 
     if @link.update_attributes(params[:link]) 
     flash[:notice] = 'Link was successfully updated.' 
     format.html { redirect_to manage_link_url(@link) } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @link.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /links/1 
    # DELETE /links/1.xml 
    def destroy 
    @link = Link.find(params[:id]) 
    @link.destroy 

    respond_to do |format| 
     format.html { redirect_to manage_link_path } 
     format.xml { head :ok } 
    end 
    end 

    def find_user 
    @user = session[:user] 
    end 
end 

爲什麼是調用「edit_manage_link_path(link.id)給我的錯誤?我知道,其實link.id返回一個數字,如果我硬編碼的參數,而不是link.id一個值,該功能似乎運行正常

+0

如果您發佈控制器代碼會很有幫助 – ennuikiller 2009-09-02 14:59:11

回答

1

嘗試通過鏈接對象,而不是直接:

fe。

fe

edit_manage_link_path(link)