简体   繁体   English

如何将参数从JS传递到rails3控制器

[英]How to pass the params from JS to rails3 controller

Here is my JS.coffee codes: 这是我的JS.coffee代码:

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: 当我单击$('。homeNav')时,它无法将参数传递给@select,日志告诉我:

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) 在2012-09-07 05:27:07 +0800由FirstpagesController#page作为JSON首次加载(0.2ms)的GET“ / firstpages / page”开始处理127.0.0.1。选择“ firstpages”。*从“ firstpages” WHERE“ firstpages“。” pid“ IS NULL在2ms内完成200 OK(查看:0.1ms | ActiveRecord:0.2ms)

Looks like the problem is in the actual ajax request. 看起来问题出在实际的ajax请求中。 Try: 尝试:

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

For more examples, check out the jQuery $.ajax documentation . 有关更多示例,请查看jQuery $ .ajax文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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