繁体   English   中英

Rails从另一个表中提取数据

[英]rails pulling data from another table

<% @checkouts.reverse.each do |checkout| %>
  <tr>
    <td><%= checkout.radio_num %></td>
    <th colspan="3"></th>
    <td><%= checkout.badge %></td>
    <th colspan="3"></th>
  </tr>
    <% @staffs.each do |staff| %>
        <% if checkout.badge == staff.badge %>
        <tr>
            <td><%= staff.name %></td>
            <th colspan="3"></th>
            <td><%= staff.dept %></td>
        </tr>
        <% end %>
    <% end %>   
  <tr>
    <td><%= link_to 'Edit', edit_checkout_path(checkout) %></td>
    <td><%= link_to 'Destroy', checkout, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>

我是红宝石的新手,我正在尝试创建收音机的结帐/退货应用程序。 我是否需要在视图/结帐中执行某些操作以允许访问我的Staffs表?

我在第8行收到错误

nil:NilClass的未定义方法“徽章”

这是询问的控制器代码

class StaffsController < ApplicationController

def index
@staffs = Staff.all
end

def show
@staff = Staff.find(params[:id])
end

def new
@staff = Staff.new
end

def create
@staff = Staff.new(staff_params)
if @staff.save
  redirect_to(:action => 'index')
else
  render('new')
end
end

def edit
@staff = Staff.find(params[:id])
end

def update
@staff = Staff.find(params[:id])
if @staff.update_attributes(staff_params)
  redirect_to(:action => 'show', :id => @staff.id)
else
  render('index')
end
end

def delete
@staff = Staff.find(params[:id])
end

def destroy
Staff.find(params[:id]).destroy
redirect_to(:action => 'index')
end

private

def staff_params
params.require(:staff).permit(:badge, :name, :dept)
end

end

谢谢!

您正在使用@checkout (第9行),但是您声明的局部变量(第1行)实际上称为checkout

同样,您使用的是@staff但实际可用的变量是staff

因此,对于第9行,您应该改为:

   <% if checkout.badge == staff.badge %>

请注意,除非模板中已设置@variables否则@variables在模板中使用@variables

在这种情况下,您好像设置了变量:控制器中的@staffs@checkouts ...它们似乎是事物的数组。 @checkout were not set up in the controller - they are just local variables, and so are correctly named without the @staff@checkout were not set up in the controller - they are just local variables, and so are correctly named without the @`

有问题的视图是checkout / index视图,该视图将引用CheckoutController的index操作/方法。 这是由于Rails对配置的约定。 如果未在该控制器方法/操作上定义@staffs实例变量,则将引发nil:NilClass错误。

您的控制器应如下所示。

class CheckoutController < ApplicationController
  def index
    # avoid calling ActiveRecord query methods in the view
    # Checkout.all.reverse or
    @checkouts = Checkout.order(created_at: :desc)
    @staffs = Staff.all
  end
end

避免视图中的逻辑也是一种好习惯。 作为初学者,在视图中放置逻辑是很自然的事情,但是随着时间的流逝,您将看到您的视图变得更加复杂,并且认知开销也在增加。

助手方法是开始在视图中封装一些逻辑的好地方。

module CheckoutHelper
  def equal_badge?(checkout_badge, staff_badge)
    checkout_badge == staff_badge
  end
end

并在视图中使用

<% if equal_badge(checkout.badge, staff.badge) %>

Rails中的另一个很酷的技巧是呈现集合的局部(此时您存储在staff和checkout实例变量中的内容)。

创建一个_staff_list.html.erb,其中包含

  <% if equal_badge(checkout.badge, staff.badge) %>
    <tr>
      <td><% staff.name %>
      <!-- etc, etc -->
  <% end %>

并在结帐/索引视图中通过

<%= render partial: "checkouts/staff_list", collection: @staffs, locals: { checkout: checkout } %>

它超出了您的要求,但我希望这能有所帮助。

暂无
暂无

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

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