我有两个模型:
第一个模型:
class Url < ActiveRecord::Base
attr_accessible :code, :target, :user_id, :user
belongs_to :user, :dependent => :destroy
end
导轨控制台
1.9.3p125 :001 > Url
=> Url(id: integer, user_id: integer, code: string, target: string, created_at: datetime, updated_at: datetime)
第二种型号:
class User < ActiveRecord::Base
attr_accessible :description, :status, :username
has_many :urls
validates :username,
:presence => true,
:uniqueness => true
def to_s
username
end
end
导轨控制台
1.9.3p125 :002 > User
=> User(id: integer, username: string, status: integer, description: text, created_at: datetime, updated_at: datetime)
现在我有一个表单,用于创建一个 url:
<%= form_for(@url) do |f| %>
<% if @url.errors.any? %>
<div id="error_explanation">
<h2><%= @url.errors.count %> Error(s):</h2>
<ul>
<% @url.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :user_id %><br />
<%= f.text_field :user_id %>
</div>
<div class="field">
<%= f.label :code %><br />
<%= f.text_field :code %>
</div>
<div class="field">
<%= f.label :target %><br />
<%= f.text_field :target %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
为了增加用户友好性,我想将字段 user_id 与字段用户名交换。需要做哪些步骤?
谢谢!