English 中文(简体)
Rails嵌套的用户配置文件字段,Devise不保留输入
原标题:Rails nested User Profile fields with Devise not retaining input

我正试图将表单字段添加到我的Devise用户注册视图中。这已经实现,验证在我提交表单时运行。但是,如果我遇到任何表单错误,输入数据不会按照原始表单重新填充表单字段,尽管我可以在添加到开发视图的小调试器中看到正确的值。

以下是我的观点:

<h2>Sign up</h2>

<% resource.build_profile %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
    <%= devise_error_messages! %>

    <div><%= f.label :email %><br />
    <%= f.email_field :email %></div>

    <div><%= f.label :username %> <i>(this cannot be changed so choose wisely)</i><br />
    <%= f.text_field :username %></div>

    <div><%= f.label :password %><br />
    <%= f.password_field :password %></div>

    <div><%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation %></div>

    <%= f.fields_for :profile do |profile_form| %>
        <div><%= profile_form.label :full_name %><br />
        <%= profile_form.text_field :full_name %></div>

        <div><%= profile_form.label :birth_date %><br />
        <%= profile_form.date_select :birth_date, start_year: Time.now.year, end_year: Time.now.year - 80, order: [:day, :month, :year], prompt: { day:  Choose day , month:  Choose month , year:  Choose year  } %></div>

        <div><%= profile_form.label :gender %><br />
        <%= profile_form.select :gender, { "Male" =>  0 , "Female" =>  1  } %></div>

        <div><%= profile_form.label :postcode %><br />
        <%= profile_form.text_field :postcode %></div>

        <div><%= profile_form.label :description, "About you" %><br />
        <%= profile_form.text_area :description %></div>
    <% end %>

    <div><%= f.submit "Sign up" %></div>
<% end %>

<%= render :partial => "devise/shared/links" %>

我没有控制器代码可以显示,因为它在Gem中使用了Devise代码。我做错了什么/没做什么?

问题回答

每次使用build_profile重建配置文件时,这意味着当要重新显示表单时,它使用的是配置文件的新实例,而不是有错误的实例。只需更改该代码,使其仅在配置文件不存在时构建配置文件:

resource.build_profile unless resource.profile

这样的事情应该奏效。

卡洛斯的回答帮助我解决了这个确切的问题。

resource.build_profile unless resource.profile

如果您的用户模型引用了配置文件模型,请尝试传递

<%= f.fields_for resource.profile do |profile_form| %>

现在,您正在传递对模型的引用,但表单需要一个实例。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签