检查实例变量是否包含一个或多个对象?(Check if an instance variable has one or more objects?)

编程入门 行业动态 更新时间:2024-10-26 06:27:26
检查实例变量是否包含一个或多个对象?(Check if an instance variable has one or more objects?)

这是一个示例代码,仅用于说明问题。

当我单击其中一个按钮时,我会将一些实例变量加载到视图中并进行渲染。 我不知道变量内部是否有一个或多个对象。 因此,我必须检查变量是否在其中有一个或多个对象,当它被加载到视图中时(否则如果内部只有一个对象,则.each方法将失败)。 或者有没有办法将变量中的一个对象存储为数组?

aa.html.erb

<div class="fill"></div> <%= button_to 'ALL', { :controller => 'animals', :action => 'vaa', :id => "0" } , remote: true %> <%= button_to 'ELEPHANT', { :controller => 'animals', :action => 'vaa', :id => "1" } , remote: true %> <%= button_to 'ZEBRA', { :controller => 'animals', :action => 'vaa', :id => "2" } , remote: true %> <div class="fill3"></div> <%= render 'animals/vaa' %>

_vaa.html.erb

<div class="fill4"> <% @animals.each do |animal| %> <table> <tr> <td><%= animal.name %></td> <td><%= animal.race %></td> <td><%= link_to 'Show', animal %></td> <td><%= link_to 'Edit', edit_animal_path(animal) %></td> <td><%= link_to 'Destroy', animal, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> </table> <% end %> </div> <div class="fill5"> </div>

animals_controller.rb

def vaa if params[:id] == "0" @animals = Animal.all elsif params[:id] == "1" @animals = Animal.first elsif params[:id] == "2" @animals.second end respond_to do |format| format.js end end

This is an example code, just to illustrate the problem.

When I click one of the buttons I will load some instance variable into the view and render it. I don't know if the variable will have one or more objects inside. I therefore have to check if the variable has one or more objects inside, when it is loaded into the view (else the .each method will fail if there is only one object inside). Or is there a way to store just one object inside the variable as an array?

aa.html.erb

<div class="fill"></div> <%= button_to 'ALL', { :controller => 'animals', :action => 'vaa', :id => "0" } , remote: true %> <%= button_to 'ELEPHANT', { :controller => 'animals', :action => 'vaa', :id => "1" } , remote: true %> <%= button_to 'ZEBRA', { :controller => 'animals', :action => 'vaa', :id => "2" } , remote: true %> <div class="fill3"></div> <%= render 'animals/vaa' %>

_vaa.html.erb

<div class="fill4"> <% @animals.each do |animal| %> <table> <tr> <td><%= animal.name %></td> <td><%= animal.race %></td> <td><%= link_to 'Show', animal %></td> <td><%= link_to 'Edit', edit_animal_path(animal) %></td> <td><%= link_to 'Destroy', animal, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> </table> <% end %> </div> <div class="fill5"> </div>

animals_controller.rb

def vaa if params[:id] == "0" @animals = Animal.all elsif params[:id] == "1" @animals = Animal.first elsif params[:id] == "2" @animals.second end respond_to do |format| format.js end end

最满意答案

如果您正在检索的@animals不是如下数组,则可以将@animals作为数组返回:

def vaa if params[:id] == "0" @animals = Animal.all elsif params[:id] == "1" @animals = Animal.first elsif params[:id] == "2" @animals.second end # The following line makes sure @animals is Array if @animals is not an array. @animals = [@animals] unless @animals.kind_of?(Array) respond_to do |format| format.js end end

You can return @animals as array if @animals you're retrieving is not an array like follows:

def vaa if params[:id] == "0" @animals = Animal.all elsif params[:id] == "1" @animals = Animal.first elsif params[:id] == "2" @animals.second end # The following line makes sure @animals is Array if @animals is not an array. @animals = [@animals] unless @animals.kind_of?(Array) respond_to do |format| format.js end end

更多推荐

本文发布于:2023-08-07 04:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1461192.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   变量   实例   对象   variable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!