我有2个模型设置如下:
class Campaign < ApplicationRecord
has_many :cities
belongs_to :user
end
class City < ApplicationRecord
belongs_to :campaign
end
控制器:
def new
@campaign = current_user.campaigns.build
@cities = @campaign.cities
end
我希望用户能够直接从广告系列页面创建和修改城市。我不知道该怎么做,这是我的尝试。我如何访问city.name? :name是@campaign
的参数键<%= form_for @campaign do |f| %>
<%= f.label "Name" %>
<br />
<%= f.text_field :name %>
<br /> <br />
<%= f.label "Titles" %>
<br />
<%= f.text_area :titles, cols: 80, rows: 20 %>
<br /> <br />
<%= f.label "Sentences" %>
<br />
<%= f.text_area :sentences, cols: 80, rows: 20 %>
<br /> <br />
<%= f.label "Keywords" %>
<br />
<%= f.text_area :keywords, cols: 80, rows: 20 %>
<br /> <br />
<% if @cities.count > 0 %>
<h2>Cities</h2>
<% for city in @cities %>
<%= f.label "City name:" %>
<br />
<%= f.text_area :name, cols: 80, rows: 20 %>
<% end %>
<% end %>
<%= f.submit "Submit" %>
<% end %>
答案 0 :(得分:0)
我能够这样做:
<% if @cities.count > 0 %>
<h2>Cities</h2>
<% for city in @cities %>
<%= form_for city do |f| %>
<%= f.text_area :name, cols: 20, rows: 1 %>
<%= f.text_area :phone_number, cols: 20, rows: 1 %>
<%= f.text_area :zip_code, cols: 20, rows: 1 %><%= f.submit "Submit" %>
<% end %>
<% end %>
<% end %>