views:

48

answers:

3

Good day...

Still a beginner question. I mostly asking to save myself hours of trial and error... I had those already and there was no result.

What I wanna do... I have a controller that has an action, of course. I want to send this action two IDs, it should make some database entry of those. No problem, the action works well.

But I don't want a view to it, I want the user to click on a link and not even leave the site. Ideally there should be some Ajax (using jQuery a lot on the site) that changes the link to a 'Thanks' or anything.

So yes, my main question is: How can I activate an action of an other controller without leaving the site, from a link. Long intro, but I hope you can give me some hints.

A: 

Look up link_to_remote method in rails. That should make an ajax call out to a controller function and not leave the page.

shoebox639
Nope. link_to_function runs javascript.
nathanvda
Whoops, was thinking `link_to_remote` and typed out `function`.
shoebox639
+2  A: 

Take a look at link_to_remote for the view side and then you can just have your controller function render nothing => true.

Tony Fontenot
Normally you would render something, preferably some js that updates your page, notifies you something has been done, stops your spinner.
nathanvda
+1  A: 

To keep it un-obtrusive & return something from the controller action... I think you probably want something like:

def my_action
  # your code...  e.g. @model = Model.find(params[:id])

  render :json => { :whatever => @model.whatever }
end

In your javascript, do what thou wilt with the returned information & prevent the default action of your hyperlink by returning false or using jQuery's prevent default functionality for the click event.

Brian