link_to显示具有关联ID的操作

时间:2014-08-07 19:35:09

标签: ruby-on-rails

我有一个模特:

class RoomType < ActiveRecord::Base
  has_many :rooms
end

class Room < ActiveRecord::Base
  belongs_to :room_type
end

rooms/show.html.erb我想链接到房间类型:

<%= link_to(room_types_path(:code => @room.type_code)

  

http://test.com/room_types?code=MMC

所需的输出是:

  

http://test.com/types/:id

1 个答案:

答案 0 :(得分:1)

您的path应该是:

types_path(@room.type)

所以你的链接将成为:

<%= link_to "Room Types", types_path(@room.type) %>