开发者

Doing an action in Rails without changing site

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 othe开发者_开发技巧r controller without leaving the site, from a link. Long intro, but I hope you can give me some hints.


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜