Ruby on Rails - 通过jQuery / Ajax在另一个View中渲染部分

时间:2022-10-08 13:22:40

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.

我的show-method视图中有一个链接。此链接应在后台运行此模型的create-method,如果已完成,则应更改此视图中的样式和链接。我不能管理它来改变其他方法的按钮。

View:

视图:

      <%= 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 %>

Controller Create Action:

控制器创建动作:

  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的:

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.

如果我使用下面的链接和我的show-action,它可以工作。

<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?

可以从另一个控制器渲染部分?我还能做什么?

Edit:

编辑:

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

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

Changed Controller:

更改控制器:

  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") )%>');
}

partial _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>

partial _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>

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,erb文件并更改我视图中的内容。(@ list_operation包含“add_to_list_default_list”)现在,如果单击更改的链接,控制器操作也会成功调用(@ list_operation包含“delete_from_list_default_list”),但没有任何反应。

1 个解决方案

#1


1  

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

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

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

render the partial in the js.erb.

在js.erb中渲染部分。

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

我喜欢通过本地人传递所需的实例,你可以在控制器中对其进行十分转换,无论哪种方式都可以。

#1


1  

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

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

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

render the partial in the js.erb.

在js.erb中渲染部分。

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

我喜欢通过本地人传递所需的实例,你可以在控制器中对其进行十分转换,无论哪种方式都可以。