使用Javascript或Ajax在ROR中附加变量

时间:2014-07-31 12:36:03

标签: javascript ruby-on-rails ruby

请原谅我非常愚蠢的问题,我是Ruby On Rails的新手。我试图创建一个页面的模型,该页面有两个视图,Product View One和Product View Two。两个视图中有一个小的内容变化,试图通过设置变量product_view_one = true或false来实现它。

现在我的问题是

  1. 如何使用Javascript或Ajax在ROR中附加变量?
  2. 以下是代码..

    <% product_view_one = [true].sample %>
    <table class="table table-hover table-line">
    <thead>
       <tr>
         <th colspan="<%= product_view_one ? "5" : "4" %>">        
        <ul class="nav nav-pills"> 
          <li> <a href="javascript:void(0)"> <%= rand(1..20) %> Published </a></li>
          <li> <a href="javascript:void(0)"> <%= rand(1..8) %> Dtafts </a></li> 
          <li> <a href="javascript:void(0)"> <%= rand(1..8) %> Expired </a></li> 
        </ul>
      </th>
    
      <th colspan="2">
        <ul class="pagination">  <!— View Change —>
          <li><a href=“#” onClick ="changeProductView ()"> Product View One </a></li> <!— 
          <li><a href=“#” onClick ="changeProductView ()"> Product View Two </a></li>
        </ul>
      </th>
    
    </tr>
    
    <tr>
      <th class="title">
        <input class="form-control input-sm" type="text" placeholder="Search">
      </th>
    
      <th> 
        <%= product_view_one ? "Users" : "" %>
      </th>
    
      <th>
        <%= product_view_one ? "Group" : "Audience" %> 
      </th>
    
      <th>
        Updates
      </th>
    
      <% if !product_view_one %>
      <th>
        Expiration
      </th>
      <% end %>
    
      <th>Published</th>
    
      <th class="icons"></th>
    
    </tr>
    
    </thead>
    <tbody>
    
    <% 10.times do |i| %>
      <tr>
        <td>
          <a href="/mockups/course/index.html"><%= Faker::Lorem.words(rand(2..5)).join(' ').capitalize %></a>
    
          <% if product_view_one %>
            <!-- tags -->
            <ul class="media-tags">
              <% [1, 2, 3, 4].sample.times do %>
              <li class="media-tag media-tag-default"><%= ["Service", "UX", "Wireframes", "Misc"].sample %></li>
              <% end %>
            </ul>
          <% end %>
        </td>
    
        <td> 
          <% if !product_view_one %>
          <!-- tags -->
          <ul class="media-tags">
            <% [1, 2, 3, 4].sample.times do %>
            <li class="media-tag media-tag-default"><%= ["Service", "UX", "Wireframes", "Misc"].sample %></li>
            <% end %>
          </ul>
          <% else %>
            <ul> 
              <% rand(1..9).times do %>
                <li> <%= Faker::Name.first_name %>, </li>
              <% end %>
            </ul>
          <% end %>
        </td>
    
        <% if product_view_one %>
        <td> 
          <ul> 
            <% rand(1..9).times do %>
              <li> <%= Faker::Name.first_name %>, </li>
            <% end %>
          </ul>
        </td>
        <% end %>
    
        <% if !product_view_one %>
        <td >
          <a class="views-views report-link" href="javascript:void(0);">
            <% if [true, false, false].sample %>
              <%= rand(1..999) %> 
            <% else %>
              <%= rand(1..9) %> 
            <% end %>
          </a>
        </td>
        <% end %>
    
        <td >
          <a class="" href="javascript:void(0);"> 
            <% if [true, false, false].sample %>
              <%= rand(1..28) %> <%= ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"].sample %> by <%= Faker::Name.first_name %>
            <% else %>
              <%= "-" %>
            <% end %>
          </a>
        </td>
    
        <% if !product_view_one %>
        <td >
          <span class="" ><%= rand(1..150) %> days </span>
        </td>
        <% end %>
    
        <td>
           Oct 28
        </td>
    
        <td class="action-btn">
          <a href="#" class="btn btn-xs"><i class="icon icon-pencil"></i></a>
          <a href="#" class="btn btn-xs"><i class="icon icon-trash-o"></i></a>
        </td>   
    
      </tr>
    <% end %>
    </tbody>
    </table>
    
    <!— jQuery Code to set the variable —>
    
    <script>
    $("#prodViewOne").click(function () {
     var prod_view_one = true ;
    });
    
    $("#prodViewTwo").click(function () {
     var prod_view_one = false ;
    });
    </script>
    

0 个答案:

没有答案