Domanda

In my rails app i have these two link_to which both obviously do something. When i

<% if Excont.where(:user_id=> current_user,:movie_id => @movie.id,:actionall => '2').includes(:user).all.count == 0%>

<tr style="border-top: none; ">
    <td>
        <%= link_to('Mark as action', {:controller => :excont, :action => 'thisisanaction', :id=> @movie.id, :actionall=>'2'}, :method => :post, :remote => true,  :disable_with => 'Updating...') %>
    </td>
</tr>

<% else %>

<tr style="border-top: none;">
    <td>
        <%= link_to('Unmark as action', {:controller => :excont, :action => 'removethisisanaction', :id=> @movie.id, :actionall=>'2'}, :method => :post, :remote => true,  :disable_with => 'Updating...') %>
    </td>
</tr>

<% end %>

When I click on the first link_to (thisisanaction), it runs the Updating... and then goes back to Mark as action, if i reload it'll have changed to Unmark as action.

What I'm asking is how to go from

Mark as action -> Updating... -> Unmark as action

and then

Unmark as action --> Updating... -> Mark as action

without having to reload the page. How would my JS code look like

È stato utile?

Soluzione

This is how i fixed it

I put this into a separate file called _action.html.erb

<% if Excont.where(:user_id=> current_user,:movie_id => @movie.id,:actionall => '2').includes(:user).all.count == 0%>

<tr style="border-top: none; ">
    <td>
        <%= link_to('Mark as action', {:controller => :excont, :action => 'thisisanaction', :id=> @movie.id, :actionall=>'2'}, :method => :post, :remote => true,  :disable_with => 'Updating...') %>
    </td>
</tr>

<% else %>

<tr style="border-top: none;">
    <td>
        <%= link_to('Unmark as action', {:controller => :excont, :action => 'removethisisanaction', :id=> @movie.id, :actionall=>'2'}, :method => :post, :remote => true,  :disable_with => 'Updating...') %>
    </td>
</tr>

<% end %>

I then created two .js.erb files named after both of the action (which is very important i've found), so thisisanaction.js.erb and removethisisanaction.js.erb.

These two files have to be both placed in the folder of the appropriate controller, in this case excont, if not it wont work.

Both of these folders should contain this code

$(".excontaction").html("<%= escape_javascript(render('layouts/action'))%>");

In the view where you want the button to show you add this

<div class="excontaction">
<%= render 'layouts/action' %>
</div>

The div class or id should match the one you put in the .js.erb folders

This should work!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top