简体   繁体   中英

Ruby How To Pass Values From Radio Button To Controller Method

Hi I've been on this problem for way too long. So i am not confident with ruby and js.

Current i need to pass values based on what i have clicked from my radio button into a controller so i can compare the values within my database if they match or not.

This is my 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> 

这表明我的单选按钮已分配给它们的值

When i click on submit button it goes to my quiz_controller.rb but the problem is it runs my show method instead of my do method? I do not know how to implement my do method to take in params for to my comparison

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 based on click will pass in id for show

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

Thank you in advanced!

I would check out this answer here on how to invoke specific methods from a button in a view: How to call a controller method from a button in rails 4 .

You can explciit set what method you want a button to invoke by calling adding method_controller_path to the button.

IE: <%= button_to 'Invite a user', new_project_invite_path, method: :get, remote: true, class: 'btn primary' %> where new_project_invite_path is the method_controller_path .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM