【发布时间】:2015-04-19 20:04:24
【问题描述】:
我有一个 User 、 Boat 和 Picture 模型。 User has_many :boats,船belongs_to :user。还有船has_many :pictures,图片belongs_to :boat,很清楚。
我的问题是,我没有嵌套用户和船模型的路线,所以当用户点击添加新船链接时,url变成...boats/new.2(2作为id = 2的用户)。所以现在我有了图片模型,船主可以上传图片。我不知道如何创建这些图片。我在检索船 ID 时遇到了麻烦。
当我写<%= 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) %> 以获取船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
给出一个错误,它没有看到表列名。但是当我写Picture.last.name。有用。我认为关联有问题。但一切似乎都很好。
这里boat_id 是nil。
> 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:
好的。我通过破坏图片模型并创建新模型来解决关联问题。但我仍然不知道如何才能到达 Boat id。
【问题讨论】:
-
看起来您正在使用 current_user 而不是 value(id) 用于现有的船对象,这就是为什么您无法获得 @boat 的结果的原因。这里没有足够的细节来提供完整的答案。
-
是的,因为我不知道如何获得船的 ID。你想学什么?我会编辑问题
-
显示你的
config/routes.rb
标签: ruby-on-rails controller routes nested