对象字符串正在Rails 4.1中打印为HTML流
我在Rails(4.1)中有几个简单的多态关系。地址和电话对象可以属于个人或组织对象。所有的管道似乎都正常工作-积垢操作都正常工作,我的所有测试都通过了。 个人和组织的显示操作都包括一个分部,该分部应显示相关的地址和电话。虽然这些字段显示正确,但整个Ruby对象也在HTML流中呈现为字符串,这当然是有问题的。 PeopleController的显示操作:对象字符串正在Rails 4.1中打印为HTML流,html,ruby,erb,ruby-on-rails-4.1,Html,Ruby,Erb,Ruby On Rails 4.1,我在Rails(4.1)中有几个简单的多态关系。地址和电话对象可以属于个人或组织对象。所有的管道似乎都正常工作-积垢操作都正常工作,我的所有测试都通过了。 个人和组织的显示操作都包括一个分部,该分部应显示相关的地址和电话。虽然这些字段显示正确,但整个Ruby对象也在HTML流中呈现为字符串,这当然是有问题的。 PeopleController的显示操作: def show @addressable = @person @addresses = @addressable.addre
def show
@addressable = @person
@addresses = @addressable.addresses
@phoneable - @person
@phones = @phoneable.phones
end
@person变量是在before_操作中使用私有函数设置的:
def set_person
@person = Person.find(params[:id])
end
用于个人的show.html.erb(仅地址和电话部分):
地址
each
返回您正在调用的内容each
因此:
@addresses.each { ... } == @addresses
用于插值,因此:
<%= @addresses.each do |address| %>
要评估(而不是插值)每个
谢谢您的捕获。我盯着代码看了这么久,我完全错过了显而易见的东西!
<h3>Addresses</h3>
1 Viceroy Plaza<br>
Suite 1300<br>
Office 125<br>
<br>
Toronto, C8R 9G3<br><br>
1313 Mockingbird Lane<br>
Apt. 227<br>
<br>
<br>
Mississauga, H3V 8Y4<br><br>
[#<Address id: 2, address_line_1: "1 Viceroy Plaza", address_line_2: "Suite 1300", address_line_3: "Office 125", address_line_4: nil, city: "Toronto", postal_code: "C8R 9G3", verified: false, use_for_contact: true, picture_file_name: nil, addressable_id: 1, addressable_type: "Person", created_at: "2014-04-13 20:32:41", updated_at: "2014-04-13 20:32:41">, #<Address id: 1, address_line_1: "1313 Mockingbird Lane", address_line_2: "Apt. 227", address_line_3: nil, address_line_4: nil, city: "Mississauga", postal_code: "H3V 8Y4", verified: false, use_for_contact: true, picture_file_name: nil, addressable_id: 1, addressable_type: "Person", created_at: "2014-04-13 20:18:56", updated_at: "2014-04-13 20:18:56">]
</br>
<h3>Phones</h3>
(1) 416-3219700<br><br>
(1) 289-4346789<br><br>
[#<Phone id: 2, country_code: "1", area_code: "416", phone_number: "3219700", phone_extension: "13125", verified: nil, use_for_contact: nil, phoneable_id: 1, phoneable_type: "Person", created_at: "2014-04-13 20:34:54", updated_at: "2014-04-13 20:34:54">, #<Phone id: 1, country_code: "1", area_code: "289", phone_number: "4346789", phone_extension: nil, verified: nil, use_for_contact: nil, phoneable_id: 1, phoneable_type: "Person", created_at: "2014-04-13 20:30:54", updated_at: "2014-04-13 20:30:54">]
@addresses.each { ... } == @addresses
<%= @addresses.each do |address| %>
<% @addresses.each do |address| %>