line_items_controller.rb def create @cart = current_cart product = Product.find(params[:product_id]) @line_item = @cart.line_items.build(product_id: product.id)
respond_to do |format| if @line_item.save format.html { redirect_to @line_item.cart, notice: 'Line item was successfully created.' } format.json { render json: @line_item, status: :created, location: @line_item } else format.html { render action: "new" } format.json { render json: @line_item.errors, status: :unprocessable_entity } end end end
carts_controller.rb
def show @cart = Cart.find(params[:id])
respond_to do |format| format.html # show.html.erb format.json { render json: @cart } end end app/views/carts/show.html.erb
<%= notice %>
<%= link_to 'Edit', edit_cart_path(@cart) %> | <%= link_to 'Back', carts_path %>
<%= button_to 'Add to Cart', line_items_path(product_id: product) %>
问题: button_to 到 line_items#create,然后 redirect_to "carts#show" 最后到 show.html.erb
出错:undefined method `title' for nil:NilClass