Ruby on rails 找不到Id=";你的“问题”;
我的错误是:Ruby on rails 找不到Id=";你的“问题”;,ruby-on-rails,ruby,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_fo
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'
Rails.application.routes.draw do
获取“/”=>“主应用程序索引”
获取“/location”=>“location#location”
post“/位置/索引”=>“位置#索引”
获取“/位置/索引”=>“位置#索引”
获取“/位置/方向”=>“位置#方向”
根目录为:“问题#索引”
通过::delete将“/logout”获取到:“会话#销毁”
资源:用户,仅:[:新建,:创建]
资源:会话,仅:[:新建,:创建]
参考资料:问题,除了:[:new]do
资源:答案,仅限:[:创建]
结束
获取“/register”至:“users#new”
获取“/login”至:“会话#新建”
通过::delete将“/logout”获取到:“会话#销毁”
获取“/questions/:id”,至:“问题#你的问题”
获取'/search',至:'问题#搜索'
您提到您有以下路线:
get '/questions/your_questions', to: 'questions#your_questions
如果您还有一个类似于遵循restful样式的路由,那么您还应该有如下内容:
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的值传递到参数散列中。所以我用你给我的代码替换了我现在的id?它不起作用,它弄乱了我的另一条路径:(这就是为什么我需要查看你的路线。你不能用路线的内容编辑帖子吗?它不允许我,因为idk如何编辑代码?我可以发电子邮件给你吗?让我们吧。)。