I have some radio buttons in show.erb.html and i want to pass the value of the selected one to the controller using a submit button or link_to

here is show.html.erb

<ol>
<% for poll_answer in @poll_question.poll_answers %>
  <li>
    <%= radio_button_tag "poll", poll_answer.content ,true%>
    <%= h poll_answer.content %>
  </li>
<% end %>


and here is the controller poll_questions_controller.rb

def calc
  p = PollAnswer.find_by_content params[:poll]
  m= p.counter
  if m.nil == true
    p.update_attributes counter: 1
  else
    m = 1+m
    p.update_attributes counter: m
  end 
end  

i tried link_to

<% link_to "click here ", controller: :poll_questions, action: :calc, :poll_questions => { :poll=> calc} %>

but it didn't work

有帮助吗?

解决方案

Try form_tag helper and set your path

    <%= form_tag calc_path do %>
      <ol>
        <% for poll_answer in @poll_question.poll_answers %>
          <li>
            <%= radio_button_tag "poll", poll_answer.content ,true%>
            <%= h poll_answer.content %>
          </li>
        <% end %>
       </ol>
      <%= submit_tag "Submit" %>
    <% end %>

In your route.rb file add the following

post 'calc' => 'poll_questions#calc', as: :calc
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top