Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 轨道4:脚手架中的条件句_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 轨道4:脚手架中的条件句

Ruby on rails 轨道4:脚手架中的条件句,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我仍在学习RoR,但我无法通过遵循其他人的代码来学习。我决定用自己的应用程序实现 我已经生成了一个脚手架,在_form.html.erb中我有: <div class="field"> <%= f.label :bath_accessories %><br> <%= f.select :bath_accessories, options_for_select(["Shower", "Extractor Fan", "Lights", "Shaver"

我仍在学习RoR,但我无法通过遵循其他人的代码来学习。我决定用自己的应用程序实现

我已经生成了一个脚手架,在_form.html.erb中我有:

<div class="field">
 <%= f.label :bath_accessories %><br>
 <%= f.select :bath_accessories, options_for_select(["Shower", "Extractor Fan", "Lights", "Shaver", "Heat Rail"]) %><br>
</div>
当用户从上述列表中选择淋浴时,我想在视图中启用:

<div class="field">
 <%= f.label :watts %><br>
 <%= f.number_field :watts %>
</div>
我不知道将if语句放在哪里:但我这样做了,但没有显示:

<% if :bath_accessories[0] %>
  # What to out put here with "<%= code %>?
生成的HTML标记:

<form class="new_bathroom_accessory" id="new_bathroom_accessory" action="/bathroom_accessories" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="r9M+Q7Np+/i68gwj65JSvV8wi4+4yb5d6tFWBHMw8k9nz1oRHGzh7AZHOTFvlaiS2lmqTch356vVYNlh7Urifw==" />

<div class="field">
 <label for="bathroom_accessory_bath_accessories">Bath accessories</label><br>
<select name="bathroom_accessory[bath_accessories]"   id="bathroom_accessory_bath_accessories"><option value="Shower">Shower</option>
<option value="Extractor Fan">Extractor Fan</option>
<option value="Lights">Lights</option>
<option value="Shaver">Shaver</option>
<option value="Heat Rail">Heat Rail</option></select><br>
</div>

 <div class="field">
  <label for="bathroom_accessory_watts">Watts</label><br>
 <input type="number" name="bathroom_accessory[watts]" id="bathroom_accessory_watts" />
</div>
<div class="field">
 <label for="bathroom_accessory_volts">Volts</label><br>
 <input type="number" name="bathroom_accessory[volts]" id="bathroom_accessory_volts" />
</div>

 <div class="actions">
  <input type="submit" name="commit" value="Create Bathroom accessory" />
 </div>
</form>
通过向特定对象添加id,您可以根据用户选择来控制它

<div class="field" id="shower_id">
 <%= f.label :watts %><br>
 <%= f.number_field :watts %>
</div>

没有任何意义,它不会起作用。更重要的是,您不能在erb中执行此操作,因为erb模板是在服务器端生成的,但您需要在用户操作时在客户端显示其他字段。所以,您应该使用JavaScript,在选择输入更改后搜索显示其他字段。您好@MarekLipka,那么客户端的任何内容都将用于JavaScript?是的,如果您想在客户端动态执行某些操作,您必须使用JavaScript。@MarekLipka非常感谢!您可以粘贴为该select标记生成的HTML代码吗?我可以给你写一个答案。
$('#bathroom_accessory_bath_accessories').on('change',function(){
        if( $(this).val()==="Shower"){
        $("#shower_id").show()
        }
        else{
        $("#shower_id").hide()
        }
    });