RoR, Passing a variable into view gives a null object
I am setting a variable in my controller but for some reason it is not getting set. I tried it two ways.
def update
# @available_cars = Car_info.where("user_id = ?", session[:user_id])
@available_cars = Car_info.find_by_user_id(session[:user_id])
end
In my view I do this.
<% @available_cars.each do |car| %>
<%= car.name %>
<% end %>
What I intend to do is populate the @available_cars into a drop down list but I开发者_如何学编程 can't even get them to print. I know the session[:user_id] is set because I print it and access it elsewhere.
I get this error... Expected D:/RailsProjects/mileage/app/models/car_info.rb to define Car_info
in my controller app/controllers/active_car_controller.rb:6:in `update'
Any help would be appreciated. New to RoR.
I see your controller method is named 'udpate' instead of 'update' - could that be your problem?
You need to change your query to:
@available_cars = Car_info.find_all_by_user_id(session[:user_id])
The find_all
part will get you all records, whereas find
only gets you the first. Another way to write this is:
@available_cars = Car_info.where("user_id = ?", session[:user_id])
Ideally, you want your class to be called CarInfo
, not Car_info
.
精彩评论