Ruby Rails - structuring data for AJAX call to con

2020-03-30 03:39发布

问题:

I have need for a button on my site which can send information to the create action of a controller ("pagetimes"). It seems to be working, although it is not sending all the data I am specifying--probably having to do with my inability to structure the data vector. I have made POST requests available in my config/routes.rb file via post 'pagetimes/create'

In application.js:

function submitForm() {
  alert("checked the button - worked");
  $.ajax({
    type:'POST', 
    url: '/pagetimes/create', 
    data: { pagename: "whatever", start: 7, end: 21 } ,
  });
}

Where :pagename, :start and :end are columns in my data table (string, integer, integer) and are made accessible in the model, and are available in what shows in the manual entry "new" page for a new pagetime.

In my page view:

<button type="button" onclick="submitForm()">Send data</button>

Everything else is pretty standard. I can see in my database that the post is being submitted successfully, but the 3 data fields I am trying to populate are all NULL. Probably this has something to do with how I am structuring the data: {} field?

O/w, Rails 3 on Win7, not using anything else fancy that might have a bearing on this...

UPDATE 1: This is what the form source code looks like that I am trying to post into. Maybe it is the case that I am referring to the fields incorrectly?

<div class="form-inputs">
  <div class="control-group string optional pagetime_pagename"><label class="string optional control-label" for="pagetime_pagename">Pagename</label><div class="controls"><input class="string optional" id="pagetime_pagename" name="pagetime[pagename]" size="50" type="text" /></div></div>
  <div class="control-group integer optional pagetime_start"><label class="integer optional control-label" for="pagetime_start">Start</label><div class="controls"><input class="numeric integer optional" id="pagetime_start" name="pagetime[start]" step="1" type="number" /></div></div>
  <div class="control-group integer optional pagetime_end"><label class="integer optional control-label" for="pagetime_end">End</label><div class="controls"><input class="numeric integer optional" id="pagetime_end" name="pagetime[end]" step="1" type="number" /></div></div>
</div>

UPDATE 2: Here is the piece of my logs that contains the POST:

Started POST "/pagetimes/create" for 127.0.0.1 at 2014-01-02 12:45:48 -0500
Processing by PagetimesController#create as */*
  Parameters: {"end"=>"21", "pagename"=>"whatever", "start"=>"7"}
  [1m[35mUser Load (1.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  [1m[36m (0.0ms)[0m  [1mbegin transaction[0m
  [1m[35mSQL (15.0ms)[0m  INSERT INTO "pagetimes" ("created_at", "end", "pagename", "start", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?)  [["created_at", Thu, 02 Jan 2014 17:45:48 UTC +00:00], ["end", nil], ["pagename", nil], ["start", nil], ["updated_at", Thu, 02 Jan 2014 17:45:48 UTC +00:00], ["user_id", 1]]
  [1m[36m (7.0ms)[0m  [1mcommit transaction[0m
Redirected to http://localhost:3000/pagetimes/22
Completed 302 Found in 91ms (ActiveRecord: 24.0ms)

UPDATE 3: Controller action

def create
  @pagetime = Pagetime.new(params[:pagetime])
  @pagetime.user = current_user

  respond_to do |format|
    if @pagetime.save
      format.html { redirect_to @pagetime, notice: 'Pagetime was successfully created.' }
      format.json { render json: @pagetime, status: :created, location: @pagetime }
    else
      format.html { render action: "new" }
      format.json { render json: @pagetime.errors, status: :unprocessable_entity }
    end
  end
end

回答1:

 function submitForm() {
      alert("checked the button - worked");
      $.ajax({
           type:'POST', 
           url: '/pagetimes/create', 
           data: $.param({ pagetime: {pagename: "whatever", start: 7, end: 21 }})
      });
 }


回答2:

$.post( "/pagetimes/create", { pagename: "whatever", start: "7", end: "21"  })
.done(function( data ) {
  alert( "Data Loaded: " + data );
});