如何在ruby if语句条件中使用HTML ID [英] How to use HTML ID in a ruby if statement condition

查看:55
本文介绍了如何在ruby if语句条件中使用HTML ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据已选中哪个复选框来呈现表单.我想在条件中使用带有复选框ID的if语句,但是rails抛出了错误未定义的方法tab1.有没有办法在if语句中定位html?谢谢

I'm trying to render a form depending on which checkbox has been ticked. I want to use an if statement with the checkbox's id in the condition, but rails is throwing up an error 'undefined method tab1. Is there a way to target html in an if statement? thanks

<input id="tab1" type="radio" name="tabs" checked>
  <label class="label-header" for="tab1"> UK Address</label>

  <input id="tab2" type="radio" name="tabs">
  <label class="label-header"for="tab2"> International Address </label>

  <!-- <section id="content1"> -->
  	<% if tab1.checked? %>
	  	<%= f.simple_fields_for :address do |fields| %>
  			<section id="content1">
	    		<%= render "address/fields", fields: fields, addressable: addressable %>
	    	</section>
	  	<% end %>
	  <% else %>
	  	<%= f.simple_fields_for :address do |fields| %>
  			<section id="content2">
	    		<%= render "address/international_fields", fields: fields, addressable: addressable %>
	    	</section>
	  	<% end %>
  	<% end %>

推荐答案

Rails不知道您要尝试引用DOM对象的 id 字段,因为这不是要查找的内容.您收到该特定错误的原因是,ruby正在查看 tab1.checked?并说:在我可以致电 checked?之前,我需要获取 tab1 ,这样我就可以对任何类型的对象 tab1 调用 checked? ...现在,我是否有一个定义为 tab1 的变量或方法?代码> ...否?然后我将抛出一个错误,因为据我所知它是未定义的".基本上,在这种情况下,考虑HTML元素ID并不是Ruby的工作.

Rails doesn't know that you are trying to reference the id field of the DOM objects because that's not something it's looking for. The reason you are getting that specific error is because ruby is looking at tab1.checked? and saying "before I can call checked? I need to grab tab1 so i can call checked? on whatever kind of object tab1 is... Now, do I have a variable or method defined called tab1... No? Then I'm going to throw an error because as far as I know it's undefined". Basically, it's not Ruby's job to think about HTML element IDs (in this context).

如果要从DOM中提取信息,则应使用Javascript.这里有一些需要关注的问题.可以认为这就像您要告诉Rails在加载视图时渲染所有内容,然后使用Javascript事件有条件地显示和隐藏这些部分.

If you want to pull information from the DOM you should use Javascript. There is a separation of concerns to understand here. Think of it as if you are telling Rails to render everything when the view loads, then conditionally showing and hiding the sections using Javascript events.

这是一个示例:

<input type='checkbox' id='foo' checked>
<label for='foo'>Foo Questions</label>

<input type='checkbox' id='bar'>
<label for='bar'>Bar Questions</label>

<%= form_for :my_model do |f| %>
   <%= f.input :foo_questions %>
   <%= f.input :bar_questions %>
   <%= f.submit 'submit me' %>
<%= end %>

然后在相关的Javascript文件中,例如...

And then in your related Javascript file something like...

$(document).ready(function() {
   var fooQuestions = $('#foo_questions');
   var barQuestions = $('#bar_questions');

   $('#foo').on('click', function() {
      fooQuestions.show();
      barQuestions.hide();
   });

   $('#bar').on('click', function() {
      fooQuestions.hide();
      barQuestions.show();
   });

});

请记住,即使您隐藏了一个问题,该隐藏的表单字段的值仍将与表单一起提交.如果您尝试提交不同的数据集,那么您很有可能尝试对一个模型进行过多的处理.或者,如果您真的想使其工作,则可以使用Javascript从表单外部的文本框中获取响应,提取输入的值并将其设置为表单内的隐藏表单元素.但这可能超出了范围,所以我只为您指出这篇文章...

Keep in mind that even if you hide one of your questions, the value of that hidden form field will still be submitted with the form. If you are trying to submit different data sets then there is a good chance you are trying to do too much with one model. Or, if you really want to make it work you could use Javascript to take the responses from text boxes outside the form, pull the value that is entered and set it to a hidden form element inside the form. But that might be beyond the scope, so I'll just point you to this article...

此似乎是有关如何使用隐藏输入的好帖子

这篇关于如何在ruby if语句条件中使用HTML ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆