【问题标题】:Rails 3.1 javascript file usageRails 3.1 javascript文件使用
【发布时间】:2011-12-28 06:53:32
【问题描述】:

我有一个 Rails 3.1 应用程序,我可以在其中创建邮件群发。我有一个我所有的爆炸列表,其中包含一个邮件链接,用于邮寄特定的爆炸(使用 ajax)。我的爆炸控制器中有一个我想使用的自定义操作。当我单击它想要调用 show.js.coffee 而不是 mail.js.coffee 的链接时。我如何强制它使用我创建的动作。当我把所有东西都放在我的控制器的 show 动作和 show.js.coffee 但不是 mail 动作和 mail.js.coffee 等时,一切正常。

这是我的爆炸部分

<td><% if can? :create, Blast%><%= link_to "Mail",blast,:action => :mail  :confirm => "Are you sure?",:remote => true%><%end%></td>

这是控制器中的方法

def mail
  @customers = Customer.all
  @mailcustomers = Customer.where(:opted_out => false)
  @blast = Blast.find(params[:id])
end

这是在我的 mail.js.coffee 文件中

 <% @mailcustomers.each do |f|%>
   <% BlastMailer.mail_blast(f,@blast).deliver %>
 <%end%>

在 routes.rb 中

resources :blasts do
  collection {  post :mail  }
end

【问题讨论】:

    标签: jquery ruby-on-rails-3.1 coffeescript


    【解决方案1】:

    我知道我需要做什么。这是行之有效的路线。

    <td>
      <% if can? :create, Blast%>
        <%= link_to "Mail", {:action => :mail, :id=>blast.id}, :method => :post,:confirm => "Are you sure?",:remote => true%>
      <%end%>
    </td>
    

    【讨论】:

      【解决方案2】:

      (编辑: 在我原来的回答中,我拒绝在 .js.coffee 文件中使用 ERB。但经过进一步调查,事实证明 Rails 3.1 在特殊情况下允许这样做JS 正在响应 :remote =&gt; true 表单操作。)

      我认为你的问题是

      :action => :mail  :confirm => "Are you sure?"
      

      尝试在:mail 后面加一个逗号。

      【讨论】:

      • Rails 解释 js.coffee 文件中的 ruby​​ 代码。不再需要 erb 扩展。我想出了我需要做什么。这是行之有效的路线。 :mail, :id=>blast.id}, :method => :post,:confirm => "你确定吗?",:远程 => true%>
      • 如您所见,我使用上述评论中的行修复了它
      猜你喜欢
      • 1970-01-01
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      相关资源
      最近更新 更多