2014-09-25 103 views
4

我正在尝试将一个链接显示到由rails动作邮件呈现的视图中。ActionMailer link_to show action failed with“missing required keys:[:id]”

mailer.rb

class Mailer < ActionMailer::Base 
    default from: "[email protected]" 

    def catalog_download_request(email, catalog) 
    @catalog = catalog 

    mail({ 
     to: email 
    }) 
    end 

end 

的routes.rb

Rails.application.routes.draw do 

    scope "(:locale)" do 
    resources :catalogs, :path => I18n.t("routes.catalogs"), only: [:index, :show] 
    end 

end 

development.rb:

config.action_mailer.default_url_options = { host: "http://localhost:3000" } 
config.action_mailer.asset_host = "http://localhost:3000" 

config.action_mailer.delivery_method = :smtp 

config.action_mailer.smtp_settings = { 
    address: "localhost", 
    port: 1025 
} 

config.action_mailer.raise_delivery_errors = false 

我的模型,其中我打电话邮件:

class CatalogDownloadRequest < ActiveRecord::Base 
    belongs_to :catalog 

    after_create :send_mail 

    private 

    def send_mail 
    Mailer.catalog_download_request(email, catalog).deliver 
    end 

end 

这就是我在我看来试过:

<%= link_to @catalog %> 

错误:

ActionView::Template::Error: No route matches {:action=>"show", :controller=>"catalogs", :format=>nil, :id=>nil, :locale=>#} missing required keys: [:id]

另一个尝试:

<%= link_to catalog_url(@catalog) %> 

错误:

ActionView::Template::Error: No route matches {:action=>"index"}

我怀疑这是发生因为我的航线范围区域。

如果我在另一个视图中使用<%= link_to catalog_url(@catalog) %>,它可以工作。

解决了:

<%= link_to catalog_url(:id => @catalog.id), catalog_url(:id => @catalog.id) %> 
+0

对我来说同样的错误。 即使尝试最简单的示例'<%= recording_url(@recording)%>'或'<%= recording_path(@recording)%>' 这是非常基本的Rails应用程序,没有任何特别的。 – Schovi 2014-10-08 13:17:38

+0

我认为你需要做和我一样的工作:'recording_url(:id => @ recording.id)' – 2014-11-06 12:42:34

回答

6

原因ActionView::Template::Error: No route matches {:action=>"index"}是:

link_to需要两个参数(文字和网址)(api)。你只通过一个。在将文本作为第一个参数传递后,它将正常工作。

如果你真的想,根据文件,你传递零作为第一个参数,然后网址作为第二个。

+0

link_to只用一个参数就可以很好地工作,它以url作为文本呈现链接。 – 2014-11-05 13:43:02

+0

对于我的回答有问题,我很抱歉,但您的回答对我来说还不够清楚,因为在其他视图中,代码可行,而在电子邮件视图中则不行。所以,link_to与单个参数一起工作,而不是在那个特定的情况下。我很欣赏你试图帮助我,尽管你的回答并不是100%正确的,但我已经解决了这个问题。 – 2014-11-06 12:40:16

+0

使它工作的真正技巧是在这部分代码中:'catalog_url(:id => @ catalog.id)' – 2014-11-06 12:43:24

相关问题