Ruby on Rails - Render a partial in another View via jQuery/Ajax

僤鯓⒐⒋嵵緔 提交于 2019-12-11 10:37:17

问题


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.

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

Edit:

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

Changed Controller:

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

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

<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

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


回答1:


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

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.



来源:https://stackoverflow.com/questions/13504372/ruby-on-rails-render-a-partial-in-another-view-via-jquery-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!