2014-10-12 78 views
0

我正在构建一个具有语言模型,问题模型和答案模型的应用程序。我是新来的编程和运行到以下错误,当我尝试点击一个链接,从问题的索引页面中的“新问题”的形式:ActiveRecord :: RecordNotFound at/questions/new无法找到没有ID的语言

ActiveRecord::RecordNotFound at /questions/new 
Couldn't find Language without an ID 
/controllers/questions_controller.rb line 81 at @language = Language.find(params[:language_id]) 
Request parameters 
{"action"=>"new", "controller"=>"questions"} 
routes nil 

这里是我的路线:

MyApp::Application.routes.draw do 
    devise_for :users 
    resources :users 

    resources :languages do 
    resources :questions, except: [:index] do 
     resources :answers 
    end 
    end 

    resources :questions, only: [:index, :new] 

    get 'about' => 'welcome#about' 
    root to: 'welcome#index' 
    end 

这里是我的questions_controller.rb:

class QuestionsController < ApplicationController 
    before_action :set_language, except: [:index] 
    before_action :set_question, only: [:show, :edit, :update, :destroy] 

    def index 
    @questions = current_user.questions 
    end 

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

    def new 
    @question = Question.new 
    authorize @question 
    end 

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

    def update 
    @question = Question.find(params[:id]) 
    authorize @question 

    if @question.update_attributes(question_params) 
     flash[:notice] = "Question was updated." 
     redirect_to [@language, @question] 
    else 
     flash[:error] = "There was an error saving the question. Please try again." 
     render :edit 
    end 
    end 

    def create 
    @question = Question.new(question_params) 
    @question.language = @language 
    @question.user = current_user 
    authorize @question 

    if @question.save 
     flash[:notice] = "Question was saved." 
     redirect_to [@language, @question] 
    else 
     flash[:error] = "There was an error saving the question. Please try again." 
     render :new 
    end 
    end 
    def destroy 
    @question.destroy.find(params[:id]) 
    respond_to do |format| 
     format.html { redirect_to questions_url } 
     format.json { head :no_content } 
    end 
    end 

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

    def question_params 
     params.require(:question).permit(:id, :body) 

    end 

    def set_language 
     @language = Language.find(params[:language_id]) 
    end 

end 

这里是我的问题型号:

class Question < ActiveRecord::Base 
     require 'obscenity/active_model' 
     has_many :answers, dependent: :destroy 
     accepts_nested_attributes_for :answers, :reject_if => :all_blank, :allow_destroy=>true 
     belongs_to :user 
     belongs_to :language 

     default_scope { order('created_at DESC') } 


     validates :body, obscenity: true 
     validates :body, length: { minimum: 10, maximum: 160 }, presence: true 
     validates :user, presence: true 

    end 

这里是我的问题,索引视图:

<div class="row"> 
    <div class="col-md-8"> 
     <h3>My Questions</h3> 
     <% @questions.each do |question| %> 
     <div class="media"> 
      <div class="media-body"> 
      <h4 class="media-heading"> 
     <p><%= question.language ? link_to(question.body, [question.language, question]) : question.body %></p> 
      </h4> 
      <small> 
       submitted <%= time_ago_in_words(question.created_at) %> ago by <%= question.user.name %> <br/> 
       <%= question.answers.count %> Answers 
      </small> 
      </div> 
     </div> 
     <% end %> 
     </div> 
     <br/> 
     <br/> 
    <div class="col-md-4"> 
     <%= link_to "Ask a New Question", new_question_path, class: 'btn btn-success' %> 
    </div> 
</div> 

我已经花了大量时间试图解决通过研究和反复试验的错误,但我不知所措。任何帮助将非常感激。如果您需要查看其他文件,请告诉我。 谢谢!

回答

1

问题出在QuestionController的set_language方法中。

Language.find(params[:language_id]),因为您所访问的网址/questions/new,其中不包含语言的任何信息产生错误,所以params[:language_id]等于nil。所以你的代码试图找到一个ID为nil的语言,这个语言不存在 - 所以会产生一个错误。

如果您运行的任务rake routes(或Rails中的较新版本浏览localhost:3000/rails/info/routes),你可以看到有关config/routes.rb产生的路由的详细信息 - 在你的情况,你实际上有两个途径来创建一个新的问题:

/languages/:language_id/questions/new new_language_question_path /questions/new new_question_path

这句话的意思是,如果你访问questions/new然后params[:language_id]将等于nil,这是什么导致你的错误(因为你试图找到ID为零,不存在语言。 )如果你vis它/languages/1/questions/new,然后params[:language_id]将等于1,这应该很好,假设实际存在ID为1的语言。

这取决于你的应用程序的细节(应该在“添加新问题”链接把用户创建一个新的问题特定语言?还是应创建一个新的问题,一般的网页,其中可能会或可能不会有语言),但有两个解决方案,您的错误可能是:

  1. 变化link_to ..., new_question_pathlink_to ..., new_language_question_path(language),其中language是你想要的链接,创建一个新的问题,任何一种语言

  2. 更改before_action :set_language, except: [:index]before_action :set_language, except: [:index, :new]QuestionsController ...所以你不要试图拨打Language.find没有ID。(你会那么需要确保有在/app/views/questions/new.html.erb@language没有提及,因为变量将不会被设置。)

我希望这是明确的!告诉我你是否需要更多帮助。

+0

谢谢@GeorgeMillo!我选择了2号选项,它的功能就像一个魅力!我非常感谢你的帮助! – 2014-10-12 16:11:34

相关问题