Rails 设计 fields_for 不工作
Posted
技术标签:
【中文标题】Rails 设计 fields_for 不工作【英文标题】:Rails Devise fields_for not working 【发布时间】:2014-02-13 20:18:26 【问题描述】:我正在使用 Devise,但我认为这在这里并不重要。
我根本看不到 fields_for 正在创建。
这些是我的文件:
new.html.erb
<%= form_for(resource, :as => resource_name, html: id: 'registration_form', class: '' , :url => registration_path(resource_name)) do |f| %>
<!-- Error messages -->
<%= devise_error_messages! %>
<%= render 'new_user_fields', f: f %>
<!-- Fields for user-information -->
<%= f.fields_for :user_info do |f| %>
<%= render 'shared/user_info_fields_for_sign_up', f: f, resource: resource %>
<% end %>
<!-- Fields for driver-information -->
<%= f.fields_for :driver do |f| %>
<%= render 'shared/driver_fields_for_sign_up', f: f, resource: resource %>
<% end unless resource.driver.nil? %>
<!-- Submit button -->
<%= f.submit "Sign up", class: "" %>
<% end %>
共享/_user_info_fields_for_sign_up.html.erb
<%= f.label t :label_gender %>
<%= f.collection_select :gender_id, Gender.all, :id, :type, class: '' %>
<%= f.label t :label_first_name %>
<%= f.text_field :first_name, class: "" %>
<%= f.label t :label_last_name %>
<%= f.text_field :last_name, class: "" %>
<%= f.label t :label_year_of_birth %>
<%= f.text_field :year_of_birth, class: "" %>
<%= f.label t :label_country %>
<%= f.select :country_alpha2, options_for_select: Country.all , class: '' %>
<%= f.label t :label_city %>
<%= f.text_field :city, class: "" %>
<%= f.fields_for :languageskills do |f| %>
<%= render 'shared/languageskill_fields', f: f %>
<% end unless resource.user_info.languageskills.empty?%>
<%= link_to_add_fields t :label_add_language, f, :languageskills %>
<%= f.label t :label_interests %>
<%= f.text_field :interests, class: "" %>
<%= f.label t :label_about %>
<%= f.text_field :about, class: "" %>
RegistratiosController.rb
def new
super
resource.build_user_info
resource.user_info.languageskills.build
if params[:is_driver].to_i == 1
resource.build_driver
end
Rails.logger.debug(resource.build_user_info.inspect)
end
Console.log
Started GET "/en/sign_up?is_driver=1" for 127.0.0.1 at 2014-02-13 13:20:01 +0100
Processing by RegistrationsController#new as JS
Parameters: "is_driver"=>"1", "locale"=>"en"
Rendered registrations/_new_user_fields.html.erb (12.1ms)
Rendered registrations/new.html.erb within layouts/application (27.8ms)
Rendered layouts/_header.html.erb (2.8ms)
Rendered layouts/_messages.html.erb (0.2ms)
Rendered layouts/_footer.html.erb (0.6ms)
(0.2ms) begin transaction
(0.1ms) commit transaction
#<UserInfo id: nil, user_id: nil, first_name: nil, last_name: nil, year_of_birth: nil, city: nil, created_at: nil, updated_at: nil, gender_id: nil, interests: nil, about: nil, country_alpha2: nil>
Completed 200 OK in 117ms (Views: 96.5ms | ActiveRecord: 0.4ms)
_new_user_fields.html.erb
正在加载,没有任何问题。
我已将所有可能的符号插入到application_controller.rb
中设计的输入参数中。虽然没关系,对吧?
你能指出我正确的方向吗?我看不出我的错:S
【问题讨论】:
【参考方案1】:刚刚找到错误的来源,并将为其打开一个新帖子。
我不能打电话
resource.build_nested_model
在我的控制器中。但是当我在视图中调用它时,它可以工作。嵌套模型在传递给视图时似乎没有保存到资源中。
链接到新话题
Rails Devise Build nested model in controller
【讨论】:
以上是关于Rails 设计 fields_for 不工作的主要内容,如果未能解决你的问题,请参考以下文章
Rails:使用 fields_for 和 check_box