Ruby on Rails-通过jQuery/Ajax在另一个视图中渲染局部视图 [英] Ruby on Rails - Render a partial in another View via jQuery/Ajax

查看:89
本文介绍了Ruby on Rails-通过jQuery/Ajax在另一个视图中渲染局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的显示方法视图中有一个链接.该链接应在后台运行该模型的创建方法,如果完成,则应更改此视图中的样式和链接. 我无法通过其他方法来更改按钮.

i have a link in the view of my show-method. This link should run the create-method of this model in the background and, if this is finished, the style and the link in this view should be changed. I cant manage it to Change the button from the other method.

查看:

      <%= div_for @movie do %>
        <p class="p p-details">
          <div class="default_list">
            <span class="label label-info">
              <%= link_to "Add to Movie List", movies_path(:add_to_list => "default_list", :mashupdb_id => @movie.mashupdb_id),:method => "post", :remote => true, :class =>"a a-label" %>
            </span>
          </div>
        </p>
      <% end %>

控制器创建操作:

  respond_to do |format|
    format.html do
      if request.xhr?
        render :partial => "movies/delete_from_default_list", :layout => "movies/show", :status => :created
      end
    end
    format.json { render :json =>  @movie }
  end

jQuery:

jQuery(function($) {
    // Callback for rendering via HTML
    $('.movie a[data-type=html]').on('ajax:success', function(event, data, status, xhr) {
        $(this).parents('div.movie').find('.label-info').replaceWith(data);
    });
});

如果我尝试这样做,就会发生提示.

If i try this, notting happens.

如果我使用下面的链接并且仅执行我的表演,它会起作用.

It works if i use the link below and my show-action only.

<span class="label label-info">
  <%= link_to "Add to Movie List", @movies, :remote => true, :class =>"a a-label" %>
</span>

是否可以从另一个控制器渲染局部图?我还能做什么?

It is possible to render a partial from another controller? What else i can do?

现在,我尝试使用js.erb文件更改链接.

Now i try to change the link with a js.erb file.

已更改的控制器:

  respond_to do |format|
    format.html
    format.js { render :content_type => 'text/javascript' }
    format.json { render :json =>  @movie }
  end

create.js.erb:

create.js.erb:

if ('<%= @list_operation.eql?('add_to_list_default_list') %>'){
    $('#<%= dom_id(@movie) %> .default_list').html('<%= raw escape_javascript( render ("movies/delete_from_default_list") )%>');
}
else if ('<%= @list_operation.eql?('delete_from_list_default_list') %>'){
    $('#<%= dom_id(@movie) %> .default_list').html('<%= raw escape_javascript( render ("movies/add_to_default_list") )%>');
}

部分_delete_from_default_list.html.erb

partial _delete_from_default_list.html.erb

<span class="label label-important">
  <%= link_to "Delete from Movie List", movies_path(:delete_from_list => "default_list", :mashupdb_id => @movie.mashupdb_id),:method => "post", :remote => true, :class =>"a a-label" %>
</span>

部分_add_to_default_list.html.erb

partial _add_to_default_list.html.erb

<span class="label label-info">
  <%= link_to "Add to Movie List", movies_path(:add_to_list => "default_list", :mashupdb_id => @movie.mashupdb_id),:method => "post", :remote => true, :class =>"a a-label" %>
</span>

如果我第一次单击该链接,则将执行js,erb文件,并且视图中的内容也会更改.(@ list_operation包含"add_to_list_default_list")
现在,如果我单击更改的链接,则也成功调用了控制器操作(@list_operation包含"delete_from_list_default_list"),但没有任何反应.

If i click the link for the first time, the js,erb file is executed and the content in my view changed.(@list_operation contains "add_to_list_default_list")
Now if i click the changed link, the controller action is also called succesfully (@list_operation contains "delete_from_list_default_list"), but nothing happens.

推荐答案

在这种情况下,我通常要做的是用js格式调用ajax调用,然后在响应的js.erb中执行此操作,

What I usually do in this situation is call the ajax call with format js, and in the responding js.erb, do this,

$('xxxx').html("<%= raw escape_javascript( render :file => "xxxx/xxxx", :locals => { xxx: xxx ) %>");

在js.erb中渲染部分.

render the partial in the js.erb.

我喜欢通过本地变量传递所需的实例,您可以在控制器中对其进行除垢,无论哪种方法都可以.

I like to pass the instances needed via locals, you can just decalre it in the controllers, either way will work.

这篇关于Ruby on Rails-通过jQuery/Ajax在另一个视图中渲染局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆