title: Ruby Rails - structuring data for AJAX call to controller action
body: <p>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 <code>post 'pagetimes/create'</code></p>
  
  <p>In application.js:</p>
  
  <pre><code>function submitForm() {
    alert("checked the button - worked");
    $.ajax({
      type:'POST', 
      url: '/pagetimes/create', 
      data: { pagename: "whatever", start: 7, end: 21 } ,
    });
  }
  </code></pre>
  
  <p>Where <code>:pagename</code>, <code>:start</code> and <code>:end</code> 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. </p>
  
  <p>In my page view:</p>
  
  <pre><code>&lt;button type="button" onclick="submitForm()"&gt;Send data&lt;/button&gt;
  </code></pre>
  
  <p>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 <code>data: {}</code> field? </p>
  
  <p>O/w, Rails 3 on Win7, not using anything else fancy that might have a bearing on this...</p>
  
  <p>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?</p>
  
  <pre><code>&lt;div class="form-inputs"&gt;
    &lt;div class="control-group string optional pagetime_pagename"&gt;&lt;label class="string optional control-label" for="pagetime_pagename"&gt;Pagename&lt;/label&gt;&lt;div class="controls"&gt;&lt;input class="string optional" id="pagetime_pagename" name="pagetime[pagename]" size="50" type="text" /&gt;&lt;/div&gt;&lt;/div&gt;
    &lt;div class="control-group integer optional pagetime_start"&gt;&lt;label class="integer optional control-label" for="pagetime_start"&gt;Start&lt;/label&gt;&lt;div class="controls"&gt;&lt;input class="numeric integer optional" id="pagetime_start" name="pagetime[start]" step="1" type="number" /&gt;&lt;/div&gt;&lt;/div&gt;
    &lt;div class="control-group integer optional pagetime_end"&gt;&lt;label class="integer optional control-label" for="pagetime_end"&gt;End&lt;/label&gt;&lt;div class="controls"&gt;&lt;input class="numeric integer optional" id="pagetime_end" name="pagetime[end]" step="1" type="number" /&gt;&lt;/div&gt;&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>UPDATE 2: Here is the piece of my logs that contains the POST:                               </p>
  
  <pre><code>Started POST "/pagetimes/create" for 127.0.0.1 at 2014-01-02 12:45:48 -0500
  Processing by PagetimesController#create as */*
    Parameters: {"end"=&gt;"21", "pagename"=&gt;"whatever", "start"=&gt;"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)
  </code></pre>
  
  <p>UPDATE 3: Controller action</p>
  
  <pre><code>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
  </code></pre>