简体   繁体   中英

How to pass the params from JS to rails3 controller

Here is my JS.coffee codes:

fetchselect =(val) ->
 $.ajax(url: '/firstpages/page', dataType: 'json', par_id: val )

$('.homeNav').find('.unactive').click ->
 id = $(this).attr('id')
 fetchselect(id)

and Here is my controller codes:

def page
@select = Firstpage.where(:pid=>params[:par_id])

 respond_to do |format|
   format.html # page.html.erb
   format.js { render :layout => false }
   format.json { render :json => @select }
end

end It can't pass the params to @select ,when I click $('.homeNav') ,the log tell me:

Started GET "/firstpages/page" for 127.0.0.1 at 2012-09-07 05:27:07 +0800 Processing by FirstpagesController#page as JSON Firstpage Load (0.2ms) SELECT "firstpages".* FROM "firstpages" WHERE "firstpages"."pid" IS NULL Completed 200 OK in 2ms (Views: 0.1ms | ActiveRecord: 0.2ms)

Looks like the problem is in the actual ajax request. Try:

$.ajax({
  type: "POST",
  url: '/firstpages/page',
  dataType: 'json',
  data: {par_id : val})
})

For more examples, check out the jQuery $.ajax documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM