找不到=“你的问题”

时间:2014-07-26 02:08:58

标签: ruby-on-rails ruby

我的错误说:

 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'

1 个答案:

答案 0 :(得分:0)

你提到你有这条路线:

get '/questions/your_questions', to: 'questions#your_questions

如果您还有类似遵循宁静风格的路线,您还应该拥有以下内容:

get 'questions/:id', to: 'questions#your_questions'

或资源调用。无论如何,所以Rails实际上是试图访问你的节目动作传递&#34; your_questions&#34;作为路线的id。写这样的路线:

 get '/questions/:id', to: 'questions#show

这意味着:&#34;如果使用GET HTTP方法的请求在url&#39; questions /:id&#39;之后,则转到控制器:问题及其操作(控制器中的方法),称为: your_questions&#34;并将URL的值传递给params hash。