【发布时间】:2011-01-05 11:19:06
【问题描述】:
我有一个包含以下字段的用户模型:first_name、last_name、email。
客户想要两个页面。第一页编辑名字和姓氏,第二页编辑电子邮件地址。
我想到的一个选项是使用参数来确定应该在 user/edit 中呈现哪些部分:
<% if !params[:section] %>
<%= render :partial => 'form_for_name' %>
<% elsif params[:section] == 'email' %>
<%= render :partial => 'form_for_email' %>
<% end %>
第一页的 url 如下所示:http://localhost:3000/users/1/edit
第二页:http://localhost:3000/users/1/edit?section=email
有一点需要注意。用户模型在def update中包含以下代码:
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
这意味着,在电子邮件页面上,如果用户输入了无效的电子邮件,错误将显示在第一页而不是电子邮件页面上。
我正在寻找实现此方案的最佳方法。或者我应该有 2 个从用户控制器继承的控制器(姓名和电子邮件)?每个控制器都有各自的视图。
有没有更好的解决方案?
【问题讨论】:
标签: ruby-on-rails