0

在我的 Rails 应用程序中,我有这两个link_to显然都做了一些事情。当我

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

当我单击第一个link_to(thisisanaction)时,它运行Updating...然后返回到Mark as action,如果我重新加载它会变为Unmark as action.

我要问的是如何从

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

进而

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

无需重新加载页面。我的 JS 代码会是什么样子

4

1 回答 1

0

这就是我修复它的方法

我把它放在一个名为 _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 %>

然后我创建了两个.js.erb以这两个动作命名的文件(我发现这非常重要),所以thisisanaction.js.erbremovethisisanaction.js.erb.

这两个文件都必须放在相应控制器的文件夹中,在这种情况下excont,如果没有,它将无法工作。

这两个文件夹都应包含此代码

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

在您希望按钮显示的视图中添加此

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

divclassid应该与您放在.js.erb文件夹中的那个相匹配

这应该工作!

于 2013-08-12T23:08:29.010 回答