John
John

Reputation: 634

Pass params to controller using ajax

It's my first time doing this, and I am a bit confused on how to pass params to a controller via ajax, and recreate the view with the new data.

So I have one <%=link_to 'Today', [@project, @keyword] remote: true%> (@project, @keyword) will redirect me to the same view, but how do I pass the param? which in this case will have to send a date (Date.today).

$ ->
 $("a[data-remote]").on "ajax:success", (e, data, status, xhr) ->
   alert "Success"

What do I need to add in that ajax response to recreate my view with new data.

Can someone help me with a good resource and applicable to rails 4.

Upvotes: 0

Views: 159

Answers (2)

Kirti Thorat
Kirti Thorat

Reputation: 53048

You could use this:

<%=link_to 'Today', [@project, @keyword], :date => Date.today, remote: true %>

It will pass the Today's date in params hash which you can use in your application.

Upvotes: 1

rhernando
rhernando

Reputation: 1071

Just pass params as it was a normal link (eg :date => Date.today)

Controller will receive it in params, and should render a JS.erb file. Use JS to re-render the part of the view affected

$('#divaffected').html('<%= escape_javascript(render('a_partial')) %>')

Upvotes: 1

Related Questions