我的错误是:
Couldn't find Question with 'id'=your_questions"
and
ActiveRecord::RecordNotFound in QuestionsController#show
我应该怎么做才能修复它?
def show
@question = Question.find(params[:id])
@answer = Answer.new
end
在第二行它指出了错误所在。
编辑:
索引 View 文件
<%= form_for(@question) do |f| %>
<%= render 'common/form_errors', object: @question %>
<p>
<%= f.label :body, "Question" %><br />
<%= f.text_field :body %>
<%= f.submit "Ask a Question" %>
</p>
<% end %>
Rails.application.routes.draw do
get "/" => "main_app#index"
get "/location" => "location#location"
post "/location/index" => "location#index"
get "/location/index" => "location#index"
get "/location/directions" => "location#directions"
root to: 'questions#index'
get '/logout', to: 'sessions#destroy', via: :delete
resources :users, only: [:new, :create]
resources :sessions, only: [:new, :create]
resources :questions, except: [:new] do
resources :answers, only: [:create]
end
get '/register', to: 'users#new'
get '/login', to: 'sessions#new'
get '/logout', to: 'sessions#destroy', via: :delete
get '/questions/:id', to: 'questions#your_questions'
get '/search', to: 'questions#search'
最佳答案
你提到你有这条路线:
get '/questions/your_questions', to: 'questions#your_questions
如果你也有像following the restful style这样的路线,你也应该有这样的东西:
get 'questions/:id', to: 'questions#your_questions'
或者资源调用。不管怎样,Rails 实际上是在尝试通过“your_questions”作为路线的 id 来访问您的 show Action 。像这样写这条路线:
get '/questions/:id', to: 'questions#show
这意味着:“如果使用 GET HTTP 方法的请求遵循 url 'questions/:id',则转到 Controller :questions 及其操作( Controller 中的方法)称为:your_questions”并传递到参数哈希URL 中 id 的值。
关于ruby-on-rails - 无法找到 ID ="your_question",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24966833/