嵌套模型路线设计

时间:2015-04-19 20:04:25

标签: ruby-on-rails controller routes nested

我有一个User,Boat and Picture模型。 User has_many :boats,船belongs_to :user。还有船has_many :pictures,图片belongs_to :boat,非常清楚。

我的问题是,我没有使用User和Boat模型嵌套路线,因此当用户点击添加新的船只链接时,网址变为...boats/new.2(2为id = 2的用户)。所以现在我有船主可以上传图片的图片模型。我不知道如何制作这些照片。我有麻烦来取回船名。

当我写<%= link_to "New Picture", pictures_new_path(current_user) %>时 网址变为...pictures/new.2。它似乎会保存到ID为2的用户。这就是Boat.last.pictures.last.name例如返回nil的原因。我无法在#create行动中获得船名。

以下是#create

def create
    @boat = Boat.find(params[:id]) #Here does not work, can't get boat id
    @picture = Picture.new(picture_params) if logged_in?
    if @picture.save
      #flash[:success] = "Continue from here"
      render 'show'
    else
      render 'new' 
    end
  end

所以我应该以某种方式改变<%= link_to "New Picture", pictures_new_path(current_user) %>来获取船只,或者筑巢路线。我很困惑。

编辑:

这是我的用户模型

class User < ActiveRecord::Base
   has_many :boats, dependent: :destroy
end

这是船模型

class Boat < ActiveRecord::Base
  belongs_to :user
  has_many :pictures
end

这是图片模型

class Picture < ActiveRecord::Base
  belongs_to :boat
end

我也无法从船上看到照片。这样; Boat.last.pictures.last.name 给出错误,它没有看到表列名。但是当我写Picture.last.name时。有用。我认为协会存在问题。但一切似乎都很好。

此处boat_idnil

> Boat.last
  Boat Load (0.2ms)  SELECT  "boats".* FROM "boats"  ORDER BY "boats"."id" DESC LIMIT 1
 => #<Boat id: 92, model: "LAGOON HERON 15C/FO [15']", year: "2004", brand: "A & M Manufacturing Inc", created_at: "2015-04-19 20:27:21", updated_at: "2015-04-19 20:27:21", user_id: 2, captained: nil, boat_type: "Power", daily_price: nil, boat_length: nil, listing_tagline: nil, listing_description: nil, boat_category: nil, hull_material: nil, mast_material: nil> 

> Picture.last
  Picture Load (0.5ms)  SELECT  "pictures".* FROM "pictures"  ORDER BY "pictures"."id" DESC LIMIT 1
 => #<Picture id: 5, name: "Something!", created_at: "2015-04-19 20:35:38", updated_at: "2015-04-19 20:35:38", boat_id: nil> 

此处boat_id也是nil

> Boat.last.pictures.last
  Boat Load (0.4ms)  SELECT  "boats".* FROM "boats"  ORDER BY "boats"."id" DESC LIMIT 1
  Picture Load (0.4ms)  SELECT  "pictures".* FROM "pictures" WHERE "pictures"."boat_id" = ?  ORDER BY "pictures"."id" DESC LIMIT 1  [["boat_id", 93]]
 => nil 

编辑2:

以下是routes.rb

 Rails.application.routes.draw do
      get 'pictures/new'
      get 'pictures/show'
      get 'pictures/edit'
      post 'pictures/create'

      get 'boats/update_years', :as => 'update_years'
      get 'boats/update_models', :as => 'update_models'
      get 'boats/new'
      resources :boats,               only: [:new, :create, :edit, :update]
      resources :users
    ......
    end

编辑3:

我得到了这个错误。我认为这种关联在某种程度上是错误的

> Boat.last.pictures.last.name
  Boat Load (0.5ms)  SELECT  "boats".* FROM "boats"  ORDER BY "boats"."id" DESC LIMIT 1
  Picture Load (0.1ms)  SELECT  "pictures".* FROM "pictures" WHERE "pictures"."boat_id" = ?  ORDER BY "pictures"."id" DESC LIMIT 1  [["boat_id", 93]]
NoMethodError: undefined method `name' for nil:NilClass

编辑4:

行。我通过破坏图片模型和创建新图片来解决关联问题。但我仍然不知道如何达到船只身份。

1 个答案:

答案 0 :(得分:1)

您应该将路线更改为:

    resources :users
    resources :boats, except: :destroy do
      get 'update_years'
      get 'update_models'
      resources: :pictures
    end

您还可以将整个resources :boats - 标记添加到用户资源的do-end-block中。