【问题标题】:Rails button on click call method点击调用方法上的Rails按钮
【发布时间】:2014-09-15 02:38:17
【问题描述】:

我仍在学习 Ruby on Rails,并尝试在该控制器中调用自定义方法 check_bike_in,并在单击按钮时将复选框和文本区域中的值传递给它。

我正在使用helper_method 调用check_bike_in,但是我不知道在单击按钮时如何调用它或如何将变量传递给控制器​​。

任何帮助将不胜感激。

<table class="checked_out">
  <thead>
  <tr>
    <th>User</th>
    <th>Bike</th>
    <th>Checkout time</th>
  </tr>
  </thead>

  <tbody>
  <% @checkouts.each do |checkout| %>
      <tr>
        <td class="user"><%= checkout.user_id %></td>
        <td class="bike_number"><%= checkout.bike_id %></td>
        <td><button onclick="$('#<%= checkout.id %>').toggle()">Check Bike In</button></td>
      </tr>
      <tr style="display: none" id = "<%= checkout.id %>">
        <td class="needs_reapir_checkbox"><input type="checkbox"> Needs Repair</td>
        <td class="repair_textbox"><textarea rows="1" cols="50"></textarea></td>
        <td><button onclick="">Check In</button></td>
      </tr>
  <% end %>
  </tbody>
</table>

【问题讨论】:

    标签: javascript html ruby-on-rails ruby helper


    【解决方案1】:

    您的方法是 POST 还是 GET?,如果您的方法是 POST,您需要在表单中使用提交按钮来发送数据,否则您需要带有参数的链接:

    例子:

    对于 POST 方法

    <td><%= button_to 'Check Bike In', action: :check_bike_in, id: checkout.id %></td>
    

    对于 GET 方法

    <td><%= link_to 'Check Bike In', action: :check_bike_in, id: checkcout.id %></td>
    

    【讨论】:

    • 我正在尝试做一个 POST,如果我通过了 checkout.id ,那么它会传入该标签内的 textarea 和复选框吗?我只是担心 Rails 参数会阻止它被传入。(使用 raisl 4。很抱歉尝试不使用 form_for 在 Rails 中做事。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 2016-11-17
    • 2014-08-17
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多