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