没有路线匹配{:action =>“show”,:controller =>“questions”,:id => nil}

时间:2013-05-07 16:18:36

标签: ruby-on-rails-3.2

Ruby:2.0.0p0,Rails:3.2.13
我的rake routes

 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  

QuestionsCotroller: class QuestionsController< ApplicationController

     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   

edit.html.erb

<div class="content">                                                                                                                                        
    <div class="box">                                                                                                                                        
        <%= render 'form' %>                                                                                                                                 
    </div>                                                                                                                                                   
</div>  

_form.html.erb

<%= 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 %> 

我可以访问 localhost:3000 / questions / 1 ,但是当我访问 localhost:3000 / questions / 1 / edit 时,会出现错误:{{ 1}}。
有什么可以解决这个问题?如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:6)

只需替换此行,

<%= link_to 'Cancel', @question.id.blank? ? questions_path : question_path(params[:question]), :class => "btn btn-danger" %>  

通过

<%= link_to 'Cancel', @question.id.blank? ? questions_path : question_path(@question), :class => "btn btn-danger" %>  

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