没有模型的表单,添加到命名空间

时间:2012-04-19 23:31:39

标签: ruby-on-rails rails-routing

我最近在rails应用程序中添加了名称空间。我有一个会通过电子邮件发送给所有者的表单,但它似乎在这个过程中出现了问题(底部错误)。表单没有模型,只是开始发送电子邮件。

这是我的路线文件

scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
  namespace :admin do
    resources :email_owners do
      collection do
       get :email
       get :islas
       post :email
       post :islas
       end
      end
    end
  match "*path", to: "sites#not_found" # handles /en/fake/path/whatever
  end
root to: redirect("/#{I18n.default_locale}") # handles /
match '*path', to: redirect("/#{I18n.default_locale}/%{path}")
end

rake路线的输出CONTROLLER = admin / email_owners

email_admin_email_owners GET    /:locale/admin/email_owners/email(.:format)    {:locale=>/en|es/, :action=>"email", :controller=>"admin/email_owners"}
islas_admin_email_owners GET    /:locale/admin/email_owners/islas(.:format)    {:locale=>/en|es/, :action=>"islas", :controller=>"admin/email_owners"}
                     POST   /:locale/admin/email_owners/email(.:format)    {:locale=>/en|es/, :action=>"email", :controller=>"admin/email_owners"}
                     POST   /:locale/admin/email_owners/islas(.:format)    {:locale=>/en|es/, :action=>"islas", :controller=>"admin/email_owners"}
  admin_email_owners GET    /:locale/admin/email_owners(.:format)          {:locale=>/en|es/, :action=>"index", :controller=>"admin/email_owners"}
                     POST   /:locale/admin/email_owners(.:format)          {:locale=>/en|es/, :action=>"create", :controller=>"admin/email_owners"}
new_admin_email_owner GET    /:locale/admin/email_owners/new(.:format)      {:locale=>/en|es/, :action=>"new", :controller=>"admin/email_owners"}
edit_admin_email_owner GET    /:locale/admin/email_owners/:id/edit(.:format) {:locale=>/en|es/, :action=>"edit", :controller=>"admin/email_owners"}
   admin_email_owner GET    /:locale/admin/email_owners/:id(.:format)      {:locale=>/en|es/, :action=>"show", :controller=>"admin/email_owners"}
                     PUT    /:locale/admin/email_owners/:id(.:format)      {:locale=>/en|es/, :action=>"update", :controller=>"admin/email_owners"}
                     DELETE /:locale/admin/email_owners/:id(.:format)      {:locale=>/en|es/, :action=>"destroy", :controller=>"admin/email_owners"}

应用程序/控制器/管理/ email_owners.rb

class Admin::EmailOwnersController < Admin::BaseController
  def email
  owner_type = params[:owner_type]
  subject = params[:subject]
  message = params[:message]

    owners = User.owners
    owners.each do |owner|
      OwnerMailer.all_owners(owner, subject, message).deliver
    end 
      flash[:notice] = "Email has been sent to all Owners"
      redirect_to admin_sites_path
  end 
end

这是我的表格,问题出在哪里。

<%= form_tag [:admin, email_admin_email_owners_path] do %>
To:
  <%= radio_button_tag "owner_type", "All" %> All Owners |
  <%= radio_button_tag "owner_type", "FullTime" %> FullTime |
  <%= radio_button_tag "owner_type", "PartTime" %> PartTime<br />
  <%= text_field_tag "subject", "Subject" %><br />
  <%= text_area_tag "message", "Message" %><br />
  <%= submit_tag "Send Email" %>
<% end %>

每当我去路线/路径时 (HTTP://本地主机:3000 / EN /管理/ email_owners /电子邮件)

我收到错误

undefined method `admin_/en/admin/email_owners/email_path' for #<#<Class:0x789db30>:0x789ab78>

但我不确定为什么。有任何想法吗?我是在说错路吗?感谢

1 个答案:

答案 0 :(得分:0)

form_tag将url作为第一个参数,因此您只应指定要转到的路径:

<%= form_tag email_admin_email_owners_path do %>

Rails使用方括号作为快捷方式从字符串,符号或对象生成嵌套路径,因此[:admin, :email]将生成admin_email_path

当您使用[:admin, email_admin_email_owners_path]之类的方括号生成此网址时,发生了两件事:

  1. email_admin_email_owners_path返回'/ en / admin / email_owners / email'
  2. 在方括号内评估:admin然后为您提供admin_/en/admin/email_owners/email_path这是一种未经解释的方法