<% @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 %>
I'm new to ruby on rails, and I'm trying to create a checkout/return application for radios. Is there something I need to do within my views/checkouts to allow access to my Staffs table?
我是ruby on rails的新手,我正在尝试创建一个用于收音机的结账/返回应用程序。在我的视图/结帐过程中是否需要做一些事情来允许访问我的员工表?
I get the error on line 8
我在第8行得到了错误。
undefined method `badge' for nil:NilClass
无定义方法“徽章”为nil:NilClass。
This is the controller code that was asked
这是被问到的控制器代码。
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
Thanks!
谢谢!
2 个解决方案
#1
0
The view in question is the checkout/index view which will reference the CheckoutController's index action/method. This is due to rails convention over configuration. If the @staffs
instance variable isn't defined on that controller method/action then a nil:NilClass error will be thrown.
问题的视图是签出/索引视图,它将引用CheckoutController的索引操作/方法。这是由于rails约定优于配置。如果在控制器方法/操作上没有定义@staff实例变量,则会抛出nil:NilClass错误。
Your controller should look something like this.
控制器应该是这样的。
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
It is also good practice to avoid logic in the views. It feels natural as a beginner to place logic in the view, but as time passes you will see your views become more complex and cognitive overhead increases.
在视图中避免逻辑也是很好的做法。作为一个初学者,在视图中放置逻辑是很自然的,但是随着时间的推移,你会看到你的观点变得更加复杂,认知开销也会增加。
Helper methods are a good place to start encapsulates some of logic in the view.
Helper方法是一个很好的起点,可以在视图中封装一些逻辑。
module CheckoutHelper
def equal_badge?(checkout_badge, staff_badge)
checkout_badge == staff_badge
end
end
And call it in the view using
在视图中调用它。
<% if equal_badge(checkout.badge, staff.badge) %>
Another cool trick in rails is rendering partials for collections (what you have stored within the staff and checkout instance variables at the moment).
rails中的另一个很酷的技巧是为集合呈现部分(此时您已经将其存储在staff和checkout实例变量中)。
Create a _staff_list.html.erb which contains
创建一个_staff_list.html。erb包含
<% if equal_badge(checkout.badge, staff.badge) %>
<tr>
<td><% staff.name %>
<!-- etc, etc -->
<% end %>
And in the checkouts/index view render that partial by
在checkouts/index视图中,呈现出部分by。
<%= render partial: "checkouts/staff_list", collection: @staffs, locals: { checkout: checkout } %>
It's more than you asked for, but I hope this helps a bit.
这比你要求的要多,但我希望这能有所帮助。
#2
0
you are using @checkout
(on line 9) but the local variable you declare (on line 1) is actually called checkout
您使用的是@checkout(在第9行),但是您声明的本地变量(在第1行)实际上称为checkout。
Likewise, you are using @staff
but the variable you actually have available to you is staff
同样,您使用的是@staff,但是您实际上可以使用的变量是staff。
So for line 9 you should instead have:
所以对于第9行,你应该:
<% if checkout.badge == staff.badge %>
Be aware that you do not need to use @variables
inside your template unless they are something that was set up in the controller.
请注意,您不需要在模板中使用@variables,除非它们是在控制器中设置的。
In this case, it looks like you set up the variables: @staffs
and @checkouts
in the controller... they appear to be arrays of things. @staff
and @checkout were not set up in the controller - they are just local variables, and so are correctly named without the
@`
在本例中,它看起来像设置了变量:控制器中的@staff和@checkouts…它们看起来是一系列的东西。@staff和@checkout并不是在控制器中设置的——它们只是局部变量,所以正确地命名为“没有@”
#1
0
The view in question is the checkout/index view which will reference the CheckoutController's index action/method. This is due to rails convention over configuration. If the @staffs
instance variable isn't defined on that controller method/action then a nil:NilClass error will be thrown.
问题的视图是签出/索引视图,它将引用CheckoutController的索引操作/方法。这是由于rails约定优于配置。如果在控制器方法/操作上没有定义@staff实例变量,则会抛出nil:NilClass错误。
Your controller should look something like this.
控制器应该是这样的。
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
It is also good practice to avoid logic in the views. It feels natural as a beginner to place logic in the view, but as time passes you will see your views become more complex and cognitive overhead increases.
在视图中避免逻辑也是很好的做法。作为一个初学者,在视图中放置逻辑是很自然的,但是随着时间的推移,你会看到你的观点变得更加复杂,认知开销也会增加。
Helper methods are a good place to start encapsulates some of logic in the view.
Helper方法是一个很好的起点,可以在视图中封装一些逻辑。
module CheckoutHelper
def equal_badge?(checkout_badge, staff_badge)
checkout_badge == staff_badge
end
end
And call it in the view using
在视图中调用它。
<% if equal_badge(checkout.badge, staff.badge) %>
Another cool trick in rails is rendering partials for collections (what you have stored within the staff and checkout instance variables at the moment).
rails中的另一个很酷的技巧是为集合呈现部分(此时您已经将其存储在staff和checkout实例变量中)。
Create a _staff_list.html.erb which contains
创建一个_staff_list.html。erb包含
<% if equal_badge(checkout.badge, staff.badge) %>
<tr>
<td><% staff.name %>
<!-- etc, etc -->
<% end %>
And in the checkouts/index view render that partial by
在checkouts/index视图中,呈现出部分by。
<%= render partial: "checkouts/staff_list", collection: @staffs, locals: { checkout: checkout } %>
It's more than you asked for, but I hope this helps a bit.
这比你要求的要多,但我希望这能有所帮助。
#2
0
you are using @checkout
(on line 9) but the local variable you declare (on line 1) is actually called checkout
您使用的是@checkout(在第9行),但是您声明的本地变量(在第1行)实际上称为checkout。
Likewise, you are using @staff
but the variable you actually have available to you is staff
同样,您使用的是@staff,但是您实际上可以使用的变量是staff。
So for line 9 you should instead have:
所以对于第9行,你应该:
<% if checkout.badge == staff.badge %>
Be aware that you do not need to use @variables
inside your template unless they are something that was set up in the controller.
请注意,您不需要在模板中使用@variables,除非它们是在控制器中设置的。
In this case, it looks like you set up the variables: @staffs
and @checkouts
in the controller... they appear to be arrays of things. @staff
and @checkout were not set up in the controller - they are just local variables, and so are correctly named without the
@`
在本例中,它看起来像设置了变量:控制器中的@staff和@checkouts…它们看起来是一系列的东西。@staff和@checkout并不是在控制器中设置的——它们只是局部变量,所以正确地命名为“没有@”