remote form_tag rails3

时间:2011-06-08 17:37:14

标签: ruby-on-rails forms

我有以下远程form_tag,其目标是POST以下参数:Promotion id和bgUploaderFields到csv_upload控制器的create动作,模仿csv_uploads / new action的行为,从另一个屏幕。

= form_tag csv_uploads_path(:method=> :post), :remote => true, :disable_with => 'Adding multiple...' do
  = text_field_tag "bgUploaderFieldName", "", :id => "bgUploaderField", :readonly => "true"
  = hidden_field :promotion_id, :value => @promotion.id unless @promotion.nil?
  = submit_tag 'Add multiple'

发布后,我提交的是空值:

INSERT INTO `csv_uploads` (`promotion_id`, `created_at`, `updated_at`, `bgUploaderField`) VALUES (NULL, '2011-06-08 17:32:15', '2011-06-08 17:32:15', NULL)

这是我的佣金路线:

$ rake routes
(in /Users/boris/projects/test)
           csv_uploads GET    /csv_uploads(.:format)                                     {:action=>"index", :controller=>"csv_uploads"}
                       POST   /csv_uploads(.:format)                                     {:action=>"create", :controller=>"csv_uploads"}

csv_uploads_controller.rb

def create
    @csv_upload = CsvUpload.new(params[:csv_upload])

    respond_to do |format|
      if @csv_upload.save
        format.html { redirect_to(@csv_upload, :notice => 'Csv upload was successfully created.') }
        format.xml  { render :xml => @csv_upload, :status => :created, :location => @csv_upload }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @csv_upload.errors, :status => :unprocessable_entity }
      end
    end
  end

  def new
    @csv_upload = CsvUpload.new
    @promotion = Promotion.find_by_sms_promo_id(params[:promo_id])

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @csv_upload }
    end   end

我做错了什么?我还应该提一下,parmas值实际上是在屏幕上填充的。

Started POST "/csv_uploads?method=post" for 127.0.0.1 at 2011-06-08 10:56:42 -0700
  Processing by CsvUploadsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"1zJCwY0sXb4TaTpO2d+MLox2CHk1sBpho/JR4oH18sw=", "bgUploaderFieldName"=>"http://upload.contextoptional.com/chag/assets/20110608172149.csv", "promotion_id"=>"{:value=>2}", "commit"=>"Add multiple", "method"=>"post"}
FbConfig:

  SQL (0.1ms)  BEGIN
  SQL (0.8ms)  describe `csv_uploads`
  AREL (0.2ms)  INSERT INTO `csv_uploads` (`promotion_id`, `created_at`, `updated_at`, `bgUploaderField`) VALUES (NULL, '2011-06-08 17:56:42', '2011-06-08 17:56:42', NULL)
  SQL (0.2ms)  COMMIT
  CsvUpload Load (0.2ms)  SELECT `csv_uploads`.* FROM `csv_uploads` ORDER BY csv_uploads.id DESC LIMIT 1

1 个答案:

答案 0 :(得分:1)

看看你的参数:

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"1zJCwY0sXb4TaTpO2d+MLox2CHk1sBpho/JR4oH18sw=", "bgUploaderFieldName"=>"http://upload.contextoptional.com/chag/assets/20110608172149.csv", "promotion_id"=>"{:value=>2}", "commit"=>"Add multiple", "method"=>"post"}

您没有名为csv_upload的密钥,但您正在使用它来设置#create操作中的属性

您应该使用form_for:

<%= form_for @csv_upload, :remote => true do |f| %>

  <%= f.text_field :bgUploaderFieldName %>

  ...

<% end %>

或...至少,您应该为标签指定正确的名称:

= form_tag csv_uploads_path(:method=> :post), :remote => true, :disable_with => 'Adding multiple...' do
  = text_field :csv_upload, "bgUploaderFieldName", "", :id => "bgUploaderField", :readonly => "true"
  = hidden_field :csv_upload, :promotion_id, :value => @promotion.id unless @promotion.nil?
  = submit_tag 'Add multiple'