Ruby on rails I am a beginner.
We are currently constructing an EC site, and the one that stumbles is the process of "putting in the cart" using the session function.
Referenced site "https://qiita.com/kenzo-ta/items/b45994c5f3fdd87b6c50"
I checked the association many times, but the error persisted.
Is it due to another part?
(Postscript) → Isn't it an error because "current_cart" is empty?
→ Since "session[:cart_id" does not contain anything, "current_cart" becomes empty and an error occurs. (hypothesis)
→If the current_cart action does not have [:cart_id], it should be written so as to "create"? ?
Applicable source code
NoMethodError (undefined method `cart_items' for nil:NilClass): app/controllers/carts_controller.rb:32:in `set_line_item'
(app/vies/products/show.html.erb) <%= form_for(:session, url: carts_path) do |f|%> <%= f.label :quantity, "quantity"%> <%= f.select :quantity, [1,2,3,4,5,6,7,8,9,10]%> <%= f.submit "Add to cart", class:"btn cart_btn" %> <% end %>
(App/controllers/carts_controller) class CartsController</pre> <pre><code>(App/controllers/application_controller) private def current_cart if session[:cart_id] current_cart = Cart.find_by(id: session[:cart_id]) current_cart = Cart.create unless current_cart session[:cart_id] = current_cart.id current_cart end end
(App/models/product.rb) class Product
(App/models/cart_item.rb) class CartItem
What I tried
(App/models/cart.rb) class Cart
I thought it might be an association issue, and reconfirmed has_many and belong_to.Supplemental information (FW/tool version, etc.)
Answer # 1
undefined method `cart_items' for nil:NilClass
This error is caused by calling cart_items from nil.
So current_cart feels nil
(App/controllers/application_controller) private def current_cart if session[:cart_id] current_cart = Cart.find_by(id: session[:cart_id]) current_cart = Cart.create unless current_cart session[:cart_id] = current_cart.id current_cart end end
If session[:cart_id] does not exist in this part, it will work if you create a new one.
- ruby - nomethoderror: undefined method `new'for: uglifier: symbol error on heroku deployment
- ruby - improvement of nomethoderror undefined method `paginate'
- ruby - i get an error saying undefined method `resize_to_limit'for
- ruby - undefined method `build_address' for order (table doesn't exist): class send data to db
- ruby - # undefined method i want to resolve an error
- ruby - undefined method `model_name'for nil: i got an error in nilclass and want to know where the cause is
- ruby on rails 5 - undefined method `tomorrow'for 00: i would like to know how to solve bigdecimal
- ruby - undefined method `save'for nil: nilclass cannot be resolved
- centos - nomethoderror: undefined method `email'for nil: nilclass cannot be resolved
- ruby on rails 6 - i want to resolve undefined method `' for nil:nilclass
- ruby undefined method `find_zone!' for time:class (nomethoderror) error
- ruby on rails - undefined method `if'for ~
- ruby - undefined method `each'for nil: nilclass is displayed with no method error
- ruby on rails: undefined method ʻuser'for nil: about nomethoderror of nilclass
- ruby - undefined method `id'for nil: nilclass
- ruby on rails - undefined method `merge'for
- ruby on rails 6 - undefined method `id'for: article: symbol resolution in rails
- ruby on rails - undefined method `name' for nil:nilclass
- ruby - i want to eliminate the undefined method
- html - i would like to know how to calculate the average of the data
- ruby - i want to get it at intervals by searching using rails active record
- ruby - resource: the server responded with a status of 404 () error
- ruby - aws deployment the asset "" is not present in the asset pipeline cannot be resolved
- ruby - passing variables using render partial
- ruby - about associations with 4 models
- ruby - all rails gems are not installed
- ruby - output the total integer value held by each user
- ruby - i want to use the search method for active hash data
- ruby - destroy action does not respond