Ruby on rails 3 路由错误-尝试访问新路径时没有匹配的路由
链接到新的\u deck\u路径时,我收到以下错误Ruby on rails 3 路由错误-尝试访问新路径时没有匹配的路由,ruby-on-rails-3,rails-routing,Ruby On Rails 3,Rails Routing,链接到新的\u deck\u路径时,我收到以下错误 No route matches {:action=>"show", :controller=>"decks", :id=>#<Deck id: nil, name: nil, created_at: nil, updated_at: nil, user_id: 3>} C.U控制器.rb class DecksController < ApplicationController before_filt
No route matches {:action=>"show", :controller=>"decks", :id=>#<Deck id: nil, name: nil, created_at: nil, updated_at: nil, user_id: 3>}
C.U控制器.rb
class DecksController < ApplicationController
before_filter :authenticate
def index
@decks = current_user.decks
end
def new
@deck = current_user.decks.new
end
def show
@deck = find_deck
@cards = @deck.cards
end
def create
@deck = current_user.decks.new(params[:deck])
@deck.save
redirect_to decks_path
end
def edit
@deck = find_deck
end
def update
@deck = find_deck
@deck.update_attributes(params[:deck])
# redirect_to @deck <-- Polymorphic path
redirect_to deck_path(@deck)
end
def destroy
@deck = find_deck
@deck.destroy
redirect_to decks_path
end
private
def find_deck
current_user.decks.find(params[:id])
end
end
class ApplicationController < ActionController::Base
protect_from_forgery
def current_user
if cookies[:user_id]
user_id = cookies.signed[:user_id]
User.find_by_id(user_id)
else
User.find(session[:user_id]) if session[:user_id]
end
end
helper_method :current_user
end
class SessionsController < ApplicationController
def new
end
def create
if !request.env["omniauth.auth"].nil?
auth = request.env["omniauth.auth"]
user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
session[:user_id] = user.id
else
user = User.find_by_email(params[:session][:email])
sign_in(user)
cookies.signed[:user_id] = user.id
end
redirect_to decks_path
end
def destroy
if cookies[:user_id].nil?
session[:user_id] = nil
else
cookies.delete(:user_id)
end
redirect_to root_path, notice: "Signed out!"
end
end
应用程序\u controller.rb
class DecksController < ApplicationController
before_filter :authenticate
def index
@decks = current_user.decks
end
def new
@deck = current_user.decks.new
end
def show
@deck = find_deck
@cards = @deck.cards
end
def create
@deck = current_user.decks.new(params[:deck])
@deck.save
redirect_to decks_path
end
def edit
@deck = find_deck
end
def update
@deck = find_deck
@deck.update_attributes(params[:deck])
# redirect_to @deck <-- Polymorphic path
redirect_to deck_path(@deck)
end
def destroy
@deck = find_deck
@deck.destroy
redirect_to decks_path
end
private
def find_deck
current_user.decks.find(params[:id])
end
end
class ApplicationController < ActionController::Base
protect_from_forgery
def current_user
if cookies[:user_id]
user_id = cookies.signed[:user_id]
User.find_by_id(user_id)
else
User.find(session[:user_id]) if session[:user_id]
end
end
helper_method :current_user
end
class SessionsController < ApplicationController
def new
end
def create
if !request.env["omniauth.auth"].nil?
auth = request.env["omniauth.auth"]
user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
session[:user_id] = user.id
else
user = User.find_by_email(params[:session][:email])
sign_in(user)
cookies.signed[:user_id] = user.id
end
redirect_to decks_path
end
def destroy
if cookies[:user_id].nil?
session[:user_id] = nil
else
cookies.delete(:user_id)
end
redirect_to root_path, notice: "Signed out!"
end
end
class ApplicationController
会话\u controller.rb
class DecksController < ApplicationController
before_filter :authenticate
def index
@decks = current_user.decks
end
def new
@deck = current_user.decks.new
end
def show
@deck = find_deck
@cards = @deck.cards
end
def create
@deck = current_user.decks.new(params[:deck])
@deck.save
redirect_to decks_path
end
def edit
@deck = find_deck
end
def update
@deck = find_deck
@deck.update_attributes(params[:deck])
# redirect_to @deck <-- Polymorphic path
redirect_to deck_path(@deck)
end
def destroy
@deck = find_deck
@deck.destroy
redirect_to decks_path
end
private
def find_deck
current_user.decks.find(params[:id])
end
end
class ApplicationController < ActionController::Base
protect_from_forgery
def current_user
if cookies[:user_id]
user_id = cookies.signed[:user_id]
User.find_by_id(user_id)
else
User.find(session[:user_id]) if session[:user_id]
end
end
helper_method :current_user
end
class SessionsController < ApplicationController
def new
end
def create
if !request.env["omniauth.auth"].nil?
auth = request.env["omniauth.auth"]
user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
session[:user_id] = user.id
else
user = User.find_by_email(params[:session][:email])
sign_in(user)
cookies.signed[:user_id] = user.id
end
redirect_to decks_path
end
def destroy
if cookies[:user_id].nil?
session[:user_id] = nil
else
cookies.delete(:user_id)
end
redirect_to root_path, notice: "Signed out!"
end
end
class sessioncontroller
我认为这会导致错误,对吗
<li class="active"><%= link_to "Back to #{@deck.name}", deck_path(@deck) %></li>
您尝试使用
nil
id链接以显示操作@deck正在生成错误,因为它尚未保存。我通过将content_for helper方法放置在视图中而不是布局中解决了这个问题
<% content_for :nav do %>
<li class="active"><%= link_to "New Deck", new_deck_path %></li>
<% end %>
<div class="masthead">
<ul class="nav nav-pills pull-right">
<% if signed_in? %>
<%= yield :nav %>
<% else %>
<li><%= link_to "Sign in with Twitter", "/auth/twitter" %></li>
<li><%= link_to "Sign in", new_session_path %></li>
<li><%= link_to "Sign up", new_user_path %></li>
<% end %>
</ul>
<a href="<%= root_path %>"><h3 class="muted offset1">Flashy</h3></a>
</div>
这似乎不是问题所在。我删除了该行,但它仍然产生错误。而且,这句话永远也不会让他明白。if/else语句停止在: