对于存在的路线,为什么我会得到“无路线匹配”?

时间:2011-04-27 00:43:02

标签: ruby-on-rails ruby-on-rails-3

这是错误:

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模型。

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

为什么会出现这种情况?

Edit1:以下是send_to_client控制器的stages操作:

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

2 个答案:

答案 0 :(得分:4)

如果你使用send_to_client_stage_path(nil)

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

答案 1 :(得分:2)

应该是

send_to_client_stage_path(stage)

而不是

send_to_client_stage_path(@stage)

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

<% unless stage.nil? %>

另外,不要忘记你可以使用“除非”,它有时更好

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

- &GT;

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