简体   繁体   English

在Ruby on Rails中使用单选按钮和link_to

[英]Using radio buttons and link_to with Ruby on Rails

Bit of a Rails newbie here so please bear with me. 这里是Rails的新手,请多多包涵。

I am trying to list a bunch of entries in a table by first listing a radio button for each entry, and then the fields for the entry as just text. 我试图通过首先列出每个条目的单选按钮,然后将条目的字段仅作为文本列出表中的一堆条目。

It's working fine, it's displaying the way I want it too. 它工作正常,也显示了我想要的方式。 However, the problem is whenever I'm using my buttons. 但是,问题出在我使用按钮时。 The idea is to have the user select one of the radio buttons to choose a specific recipe, and then click on one of the universal Add, Edit, Delete buttons at the top of the page to perform the respective action on the selected recipe. 想法是让用户选择一个单选按钮以选择特定的配方,然后单击页面顶部的通用“添加”,“编辑”,“删除”按钮之一以对所选配方执行相应的操作。

So far this is my code: 到目前为止,这是我的代码:

<!--Action buttons at the top of page, supposed to communicate with the radio buttons --> 
<%= link_to("Show", {:action => 'show', :id => recipe.id}, :class => 'action show') %>
<%= link_to("Edit", {:action => 'edit', :id => recipe.id}, :class => 'action edit') %>
<%= link_to("Delete", {:action => 'delete', :id => recipe.id}, :class => 'action delete') %> 

<% @recipes.each do |recipe| %>
<tr>
  <td><%= radio_button_tag 'recipe', recipe, @recipe == recipe %></td>
  <td><%= recipe.total_calories %></td>
  <td><%=........15 more fields
</tr>`
<% end unless @recipes.nil? %>

It's throwing me an error that undefined local variable or method `recipe'. 这给我抛出了一个错误,即未定义的局部变量或方法“ recipe”。 I get why I'm getting the error; 我明白为什么我得到了错误; Ruby doesn't know exactly which recipe the user is selecting via the radio button. Ruby并不完全知道用户通过单选按钮正在选择哪种食谱。 Can someone show me how to get the radio buttons to communicate with the action button? 有人可以告诉我如何使单选按钮与操作按钮通信吗?

The most straightforward way of doing this would be to create a form containing the actions as submission button values. 最简单的方法是创建一个包含动作作为提交按钮值的表单。 Something like: 就像是:

<%= form_tag(PATH_TO_YOUR_CONTROLLER) do %>
   <%= submit_tag("Show") %>
   <%= submit_tag("Edit") %>
   <%= submit_tag("Delete") %>

   RECIPE_LIST_HERE
<% end >

Then your controller would need to look up what action and recipe were submitted (I believe those will be under params[:commit] and params[:recipe]). 然后,您的控制器将需要查找提交了哪些操作和配方(我相信这些将在params [:commit]和params [:recipe]下)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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