我的错误是:
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在第二行,它指出了错误所在。
编辑:
索引视图文件
<%= 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'发布于 2014-07-26 11:35:01
你提到你有这条路线:
get '/questions/your_questions', to: 'questions#your_questionsget 'questions/:id', to: 'questions#your_questions'或者资源调用。不管怎样,Rails实际上是在尝试通过传递"your_questions“作为路由的id来访问您的show操作。像这样写下这条路径:
get '/questions/:id', to: 'questions#show这意味着:“如果使用GET HTTP方法的请求遵循URL ' questions /: id ',则转到控制器:questions及其名为:your_questions的操作(控制器中的方法)”,并将id的值散列到参数中。
https://stackoverflow.com/questions/24966833
复制相似问题