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:
For more examples, check out the jQuery $.ajax documentation.