2

红宝石: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: 类 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时,会出现 error: No route matches {:action=>"show", :controller=>"questions", :id=>nil}
什么课程可能会出现这个问题?如果您需要更多信息,请告诉我。

4

1 回答 1

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

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

于 2013-05-07T16:27:21.627 回答