2013-05-07 39 views
2

紅寶石:2.0.0p0,Rails的:3.2.13
rake routes沒有路由匹配{:動作=> 「秀」,:控制器=> 「問題」:ID =>零}

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: 類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 %> 

,我可以訪問本地主機:3000 /問題/ 1,但是當我訪問本地主機:3000 /問題/ 1 /編輯,說到錯誤:No route matches {:action=>"show", :controller=>"questions", :id=>nil}
這個問題可能會發生什麼?如果您需要更多信息,請告訴我。

+0

你的'routes.rb'文件是什麼樣的? – lightswitch05 2013-05-07 16:22:34

回答

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

需要一個對象傳遞給路徑幫手,而不是字符串或整數。

相關問題