找不到<对象>无 ID 导轨 3.0.1

发布于 2024-10-11 03:29:05 字数 1121 浏览 4 评论 0原文

不幸的是,这是我几天来的第二篇文章。因此,该应用程序在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高冷爸爸 2024-10-18 03:29:05

有两个问题。

  1. 您试图在实际上应该资源丰富的路线上使用非资源丰富的路线。
  2. 看起来您正在尝试将 /show2 发送到名为 hospitals 的控制器,而您的操作实际上是在 fathers 控制器上指定的。

这应该可以解决问题:

resources :fathers do
  get :show2, :on => :member
  resources :kids
end

您也可以将上面的内容写为:

resources :fathers do
  member do
    get :show2
  end

  resources :kids
end    

There are two problems.

  1. You're trying to use a non-resourceful route on a route that actually should be resourceful.
  2. It looks like you're trying to send /show2 to a controller named hospitals, when your action is actually specified on the fathers controller.

This should do the trick:

resources :fathers do
  get :show2, :on => :member
  resources :kids
end

You can also write the above as:

resources :fathers do
  member do
    get :show2
  end

  resources :kids
end    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文