找不到<对象>无 ID 导轨 3.0.1对象>
不幸的是,这是我几天来的第二篇文章。因此,该应用程序在 mysql 和 Rails 3.0.3 上运行良好,但我发现我需要使用 MSSQL,所以我不得不将 Rails 降级到 3.0.1。
简而言之,我将 show.html.erb 复制为 show2.html.erb 并创建了一个新方法,它是 show 方法的副本。然后我创建了一个路线匹配。
我的控制器
class fathersController < ApplicationController
def show
@father= Father.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @father}
end
end
def show2
@father= Father.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @father}
end
end
end
routes.rb
resources :fathers do
match '/show2' => 'fathers#show2'
resources :kids
end
当我调用时,
http://127.0.0.1:3000/father/1
我得到了显示视图,但是当我调用时,
http://127.0.0.1:3000/father/1/show2
出现以下错误
Couldn't find father without an ID
请求参数返回,
{"father_id"=>"1"}
所以我知道问题是应用程序将 id 作为father_id 传递,但我该如何修复它?任何帮助将不胜感激。
Unfortunately, this is my second post in as many days. So the application worked fine with mysql and rails 3.0.3 but I found out that I needed to use MSSQL so I had to downgrade rails to 3.0.1.
In a nutshell, I copied the show.html.erb as show2.html.erb and created a new method which is a copy of the show method. Then I created a route match.
my controller
class fathersController < ApplicationController
def show
@father= Father.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @father}
end
end
def show2
@father= Father.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @father}
end
end
end
routes.rb
resources :fathers do
match '/show2' => 'fathers#show2'
resources :kids
end
when I call
http://127.0.0.1:3000/father/1
I get the show view but when I call
http://127.0.0.1:3000/father/1/show2
I get the following error
Couldn't find father without an ID
The request Parameters come back as
{"father_id"=>"1"}
so I know that the problem is that the app is passing the id as father_id but how do I fix it? Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两个问题。
/show2
发送到名为hospitals
的控制器,而您的操作实际上是在fathers
控制器上指定的。这应该可以解决问题:
您也可以将上面的内容写为:
There are two problems.
/show2
to a controller namedhospitals
, when your action is actually specified on thefathers
controller.This should do the trick:
You can also write the above as: