2011-04-27 66 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