Ruby on Rails 4 渲染部分不渲染
Ruby on Rails 4 render partial does not render
几个月来我一直在尝试将旧的 Rails 2.X 网页升级到 Rails 4。
我仍在为一些局部渲染视图而苦苦挣扎。所以我想我需要一些帮助。如果对某人有帮助,我已经保存了我所有的 upgrade notes。但是这个我想不通。
目前的问题是下面的代码只打印地址表(见评论)。
views/users/_seller.erb:
<table class="labels">
<tr>
<td>{Seller type}</td>
<td><%= seller.select(:stype, ["producer","distributor","store"]) %></td>
</tr>
(...)
<h3>{Store/Pickup address}</h3>
<% seller.fields_for :address do |address_fields| %>
<!-- THIS IS NOT RENDERED!! -->
<table class="labels">
<tr>
<td>{Address Line 1}</td>
<td><%= address_fields.text_field :line1 %></td>
</tr>
<!-- UP TO THERE IS NOT RENDERED!! -->
<% end %>
这个局部视图被调用:
views/users/create_seller.html.erb:
<h1>{Before you sell a product, please enter your seller information}</h1>
<%= form_for @seller, :url => "/seller/create" do |f| %>
<h2>form</h2>
<% @seller.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
<%= render partial: "seller", locals: {seller:f} %>
<%= f.submit "Submit" %>
<% end %>
如果你需要看我的模型。我认为它很好,因为它在旧版本中工作:
models/seller.rb
class Seller < ActiveRecord::Base
belongs_to :user
belongs_to :address, :class_name => "Address", :foreign_key => 'address_id'
accepts_nested_attributes_for :address, :allow_destroy => true
belongs_to :shipping_address, :class_name => "Address", :foreign_key => 'shipping_address_id'
accepts_nested_attributes_for :shipping_address, :allow_destroy => true
end
这是控制器代码
controllers/users_controllers.rb
def create_seller #get
@user = @current_user
@seller = Seller.new
@seller.tax1 = 500
@seller.tax1_name_fr = "TPS"
@seller.tax1_name_en = "GST"
@seller.tax2 = 950
@seller.tax2_name_fr = "TVQ"
@seller.tax2_name_en = "QST"
@seller.build_address
@seller.build_shipping_address
end
<% seller.fields_for :address do |address_fields| %>
应该是
<%= seller.fields_for :address do |address_fields| %>
请注意 <%
之后的“=”符号以显示输出 我相信您已经意识到这一点,但是在 erb 标签中使用 = 符号是版本 2.xx[= 之后的重大变化13=]
几个月来我一直在尝试将旧的 Rails 2.X 网页升级到 Rails 4。 我仍在为一些局部渲染视图而苦苦挣扎。所以我想我需要一些帮助。如果对某人有帮助,我已经保存了我所有的 upgrade notes。但是这个我想不通。
目前的问题是下面的代码只打印地址表(见评论)。
views/users/_seller.erb:
<table class="labels">
<tr>
<td>{Seller type}</td>
<td><%= seller.select(:stype, ["producer","distributor","store"]) %></td>
</tr>
(...)
<h3>{Store/Pickup address}</h3>
<% seller.fields_for :address do |address_fields| %>
<!-- THIS IS NOT RENDERED!! -->
<table class="labels">
<tr>
<td>{Address Line 1}</td>
<td><%= address_fields.text_field :line1 %></td>
</tr>
<!-- UP TO THERE IS NOT RENDERED!! -->
<% end %>
这个局部视图被调用: views/users/create_seller.html.erb:
<h1>{Before you sell a product, please enter your seller information}</h1>
<%= form_for @seller, :url => "/seller/create" do |f| %>
<h2>form</h2>
<% @seller.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
<%= render partial: "seller", locals: {seller:f} %>
<%= f.submit "Submit" %>
<% end %>
如果你需要看我的模型。我认为它很好,因为它在旧版本中工作: models/seller.rb
class Seller < ActiveRecord::Base
belongs_to :user
belongs_to :address, :class_name => "Address", :foreign_key => 'address_id'
accepts_nested_attributes_for :address, :allow_destroy => true
belongs_to :shipping_address, :class_name => "Address", :foreign_key => 'shipping_address_id'
accepts_nested_attributes_for :shipping_address, :allow_destroy => true
end
这是控制器代码
controllers/users_controllers.rb
def create_seller #get
@user = @current_user
@seller = Seller.new
@seller.tax1 = 500
@seller.tax1_name_fr = "TPS"
@seller.tax1_name_en = "GST"
@seller.tax2 = 950
@seller.tax2_name_fr = "TVQ"
@seller.tax2_name_en = "QST"
@seller.build_address
@seller.build_shipping_address
end
<% seller.fields_for :address do |address_fields| %>
应该是
<%= seller.fields_for :address do |address_fields| %>
请注意 <%
之后的“=”符号以显示输出 我相信您已经意识到这一点,但是在 erb 标签中使用 = 符号是版本 2.xx[= 之后的重大变化13=]