Ruby on rails 3.2 没有路由匹配{:action=>;show";,:controller=>;questions";,:id=>;nil}

Ruby on rails 3.2 没有路由匹配{:action=>;show";,:controller=>;questions";,:id=>;nil},ruby-on-rails-3.2,Ruby On Rails 3.2,Ruby:2.0.0p0,Rails:3.2.13 我的耙路线: questions GET /questions(.:format) questions#index POST /questions(.:format) questions#create new_question GET /questions/new(.:format) questions#new edit_question GET

Ruby:2.0.0p0,Rails:3.2.13
我的<代码>耙路线:

 questions GET    /questions(.:format)          questions#index
              POST   /questions(.:format)          questions#create
 new_question GET    /questions/new(.:format)      questions#new
edit_question GET    /questions/:id/edit(.:format) questions#edit
     question GET    /questions/:id(.:format)      questions#show
              PUT    /questions/:id(.:format)      questions#update
              DELETE /questions/:id(.:format)      questions#destroy  
     class QuestionsController < ApplicationController                                                                                                            

  def index                                                                                                                                                  
    @questions = Question.all                                                                                                                                
  end                                                                                                                                                        

  def show                                                                                                                                                   
    @question = Question.find(params[:id])                                                                                                                   
  end                                                                                                                                                        

  def new                                                                                                                                                    
    @question = Question.new                                                                                                                                 
  end                                                                                                                                                        

  def create                                                                                                                                                 
    @question = Question.new(params[:question])                                                                                                              
  #  @question.save!                                                                                                                                         
  #  flash[:notic] = 'Page saved'                                                                                                                            
  #  redirect_to :action => 'index'                                                                                                                          
  #  rescue ActiveRecord::RecordInvalid                                                                                                                      
    #    render  :action => 'new'                                                                                                                            
    respond_to do |format|                                                                                                                                   
      if @question.save                                                                                                                                      
        format.html  { redirect_to(@question,                                                                                                                
                                   :notice => 'question was successfully created.') }                                                                        
        format.json  { render :json => @question,                                                                                                            
                       :status => :created, :location => @question }                                                                                         
      else                                                                                                                                                   
        format.html  { render :action => "new" }                                                                                                             
        format.json  { render :json => @question.errors,                                                                                                     
                       :status => :unprocessable_entity }                                                                                                    
      end                                                                                                                                                    
    end                                                                                                                                                      
  end                                                                                                                                                        

  def edit                                                                                                                                                   
    @question = Question.find(params[:id])                                                                                                                   
  end     

def update                                                                                                                                                 
    @question = Question.find(params[:id])                                                                                                                   
#    if @question.save                                                                                                                                       
#      redirect_to(question_path(@question.id), :notice => t("success update"))                                                                              
#    else                                                                                                                                                    
#      render :action => "new"                                                                                                                               
#    end                                                                                                                                                     
    respond_to do |format|                                                                                                                                   
      if @question.update_attributes(params[:id])                                                                                                            
        format.html { redirect_to(@question,                                                                                                                 
                      notic: "Question #{@question.title} was successfully updated") }                                                                       
        format.json { head :no_content }                                                                                                                     
      else                                                                                                                                                   
        format.html { render action: "edit" }                                                                                                                
      end                                                                                                                                                    
    end                                                                                                                                                      
  end                                                                                                                                                        
end   
<div class="content">                                                                                                                                        
    <div class="box">                                                                                                                                        
        <%= render 'form' %>                                                                                                                                 
    </div>                                                                                                                                                   
</div>  
<%= simple_form_for @question do |f| %>                                                                                                                      
<fieldset>                                                                                                                                                   
    <legend><%= @question.new_record? ? t("question.create_topic") : t("questions.edit_topic") %></legend>                                                   
<%= f.input :title, :input_html => { :class => "span6" } %>                                                                                                  
<%= f.input :content, :as => :text, :input_html => { :class => "span6" } %>                                                                                  
<%= f.button :submit, :class => 'btn-primary' %>                                                                                                             
<%= link_to 'Cancel', @question.id.blank? ? questions_path : question_path(params[:question]), :class => "btn btn-danger" %>                                 
</fieldset>                                                                                                                                                  
<% end %> 
questionscottroller
: 类问题控制器<应用程序控制器

     class QuestionsController < ApplicationController                                                                                                            

  def index                                                                                                                                                  
    @questions = Question.all                                                                                                                                
  end                                                                                                                                                        

  def show                                                                                                                                                   
    @question = Question.find(params[:id])                                                                                                                   
  end                                                                                                                                                        

  def new                                                                                                                                                    
    @question = Question.new                                                                                                                                 
  end                                                                                                                                                        

  def create                                                                                                                                                 
    @question = Question.new(params[:question])                                                                                                              
  #  @question.save!                                                                                                                                         
  #  flash[:notic] = 'Page saved'                                                                                                                            
  #  redirect_to :action => 'index'                                                                                                                          
  #  rescue ActiveRecord::RecordInvalid                                                                                                                      
    #    render  :action => 'new'                                                                                                                            
    respond_to do |format|                                                                                                                                   
      if @question.save                                                                                                                                      
        format.html  { redirect_to(@question,                                                                                                                
                                   :notice => 'question was successfully created.') }                                                                        
        format.json  { render :json => @question,                                                                                                            
                       :status => :created, :location => @question }                                                                                         
      else                                                                                                                                                   
        format.html  { render :action => "new" }                                                                                                             
        format.json  { render :json => @question.errors,                                                                                                     
                       :status => :unprocessable_entity }                                                                                                    
      end                                                                                                                                                    
    end                                                                                                                                                      
  end                                                                                                                                                        

  def edit                                                                                                                                                   
    @question = Question.find(params[:id])                                                                                                                   
  end     

def update                                                                                                                                                 
    @question = Question.find(params[:id])                                                                                                                   
#    if @question.save                                                                                                                                       
#      redirect_to(question_path(@question.id), :notice => t("success update"))                                                                              
#    else                                                                                                                                                    
#      render :action => "new"                                                                                                                               
#    end                                                                                                                                                     
    respond_to do |format|                                                                                                                                   
      if @question.update_attributes(params[:id])                                                                                                            
        format.html { redirect_to(@question,                                                                                                                 
                      notic: "Question #{@question.title} was successfully updated") }                                                                       
        format.json { head :no_content }                                                                                                                     
      else                                                                                                                                                   
        format.html { render action: "edit" }                                                                                                                
      end                                                                                                                                                    
    end                                                                                                                                                      
  end                                                                                                                                                        
end   
\u form.html.erb

 questions GET    /questions(.:format)          questions#index
              POST   /questions(.:format)          questions#create
 new_question GET    /questions/new(.:format)      questions#new
edit_question GET    /questions/:id/edit(.:format) questions#edit
     question GET    /questions/:id(.:format)      questions#show
              PUT    /questions/:id(.:format)      questions#update
              DELETE /questions/:id(.:format)      questions#destroy  
     class QuestionsController < ApplicationController                                                                                                            

  def index                                                                                                                                                  
    @questions = Question.all                                                                                                                                
  end                                                                                                                                                        

  def show                                                                                                                                                   
    @question = Question.find(params[:id])                                                                                                                   
  end                                                                                                                                                        

  def new                                                                                                                                                    
    @question = Question.new                                                                                                                                 
  end                                                                                                                                                        

  def create                                                                                                                                                 
    @question = Question.new(params[:question])                                                                                                              
  #  @question.save!                                                                                                                                         
  #  flash[:notic] = 'Page saved'                                                                                                                            
  #  redirect_to :action => 'index'                                                                                                                          
  #  rescue ActiveRecord::RecordInvalid                                                                                                                      
    #    render  :action => 'new'                                                                                                                            
    respond_to do |format|                                                                                                                                   
      if @question.save                                                                                                                                      
        format.html  { redirect_to(@question,                                                                                                                
                                   :notice => 'question was successfully created.') }                                                                        
        format.json  { render :json => @question,                                                                                                            
                       :status => :created, :location => @question }                                                                                         
      else                                                                                                                                                   
        format.html  { render :action => "new" }                                                                                                             
        format.json  { render :json => @question.errors,                                                                                                     
                       :status => :unprocessable_entity }                                                                                                    
      end                                                                                                                                                    
    end                                                                                                                                                      
  end                                                                                                                                                        

  def edit                                                                                                                                                   
    @question = Question.find(params[:id])                                                                                                                   
  end     

def update                                                                                                                                                 
    @question = Question.find(params[:id])                                                                                                                   
#    if @question.save                                                                                                                                       
#      redirect_to(question_path(@question.id), :notice => t("success update"))                                                                              
#    else                                                                                                                                                    
#      render :action => "new"                                                                                                                               
#    end                                                                                                                                                     
    respond_to do |format|                                                                                                                                   
      if @question.update_attributes(params[:id])                                                                                                            
        format.html { redirect_to(@question,                                                                                                                 
                      notic: "Question #{@question.title} was successfully updated") }                                                                       
        format.json { head :no_content }                                                                                                                     
      else                                                                                                                                                   
        format.html { render action: "edit" }                                                                                                                
      end                                                                                                                                                    
    end                                                                                                                                                      
  end                                                                                                                                                        
end   
<div class="content">                                                                                                                                        
    <div class="box">                                                                                                                                        
        <%= render 'form' %>                                                                                                                                 
    </div>                                                                                                                                                   
</div>  
<%= simple_form_for @question do |f| %>                                                                                                                      
<fieldset>                                                                                                                                                   
    <legend><%= @question.new_record? ? t("question.create_topic") : t("questions.edit_topic") %></legend>                                                   
<%= f.input :title, :input_html => { :class => "span6" } %>                                                                                                  
<%= f.input :content, :as => :text, :input_html => { :class => "span6" } %>                                                                                  
<%= f.button :submit, :class => 'btn-primary' %>                                                                                                             
<%= link_to 'Cancel', @question.id.blank? ? questions_path : question_path(params[:question]), :class => "btn btn-danger" %>                                 
</fieldset>                                                                                                                                                  
<% end %> 

{:class=>“span6”}%>
:text,:input_html=>{:class=>“span6”}%>
“btn主节点“%”
“btn btn危险”%>
我可以访问localhost:3000/questions/1,但是当我访问localhost:3000/questions/1/edit时,会出现错误:
没有路由匹配{:action=>“show”,:controller=>“questions”,:id=>nil}


什么会导致这个问题?如果您需要更多信息,请告诉我。

只要更换这一行

     class QuestionsController < ApplicationController                                                                                                            

  def index                                                                                                                                                  
    @questions = Question.all                                                                                                                                
  end                                                                                                                                                        

  def show                                                                                                                                                   
    @question = Question.find(params[:id])                                                                                                                   
  end                                                                                                                                                        

  def new                                                                                                                                                    
    @question = Question.new                                                                                                                                 
  end                                                                                                                                                        

  def create                                                                                                                                                 
    @question = Question.new(params[:question])                                                                                                              
  #  @question.save!                                                                                                                                         
  #  flash[:notic] = 'Page saved'                                                                                                                            
  #  redirect_to :action => 'index'                                                                                                                          
  #  rescue ActiveRecord::RecordInvalid                                                                                                                      
    #    render  :action => 'new'                                                                                                                            
    respond_to do |format|                                                                                                                                   
      if @question.save                                                                                                                                      
        format.html  { redirect_to(@question,                                                                                                                
                                   :notice => 'question was successfully created.') }                                                                        
        format.json  { render :json => @question,                                                                                                            
                       :status => :created, :location => @question }                                                                                         
      else                                                                                                                                                   
        format.html  { render :action => "new" }                                                                                                             
        format.json  { render :json => @question.errors,                                                                                                     
                       :status => :unprocessable_entity }                                                                                                    
      end                                                                                                                                                    
    end                                                                                                                                                      
  end                                                                                                                                                        

  def edit                                                                                                                                                   
    @question = Question.find(params[:id])                                                                                                                   
  end     

def update                                                                                                                                                 
    @question = Question.find(params[:id])                                                                                                                   
#    if @question.save                                                                                                                                       
#      redirect_to(question_path(@question.id), :notice => t("success update"))                                                                              
#    else                                                                                                                                                    
#      render :action => "new"                                                                                                                               
#    end                                                                                                                                                     
    respond_to do |format|                                                                                                                                   
      if @question.update_attributes(params[:id])                                                                                                            
        format.html { redirect_to(@question,                                                                                                                 
                      notic: "Question #{@question.title} was successfully updated") }                                                                       
        format.json { head :no_content }                                                                                                                     
      else                                                                                                                                                   
        format.html { render action: "edit" }                                                                                                                
      end                                                                                                                                                    
    end                                                                                                                                                      
  end                                                                                                                                                        
end   
<%= link_to 'Cancel', @question.id.blank? ? questions_path : question_path(params[:question]), :class => "btn btn-danger" %>  
“btn btn危险”%>

     class QuestionsController < ApplicationController                                                                                                            

  def index                                                                                                                                                  
    @questions = Question.all                                                                                                                                
  end                                                                                                                                                        

  def show                                                                                                                                                   
    @question = Question.find(params[:id])                                                                                                                   
  end                                                                                                                                                        

  def new                                                                                                                                                    
    @question = Question.new                                                                                                                                 
  end                                                                                                                                                        

  def create                                                                                                                                                 
    @question = Question.new(params[:question])                                                                                                              
  #  @question.save!                                                                                                                                         
  #  flash[:notic] = 'Page saved'                                                                                                                            
  #  redirect_to :action => 'index'                                                                                                                          
  #  rescue ActiveRecord::RecordInvalid                                                                                                                      
    #    render  :action => 'new'                                                                                                                            
    respond_to do |format|                                                                                                                                   
      if @question.save                                                                                                                                      
        format.html  { redirect_to(@question,                                                                                                                
                                   :notice => 'question was successfully created.') }                                                                        
        format.json  { render :json => @question,                                                                                                            
                       :status => :created, :location => @question }                                                                                         
      else                                                                                                                                                   
        format.html  { render :action => "new" }                                                                                                             
        format.json  { render :json => @question.errors,                                                                                                     
                       :status => :unprocessable_entity }                                                                                                    
      end                                                                                                                                                    
    end                                                                                                                                                      
  end                                                                                                                                                        

  def edit                                                                                                                                                   
    @question = Question.find(params[:id])                                                                                                                   
  end     

def update                                                                                                                                                 
    @question = Question.find(params[:id])                                                                                                                   
#    if @question.save                                                                                                                                       
#      redirect_to(question_path(@question.id), :notice => t("success update"))                                                                              
#    else                                                                                                                                                    
#      render :action => "new"                                                                                                                               
#    end                                                                                                                                                     
    respond_to do |format|                                                                                                                                   
      if @question.update_attributes(params[:id])                                                                                                            
        format.html { redirect_to(@question,                                                                                                                 
                      notic: "Question #{@question.title} was successfully updated") }                                                                       
        format.json { head :no_content }                                                                                                                     
      else                                                                                                                                                   
        format.html { render action: "edit" }                                                                                                                
      end                                                                                                                                                    
    end                                                                                                                                                      
  end                                                                                                                                                        
end   
“btn btn危险”%>

需要将对象传递给路径帮助器,而不是字符串或整数。

您的
routes.rb
文件是什么样子的?
     class QuestionsController < ApplicationController                                                                                                            

  def index                                                                                                                                                  
    @questions = Question.all                                                                                                                                
  end                                                                                                                                                        

  def show                                                                                                                                                   
    @question = Question.find(params[:id])                                                                                                                   
  end                                                                                                                                                        

  def new                                                                                                                                                    
    @question = Question.new                                                                                                                                 
  end                                                                                                                                                        

  def create                                                                                                                                                 
    @question = Question.new(params[:question])                                                                                                              
  #  @question.save!                                                                                                                                         
  #  flash[:notic] = 'Page saved'                                                                                                                            
  #  redirect_to :action => 'index'                                                                                                                          
  #  rescue ActiveRecord::RecordInvalid                                                                                                                      
    #    render  :action => 'new'                                                                                                                            
    respond_to do |format|                                                                                                                                   
      if @question.save                                                                                                                                      
        format.html  { redirect_to(@question,                                                                                                                
                                   :notice => 'question was successfully created.') }                                                                        
        format.json  { render :json => @question,                                                                                                            
                       :status => :created, :location => @question }                                                                                         
      else                                                                                                                                                   
        format.html  { render :action => "new" }                                                                                                             
        format.json  { render :json => @question.errors,                                                                                                     
                       :status => :unprocessable_entity }                                                                                                    
      end                                                                                                                                                    
    end                                                                                                                                                      
  end                                                                                                                                                        

  def edit                                                                                                                                                   
    @question = Question.find(params[:id])                                                                                                                   
  end     

def update                                                                                                                                                 
    @question = Question.find(params[:id])                                                                                                                   
#    if @question.save                                                                                                                                       
#      redirect_to(question_path(@question.id), :notice => t("success update"))                                                                              
#    else                                                                                                                                                    
#      render :action => "new"                                                                                                                               
#    end                                                                                                                                                     
    respond_to do |format|                                                                                                                                   
      if @question.update_attributes(params[:id])                                                                                                            
        format.html { redirect_to(@question,                                                                                                                 
                      notic: "Question #{@question.title} was successfully updated") }                                                                       
        format.json { head :no_content }                                                                                                                     
      else                                                                                                                                                   
        format.html { render action: "edit" }                                                                                                                
      end                                                                                                                                                    
    end                                                                                                                                                      
  end                                                                                                                                                        
end