【问题标题】:Ruby How To Pass Values From Radio Button To Controller MethodRuby如何将值从单选按钮传递到控制器方法
【发布时间】:2021-09-02 10:44:49
【问题描述】:

您好,我已经解决这个问题太久了。所以我对 ruby​​ 和 js 没有信心。

当前我需要根据我从单选按钮单击的内容将值传递到控制器中,以便我可以比较数据库中的值是否匹配。

这是我的show.html.erb

<h1>Questions</h1>
      <%= @questions.question %>

<div class="container mt-sm-5 my-1">
    <div class="question ml-sm-5 pl-sm-5 pt-2">
        <div class="py-2 h5"><b></b></div>
        <div class="ml-md-3 ml-sm-3 pl-md-5 pt-sm-0 pt-3" id="options"> 
        <% str = @questions.answers.to_s %>
        <% pairs = str.split(',').map { |pair| pair.split('=>') }%>
        <% pairs.each do |sub_array| %>
    #this is the part where it prints out what is in my db and also sets the value corresponding to the click
             <label class="options"> <%= sub_array[1] if sub_array[1] != "nil" %><input type="radio" name="radio" value=<%= sub_array[0] if sub_array[0] != "nil" %>> <span class="checkmark"></span> </label> 
             <% end %>
        
    </div>
    
    <div class="d-flex align-items-center pt-3">
        <!--<div id="prev"> <button class="btn btn-primary">Previous</button> </div>-->
        <!--<div class="ml-auto mr-sm-5"> <button class="btn btn-success" onclick="window.location='quiz#index'">Next</button></div>-->
        <%= link_to_remote 'Submit now', :url => {:action=>"do"}, :submit => "radio_buttons"%>
        </div>
</div> 

当我点击提交按钮时,它会转到我的 quiz_controller.rb 但问题是它运行我的 show 方法而不是我的 do 方法?我不知道如何实现我的 do 方法来获取参数以进行比较

class QuizController < ApplicationController
    
    def show
        @questions = Question.find(params[:format])
    end
    
    def show_url
        
        redirect_back(fallback_location: root_path)
    end
    
    def index
        @questions = Question.all
        Question.logic
    end
    
    def do
        
    end 
    
    
private

    
end 


routes.rb

Rails.application.routes.draw do
  root to: 'home_page#index' 
  
  get 'show_url/:id', to: 'quiz#show'
  get    '/show', to: 'quiz#show'
  get    '/quiz',  to: 'quiz#index'
end

index.html.erb基于点击会传入id进行展示

<h2>Questions</h2>

<table>
  <tr>
    <th>Question</th>
    <th>Button</th>
  </tr>
 </tr>
   <% @questions.each do |q| %>
  <tr>
    <td><%= q.question %></td>
    <td><%= link_to 'Question', show_url(q.id), method: :get %></td>
    <td><%= q.id %> Button</td>
    <% end %>
  </tr>
</table>

</body>
</html>

谢谢你!

【问题讨论】:

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


    【解决方案1】:

    我会在此处查看有关如何从视图中的按钮调用特定方法的答案:How to call a controller method from a button in rails 4

    您可以通过调用将method_controller_path 添加到按钮来明确设置您希望按钮调用的方法。

    IE:&lt;%= button_to 'Invite a user', new_project_invite_path, method: :get, remote: true, class: 'btn primary' %&gt;,其中new_project_invite_pathmethod_controller_path

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 2021-08-16
      相关资源
      最近更新 更多