Rails:link_to和session

时间:2015-12-02 01:52:36

标签: ruby-on-rails

我在网络上创建简单的CRUD。

index.html.erb

 <% @diots.each do |diot| %>
      <tr>
        <td align="center"><%= diot.id %></td>
        <td align="center"><%= image_tag(diot.something, width: '50', height: '50') %></td>
        <td align="center"><%= link_to session[:is_checked] ? "Running" : "Stop", (session[:is_checked] ? drive_diot_path(diot) : drive_stop_diot_path(diot)) %></td>
        <td align="center"><%= link_to 'Edit', edit_diot_path(diot) %></td>
        <td align="center"><%= link_to 'Destroy', diot, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>

当我点击其中一个&#34; Running&#34;时,会转到drive.html.erb,但我会返回index.html.erb,它们都会改变&#34;停止&#34;。

当我点击其中一个&#34;正在运行&#34;,返回index.html.erb时,我该怎么办?只有一个更改为&#34;停止&#34;?

由于

1 个答案:

答案 0 :(得分:0)

您可以通过在会话中为每个diot存储一个值:

会话[:&#34;#is_checked {diot.id}&#34;]

给你:(注意:"string"给出一个有效的符号)

<% @diots.each do |diot| %>
   <tr>
     <td align="center"><%= diot.id %></td>
     <td align="center"><%= image_tag(diot.something, width: '50', height: '50') %></td>
     <td align="center"><%= link_to session[:"is_checked#{diot.id}"] ? "Running" : "Stop", (session[:"is_checked#{diot.id}"] ? drive_diot_path(diot) : drive_stop_diot_path(diot)) %></td>
     <td align="center"><%= link_to 'Edit', edit_diot_path(diot) %></td>
     <td align="center"><%= link_to 'Destroy', diot, method: :delete, data: { confirm: 'Are you sure?' } %></td>
   </tr>
 <% end %>