开发者

remote form_tag rails3

I have the following remote form_tag, whose goal is to POST the following params: Promotion id and bgUploaderFields to the create action of the csv_upload controller, mimicking the behavior of csv_uploads/new action, from another screen.

= 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'

After I post, I am submitting null values:

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)

Here is my rake routes:

$ 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

What have I done wrong? I should also mention that the parmas values are in fact populated on screen.

Started POST "/csv_uploads?method=post" for 127.0.0.1 at 2011-06-08 10:56:42 -0700
  P开发者_高级运维rocessing 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


Look at your params:

  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"}

You don't have a key named :csv_upload but that's what you're using to set the attributes in your #create action

You should be using form_for:

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

  <%= f.text_field :bgUploaderFieldName %>

  ...

<% end %>

or... at the very least, you should give the tags their proper name:

= 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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜