开发者

RoR- How to use div onclick to render results in a separate area of the page?

I am learning Ruby on Rails, and I am very confused on how the controller-model-view relationship works for my application.

What I have now is a table full of comments (posts) users have made. What I want to do is let users click on a comment to see more information in a separate panel (ie, other database fields that weren't in开发者_JAVA百科itially shown, for example the user_id of the person who posted the comment).

In my _post.html.erb, I have something like:

<div class="post" id="<%= post.post_id %>" onclick = ?? >
<p>post.text</p></div>

What should go in onclick? I need a way for the onclick to call a helper/controller method which can load more information, and then put that in another div on a page (I've tried variations of using the controller and helper to call javascript which inserts html into the site, but that seems messier than it should be). From what I understand, I should create some kind of partial _postdetails.html.erb file that handles the actual displaying of the html, but I have no idea how to specific where that partial would go in the page.

Any help would be appreciated, thanks!


You can achieve what you want either by using Rails helpers or by writing the AJAX calls yourself.

Personally I manually write all my AJAX calls using jQuery. You can also use Prototype which ships with Rails.

That being said you can do.

In your JS file :

$("div.some-class").click(function()
{
    $.ajax(
    {
      url:"url/to/controller/action",
      type:<GET>/<POST>,
      data://If you wish to sent any payload
    });
});

In your controller :

def some_action
  #some computation
  render :update do |page|
    page["id_of_div_to_be_refreshed"].replace_html :partial => "some_partial"
  end
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜