ruby-on-rails - 对于存在的路由,为什么我会得到 'no route matches' ?

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

这是错误:

没有路由匹配 {: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模型的 View 文件夹中......而不是stages型号。

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

为什么会这样?

Edit1:这是 stages Controller 的 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

最佳答案

如果您使用send_to_client_stage_path(nil),Rails 将引发 ActionController::RoutingError。

您混淆了@stagestage。如果您没有在 Controller 操作中定义 @stage ,则它将是 nil 并引发错误。在这种情况下,只需使用@upload.stage

喜欢:

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

如果您想使用@stage,只需在操作中使用@stage = @upload.stage 定义它,然后使用它来代替@upload。阶段:

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

关于ruby-on-rails - 对于存在的路由,为什么我会得到 'no route matches' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5798090/

相关文章:

ruby-on-rails - 存储桶策略和签名 URL 之间是否存在冲突?

css - Twitter BootStrap Scrollspy Bug 仍然存在......?

ruby-on-rails-3 - 在我的应用程序中查找所有 mongoid 模型名称

ruby-on-rails - Rails后台进程/数据结构

ruby-on-rails - 没有路由与发布请求中的 [GET] 匹配

Javascript 未从 remotipart 的提交后执行

css - 我的 .scss 不在我的 ruby​​ 应用程序中

ruby-on-rails - 如何在 Rails 中优化这段代码?

ruby-on-rails - ELK stack + Filebeat 收集Rails日志

ruby-on-rails - RSpec 错误 "undefined method ` respond_with'..."