1
我有這樣的錯誤與我的鏈接在我的意見Rails的無路由匹配缺少必需的鍵:[:ID]
HTML
<% if logged_in? %>
<%=link_to "View Your Cart", cart_path(@cart)%>
<% end %>
我的路線
resources :users
resources :parts
resources :carts
resources :categories
resources :line_items
我有這個方法在這裏爲用戶指定購物車
def set_cart
@cart = Cart.find_by(id: session[:cart_id], user: session[:user_id])
rescue ActiveRecord::RecordNotFound
@cart = Cart.create
session[:cart_id] = @cart.id
end
這是我的會話控制器
def new
@user = User.new
end
def create
if params[:provider] == "facebook"
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.id
redirect_to root_path
else
@user = User.find_by(email: params[:user][:email])
@user = User.new if @user.blank?
if @user && @user.authenticate(params[:user][:password])
session[:user_id] = @user.id
@cart = Cart.create
@user.cart = @cart.id
@user.save
redirect_to @user
else
flash[:notice] = "Failed to login, please try again"
render 'new'
end
end
end
def destroy
session[:user_id] = nil
redirect_to root_url
end
end
這是我的車控制器
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :edit, :update, :destroy]
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
def show
@cart = Cart.find(params[:id])
end
def edit
@cart = Cart.new(cart_params)
end
def update
@cart = Cart.find(params[:id])
if @cart.update_attributes(cart_params)
redirect_to @cart
end
end
def destroy
@cart.destroy if @cart.id == session[:cart_id]
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to root_path }
format.json { head :no_content }
end
end
private
def cart_params
params.require(:cart).permit(:user_id)
end
def invalid_cart
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to root_path, notice: "Invalid cart"
end
end
以下錯誤 「無路由匹配{:動作=>」 秀」,:控制器=> 「大車」, :id => nil}缺少必需的鍵:[:id]「在用戶登錄其帳戶時上升。我想要的是,用戶在登錄時(在佈局視圖中)有一個「查看您的購物車鏈接」,以便他們可以在任何地方查看購物車。然而,一旦他們登錄,這個錯誤就會升起。任何幫助這個人都會很感激,我很樂意提供更多的信息。
嘗試'redirect_to user_path(@user)' –
爲link_to「查看您的購物車」??? – Dan