Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
10
rated 0 times [  12] [ 2]  / answers: 1 / hits: 10726  / 11 Years ago, thu, january 2, 2014, 12:00:00

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

More From » jquery

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

[#49049] Thursday, January 2, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kalistaz

Total Points: 0
Total Questions: 100
Total Answers: 106

Location: Bermuda
Member since Thu, Apr 20, 2023
1 Year ago
;