简体   繁体   English

rails和html,如何从表单中检索参数

[英]rails and html, how to retrieve params from a form

i have the follow 我有以下

<%= form_tag lessons_path, :method => 'get', :name => "filter_form" do %>
  <div class="btn-group" data-toggle-name="is_private" data-toggle="buttons-radio" >
  <button value="0" class="btn" data-toggle="button">Public</button>
  <button value="1" class="btn" data-toggle="button">Private</button>
</div>

when a user clicks on either of those buttons, the form submits and goes to my lessons#index. 当用户单击这些按钮中的任何一个时,表单将提交并转到我的课程#index。 how can i get what value the user clicked on? 我如何获得用户点击的价值? i understand that params[:something] usually comes from a GET request in the url, but what about in this case? 我知道params [:something]通常来自url中的GET请求,但是在这种情况下呢?

thanks 谢谢

Just give your buttons name attributes: 只需给您的按钮name属性:

<%= form_tag lessons_path, :method => 'get', :name => "filter_form" do %>
  <div class="btn-group" data-toggle-name="is_private" data-toggle="buttons-radio" >
    <button name="public"  type="submit" value="0" class="btn" data-toggle="button">Public</button>
    <button name="private" type="submit" value="1" class="btn" data-toggle="button">Private</button>
  </div>
<% end %>

and then check params as usual in your controller: 然后像往常一样在控制器中检查params

if(params[:public])
    # public has pressed
elsif(params[:private])
    # private was pressed
else
    # Hmm, someone is trying to cause trouble.
end

You'll note that I added type="submit" to the <button> s, some browsers use type="submit" as the default (as per the standard ) and some use type="button" so it is best to be explicit. 您会注意到,我在<button>添加了type="submit" ,某些浏览器将type="submit"作为默认值(按照标准 ),而某些浏览器则使用type="button"因此最好明确。

In rails, the params hash will contain all the elements that have been submitted/passed to the page. 在rails中,params哈希将包含已提交/传递到页面的所有元素。 This includes GET parameters and POST parameters. 这包括GET参数和POST参数。 You should be using a check box (or perhaps radio buttons) to represent this type of data. 您应该使用一个复选框 (或也许是单选按钮)来表示这种类型的数据。 If your field exists on the database, then you should do the following: 如果您的字段存在于数据库中,则应执行以下操作:

<br><%= f.check_box :is_private %> Private

You can then access the data with params[:is_private] 然后,您可以使用params[:is_private]访问数据

If you want to use buttons, then I Would recommend creating two urls to toggle the state of the check box. 如果要使用按钮,那么我建议创建两个URL来切换复选框的状态。 Otherwise you will have to depend on JavaScript to change a field. 否则,您将不得不依靠JavaScript来更改字段。

If you want to use buttons, but also use the same update url, then you will need to have the button presses toggle the state of the check box there are JQuery plugins that can do this (one example is http://widowmaker.kiev.ua/checkbox/ ). 如果要使用按钮,而且还使用相同的更新URL,则需要按按钮来切换复选框的状态,有一些JQuery插件可以做到这一点(一个示例是http://widowmaker.kiev .ua / checkbox / )。

A form will only submit to one action. 一张表格只会提交给一个动作。

Use i18n to define your button text then check the i18n value of value of the commit param in the controller action 使用i18n定义按钮文本,然后在控制器操作中检查提交参数的i18n值

eg in your form 例如以你的形式

<%= submit_tag I18n.t('checkout.button.place_order'), :class => "submit" %>

The same will work for a f.submit button. f.submit按钮也一样。

then in your controller 然后在您的控制器中

if params[:commit] == I18n.t('checkout.button.place_order')
  Do something
else
  Do something else
end

The in your en.yml or whatever to define the actual text 您的en.yml中的或定义实际文本的内容

en:
  checkout:
    button:
      place_order: 'Place Order'

Obviously just adjust to suit your needs 显然只是调整以适合您的需求

By using i18n, you can easily change the text on your buttons in the i18n definition without having any effect on your controller logic and will still work on any internationalisation/translations you wish to use 通过使用i18n,您可以轻松地在i18n定义中更改按钮上的文本,而不会影响您的控制器逻辑,并且仍然可以使用您希望使用的任何国际化/翻译

Using this you can set up as many buttons on a single form as you like then add whatever conditional logic you need in the controllers action 使用此功能,您可以在单个表单上设置任意数量的按钮,然后在controllers操作中添加所需的任何条件逻辑。

UPDATE I18n is short for internationalisation (18 chars in the word :)), just create a file in the locale folder called en.yml then define whatever you need in the format that I described above and you are good to go. UPDATE I18n是国际化的缩写(单词:)中的18个字符),只需在区域设置文件夹中创建一个名为en.yml的文件,然后按照我上面描述的格式定义所需的内容即可。 Reference I18n variables anywhere you need them using the t. 使用t在需要它们的任何地方引用I18n变量。 helper Have a look at this railscast for more detailed explanation http://railscasts.com/episodes/138-i18n 帮手看看这个railscast以获得更详细的说明http://railscasts.com/episodes/138-i18n

Update 2 In response to mu is too short's comment below 更新2在下面回应mu太短了

Why not just add a name attribute? 为什么不只是添加名称属性? Then you can work with what the button independent of its content. 然后,您可以使用与按钮内容无关的按钮。 Keep in mind that a can contain embedded HTML that won't make much sense inside param 请记住,a可以包含在param内部没有多大意义的嵌入式HTML

using i18n makes this comment totally invalid as the text is not checked at all. 使用i18n会使此注释完全无效,因为根本不检查文本。 You can make the text whatever you want and indeed it is good practice to use I18n. 您可以随意编写文本,使用I18n确实是一种好习惯。 Note that you are checking the constant in the controller 'checkout.button.place_order' in the above example NOT the value which is defined as 'Place Order'. 请注意,在上面的示例中,您正在检查控制器“ checkout.button.place_order”中的常量,而不是定义为“下订单”的值。

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

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