rails 4将值传递给控制器

时间:2014-04-18 17:05:30

标签: ruby-on-rails ruby-on-rails-4 controller parameter-passing

我在show.erb.html中有一些单选按钮,我想使用提交按钮或link_to

将所选的值传递给控制器

这里是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 %>


这是控制器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  

我试过link_to

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

但它无效

1 个答案:

答案 0 :(得分:2)

尝试使用form_tag帮助程序并设置路径

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

在您的route.rb文件中添加以下内容

post 'calc' => 'poll_questions#calc', as: :calc