嵌套模型路由设计

Nested model Routes design

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

我的问题是,我没有嵌套 User 和 Boat 模型的路线,所以当用户点击添加新船 link 时,url 变成 ...boats/new.2 (2作为 id = 2 的用户)。所以现在我有了图片模型,船主可以上传图片。我不知道如何创建这些图片。我在检索船 ID 时遇到了麻烦。

当我写<%= link_to "New Picture", pictures_new_path(current_user) %> url 变为 ...pictures/new.2。似乎它将保存到 ID 为 2 的用户。这就是为什么 Boat.last.pictures.last.name 例如 return nil。我也无法在 #create 操作中获得船 ID。

这里是#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) %> 以获取船 ID 或嵌套路线。我很困惑。

编辑:

这是我的用户模型

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 给出错误,它没有看到 table 列名。但是当我写 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_idnil

> 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:

好的。我通过破坏图片模型并创建新模型来解决关联问题。但是我还是不知道怎么才能找到Boat id。

您应该将路线更改为:

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

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