2011-04-27 68 views
1

这是错误:为什么我会得到'没有路线匹配',对于存在的路线?

No route matches {:action=>"send_to_client", :controller=>"stages"}

它对应于这一行:

<%= link_to "<span class='icon send-to-client-icon' title='Send to Client' id='send-to-client'> </span>".html_safe, send_to_client_stage_path(@stage), :id => stage.id, :confirm => "This will send #{stage.name.capitalize} to #{stage.client.email}. Are you sure you are ready?" %> 

在这种_show_table.html.erb

<% 

if @upload != nil 
    stage = @upload.stage 
end 

%> 
<h1 class="panel-header">Images</h1> 

<% if stage == nil %> 
    <div class="images_menu"> 
     <%= link_to "<span class='icon send-to-client-icon' title='Send to Client' id='send-to-client'> </span>".html_safe, send_to_client_stage_path(@stage), :id => stage.id, :confirm => "This will send #{stage.name.capitalize} to #{stage.client.email}. Are you sure you are ready?" %>  
     <span class="icon compare-icon" data-url="<%= compare_stage_path(stage)%>" title="Compare Images" id="compare-images"> </span> 
    </div> 
<% end %> 

这里是我的routes.rb:

resources :stages do 
    member do 
     get :step 
     get :compare 
     get :send_to_client 
    end 
    end 

问题是,这部分_show_table.html.erb是在我的uploads模型的视图文件夹...而不是stages模型。

当我在stages模型中执行link_to时,它工作正常。一旦我将它带入uploads模型中,它会抛出该错误。

为什么会这样?

EDIT1:这里是stages控制器的send_to_client行动:

def send_to_client 
     stage = Stage.find(params[:id]) 
     ClientMailer.send_stage(stage).deliver 
     if ClientMailer.send_stage(stage).deliver 
      flash[:notice] = "Successfully sent to client." 
      redirect_to("/") 
     else 
      flash[:notice] = "There were problems, please try re-sending." 
      redirect_to("/") 
     end 
    end 
+0

运行'耙routes'看到所有的可用的路线你有。 – 2011-04-27 03:27:27

回答

4

Rails会引发的ActionController :: RoutingError。

您在混合@stagestage。如果您在控制器操作中没有定义@stage,则它将是nil并且错误会上升。在这种情况下,只需使用@upload.stage

像:

<% if @upload.stage %> 
    <%= link_to "...", send_to_client_stage_path(@upload.stage), :confirm => "..." %> 
<% end %> 

如果你想使用@stage,只是@stage = @upload.stage定义它的动作,并用它来代替@upload.stage

<% if @stage %> 
    <%= link_to "...", send_to_client_stage_path(@stage), :confirm => "..." %> 
<% end %> 
2

这也许应该是

send_to_client_stage_path(stage) 

,而不是

send_to_client_stage_path(@stage) 

它应该是 “除非” ,而不是“如果”在这里,对吗?

<% unless stage.nil? %> 

而且,不要忘记,你可以使用 “除非”,这是更好的,有时

if @upload != nil 
    stage = @upload.stage 
end 

- >如果你使用send_to_client_stage_path(nil)

stage = @upload.stage unless @upload.nil? 
+0

这些更改仍然不能帮助我。我仍然收到路由错误。 – marcamillion 2011-04-27 01:14:23

+0

路由错误是因为它是成员路由,因此需要一个id。如果舞台是零,它不会有一个ID。 – 2011-04-27 01:21:04

+0

@奥斯汀,那我该如何处理?现在,如果我将它设置为'unless stage.nil?'它不会执行,但是当我做'除非!stage.nil?'它会引发路由错误。基本上我希望它显示该链接,如果stage.id存在(即不是零)。如果它不存在,那么我不想显示链接。 – marcamillion 2011-04-27 01:30:34