尝试在单个表单上显示我的用户和配置文件模型时尝试获取*fields_for*以产生输出时遇到了一些麻烦,这些使用*has_one*和*belongs_to*关系.
所以这里是模型类顶部的提取:
class User < ActiveRecord::Base
has_one :profile
accepts_nested_attributes_for :profile
class Profile < ActiveRecord::Base
belongs_to :user
Run Code Online (Sandbox Code Playgroud)
控制器非常简单和标准:
def new
@user = User.new
end
def edit
@user = User.find(params[:id])
end
Run Code Online (Sandbox Code Playgroud)
以下是目前的视图中的一个片段:
<%= form_for(@user) do |f| %>
<% f.fields_for :profile do |profile_form| %>
<div class="field">
<%= profile_form.label :name %><br />
<%= profile_form.text_field :name %>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我尝试过其他的东西,比如:
<% fields_for …Run Code Online (Sandbox Code Playgroud) 我有一些人的记录,我想按以下方式分解信息:
可能在这里有一些不正确的假设,即单个单词姓氏.我正在寻求使用正则表达式进行批量处理,并且我会继续关注这个问题.正则表达式的实现将在PHP中.
以下是一些示例记录:
Sam Leicester
Sam Christopher Leicester
Sam Leicester (London)
Sam Christopher Leicester (France)
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经制作了这个http://regexr.com/39cbk,它将名称解析为一个数组(易于使用的最后一个元素作为姓氏,其余作为名字加入)与该位置的recond组.虽然我似乎对使用preg_match()实现这一点没有任何乐趣.