views:

335

answers:

1

I have a controller "manage_links.rb" that allows users to manage their links, which have a corresponding "link" model. I am getting the following error:

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 %>

I've got the following line in my config/routes.rb:

map.resources :manage_links

Here's an excerpt of what I get when I run "rake routes":

          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)

and here's the full code block that's causing the error:

<% @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 %>

here is the controller code:

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

why is the call to "edit_manage_link_path(link.id) giving me an error? I know that link.id actually returns a number, and if I hard code a value for the parameter instead of link.id, the function seems to run correctly

+1  A: 

Try to pass the link-object directly instead:

f.e.

edit_manage_link_path(link)
Lichtamberg