我有一个控制器“manage_links.rb”,允许用户管理他们的链接,这些链接具有相应的“链接”模型。我收到以下错误:
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
以下是我在运行“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)
这是导致错误的完整代码块:
<% @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 :(得分:1)
尝试直接传递link-object:
f.e。
edit_manage_link_path(link)