1

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
  • more details, do these calls do anything besides replace something in the view? like saving or deleting records? if not, you should make another method for this separate from the crud methods, no need to make them POST methods either. would recommend to have different methods for add and deleting too. Commented Nov 23, 2012 at 1:15

1 Answer 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,

$('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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, now i can change my link, but the next problem occurs directly. The js.erb is only called one time, if i use the changed content to call it again, nothing happens. Whats wrong? Controller : respond_to do |format| format.html format.js { render :content_type => 'text/javascript' } format.json { render :json => @movie } end
could you post the edited code in the question? Also, could you try figuring out how far the code is getting executed? does the call make it to the controller the second time? and the params passed the second time would help too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.