我正在构建一个具有语言模型,问题模型和答案模型的应用程序。我是新来的编程和运行到以下错误,当我尝试点击一个链接,从问题的索引页面中的“新问题”的形式: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>
我已经花了大量时间试图解决通过研究和反复试验的错误,但我不知所措。任何帮助将非常感激。如果您需要查看其他文件,请告诉我。 谢谢!
谢谢@GeorgeMillo!我选择了2号选项,它的功能就像一个魅力!我非常感谢你的帮助! – 2014-10-12 16:11:34