这是一个示例代码,仅用于说明问题。
当我单击其中一个按钮时,我会将一些实例变量加载到视图中并进行渲染。 我不知道变量内部是否有一个或多个对象。 因此,我必须检查变量是否在其中有一个或多个对象,当它被加载到视图中时(否则如果内部只有一个对象,则.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 endThis 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 endYou 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更多推荐
发布评论