表单未提交自定义操作

时间:2012-11-06 15:11:53

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2

我仍然是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

问题是当我提交它时使用'create'动作而不是'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"}

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

非常感谢!

1 个答案:

答案 0 :(得分: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 doc。

如果有效,建议命名为:

的routes.rb

resources :events do
  collection { post :share }
end

因此,您可以将其称为share_events_path

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