使用Ajax在Rails 3的更新部分部分、Ajax、Rails

由网友(白开水装什么纯牛奶)分享简介:我有项目的列表和侧边栏在那里我显示有关项目的详细信息。侧边栏是部分应该使用Ajax改变时,该事件OnMouseEnter在被触发的列表中的项目。I have a list of item and a sidebar where I display details about an item. The sidebar...

我有项目的列表和侧边栏在那里我显示有关项目的详细信息。侧边栏是部分应该使用Ajax改变时,该事件OnMouseEnter在被触发的列表中的项目。

I have a list of item and a sidebar where I display details about an item. The sidebar is a partial which should change with Ajax when the event onmouseenter is triggered on an item of the list.

我已成功地通过阿贾克斯(在我进入鼠标的项目)的ID发送给我的控制器行动名单,但部分未更新...:(

I have managed to send the id (of the item on which I enter the mouse) to my controller's action "list" through Ajax but the partial is not updated... :(

我相信,我是非常接近的解决方案。我是新使用Ajax,我已经搜查计算器和其他许多网站整个下午,但他们总是解释如何管理与阿贾克斯的CRUD操作,它似乎并没有为我工作...

I am sure that I am very close to the solution. I am new with Ajax and I have searched the entire afternoon on StackOverflow and many other websites but they always explain how to manage the CRUD actions with Ajax and it doesn't seem to work for me...

下面是code:

-The AJAX部分:

-The ajax part:

$("div.item").mouseenter(function(event){
 var id = $(this).attr('data');
    var url = "/item/list?id="+id+"&lg=en";
    var data = $(this).serialize();
    $.post(url,function(response_data){$("#item_details").html(response_data)});
    return false;
});

-The控制器操作:

-The controller action:

def list
    @items = Item.all
    if request.xhr?
        id_exists?("item", params[:id], "item", "list"){@item = Item.find(params[:id])}

        render :partial => "item/sidebar/sidebar_home", :object => @item
    else
        @item = @items.first unless @items.empty?
    end
end

-The HAML:

-The haml:

content_for :sidebar do
   #item_details
       =render :partial => "item/sidebar/sidebar_home", :object => @item


.items_wrapper
-if @items.empty?
    ="No items"
-else
    = render :partial => "item/partials/item", :collection => @items

里面的第二个部分我有一个包裹一切div.item。 所以基本上,我想更新侧边栏的内容,这就是全部。

Inside of the second partial I have the div.item which wraps everything. So basically, I want to update the content of the sidebar, that's all.

抱歉,如果这个问题对你有所帮助看起来很可笑,并在此先感谢。

Sorry if the question seem silly and thanks in advance for your help.

问题解决了感谢,以OGZ的精灵! XD

Problem solved thank to Wizard of Ogz! XD

推荐答案

您需要传递一个回调函数来处理来自您的AJAX请求的响应。请参见 http://api.jquery.com/jQuery.post/

You need to pass a callback function to handle the response from your AJAX request. See http://api.jquery.com/jQuery.post/

一个例子可能是这样的:

An example could look like this:

$.post(url, data, function(response_data){ $("#sidebar_id").replaceWith(response_data) });
阅读全文

相关推荐

最新文章