0

我对 Rails 还比较陌生,在这里查看了许多类似的问题,但到目前为止没有一个对我有帮助。所以...我有一个表格,我想发送一封简单的电子邮件。它以 Bootstrap 模式弹出,如下所示:

.form-horizontal
  = form_tag :url => { :action => :share_event }, remote: true, html: {"data-type" => :json} do
    .control-group
      = label_tag :to
      = text_field_tag :to
    .control-group
      = label_tag :from
      = text_field_tag :from
    .control-group
      = label_tag :subject
      = text_field_tag :subject  
    - hidden_field_tag :event_url, :value => url
    .actions
      = submit_tag 'share event', :class => "btn btn-mini submit"

在我的控制器中:

def share_event
  @to = params[:to]
  @from = params[:from]
  @subject = params[:subject]
  @url = params[:url]
  respond_to do |format|
    EventMailer.share_event(@to, @from, @subject, @url).deliver
    redirect_to events_path, notice: 'Event sent'
  end 
end

  EventMailer.share_event(@to, @from, @subject, @url).deliver
end

路线:

resources :events do
  post :share_event, :on => :collection
end

问题是当我提交它时使用“创建”操作而不是“share_event”。我尝试了很多方法,但它继续使用“创建”。以下是日志的摘录:

Started POST "/events?html%5Bdata-type%5D=json&remote=true&url=share_events_path" for 41.134.48.225 at 2012-11-06 15:34:25 +0100
Processing by EventsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7/6pg9XX/N09qUB+SUlWOdpCsgPOAiXM+nC+mq6VtKE=", "to"=>"to@example.com", "from"=>"from@example.com", "subject"=>"gfh", "event_url"=>"{:value=>\"http://events.example.com/events/3\"}", "commit"=>"share event »", "html"=>{"data-type"=>"json"}, "remote"=>"true", "url"=>"share_events_path"}

任何帮助/建议将不胜感激。

非常感谢!

4

1 回答 1

1

我不能确定是什么问题,但猜想它在你的form_tag声明中。您可以尝试以下修改

在你的route.rb

resources :events do
  collection { post :share_event }
end

在你看来

代替:

= form_tag :url => { :action => :share_event }, remote: true, html: {"data-type" => :json} do

和:

= form_tag(share_event_events_path({format: 'json'}), remote: true) do

请检查form_tag api 文档。

如果可行,建议的命名为:

路线.rb

resources :events do
  collection { post :share }
end

因此,您可以将其share_events_path称为

= form_tag(share_events_path({format: 'json'}), remote: true) do
于 2012-11-06T15:21:29.963 回答