0

How can I call a method about a form?

I have 3 radio buttons and I want to call a method depending on which radio button is checked. After the call I have to return the controller a variable to the view.

How can I do that?

My approach:

I want use CSS do design my HTML. That is the reason why I haven't used a form helper:

<div id="cx" class="cxs">
  <form>
    <fieldset>
      <label>
        <input type="radio" id="cxId" name="checkBox"> ID
      </label>
      <label>
        <input type="radio" id="cxName" name="checkBox"> Name
      </label>
      <label>
        <input type="radio" id="cxArtikelnummer" name="checkBox"> Artikelnummer
      </label>
    </fieldset>
    <button type="button" id="cxBtn">Ok</button>
  </form>
</div>

Here I want to call a method depending on which radio button is checked (show by name, id or artikelnummer).

I have solved it stati, but the choose have to be dynamic and I thought do that with methods.

<% $i = 0 %>
  <% $num = @title.count %>
  <% @title.each do |row| %>
    Name: <%= row["Name"] %>
    Durchlauf: <%= $i %> <br>
  <% $i += 1 %>
<% end %>
6
  • what is @title? where is it initialised? Commented May 3, 2017 at 13:51
  • sorry, title is initialised in my controller. "@title = data_array" title is an 2d array Commented May 3, 2017 at 13:52
  • you can set name as your method_name, and when selected you can call the corresponding method using send(method_name.to_sym). Commented May 3, 2017 at 13:56
  • i dont understand it. can you give me an example? Commented May 3, 2017 at 14:08
  • "Here I want to call a method depending on which radio button is checked" did you consider to use JS to do what you want? Commented May 3, 2017 at 14:58

1 Answer 1

2

If @title represents a model in which there is for example "aaa" field of string type which can have the "ID" value, "Name" value and "Artikelnummer", try use this:

<% @title.each do |row| %>
      <% if row.aaa == "ID" %>
          #code for ID option
      <% elsif row.aaa == "Name" %>
          #code for Name option
      <% elsif row.aaa == "Artikelnummer %>
          #code for Artikelnummer option
      <% end %>
<% end %>

That is my idea. Tell me if it will help you.

But I think you can use form_for helper and radio_button helper inside with its :checked option and the whole form_for you can design via CSS via classes for every <li class="sth"><%= f.radio_button "Name", checked: true%></li> but I should check if this solution is right.

Rgds, Marta

Sign up to request clarification or add additional context in comments.

1 Comment

Bad thing is he's trying to do something when the radio button is checked. So isn't completelly a server side issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.