我有三页用户编辑,(个人资料,帐户,设置)系统。
app/view/users
_form.html.erb
edit.html.erb
edit_account.html.erb
edit_profile.html.erb
edit_settings.html.erb
new.html.erb
show.html.erb
我将发布edit_profile.html.erb
一些专有信息将被删除。
edit_profile.html.erb
<% provide(:title, "Edit Profile") %>
<div>
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><%= link_to 'Profile', "#profile", "aria-controls" => "profile", "role" => "tab", "data-toggle" => "tab" %></li>
<li role="presentation"><%= link_to 'Bio', "#bio", "aria-controls" => "bio", "role" => "tab", "data-toggle" => "tab" %></li>
<li role="presentation"><%= link_to 'My Websites', "#websites", "aria-controls" => "websites", "role" => "tab", "data-toggle" => "tab" %></li>
<li role="presentation"><%= link_to 'Upload Photos', "#photos", "aria-controls" => "photos", "role" => "tab", "data-toggle" => "tab" %></li>
<li role="presentation"><%= link_to 'Upload Videos', "#videos", "aria-controls" => "videos", "role" => "tab", "data-toggle" => "tab" %></li>
</ul>
<%= render 'shared/error_messages' %>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="profile">
<div class="row">
<div class="col-md-8">
<%= simple_form_for @user, url: {action: "update"} do |f| %>
<div class="form-inputs form-inline well">
<legend>Tell us about yourself!</legend>
<%= f.input :birthdate, as: :date,
start_year: Date.today.year - 18,
end_year: Date.today.year - 100,
order: [:month, :day, :year],
:label=> 'Date of Birth',
:required => true %>
<br>
<%= f.input :age_valid,
:as => :boolean,
:label => false,
:checked_value => true,
:unchecked_value => false,
:inline_label => 'I am 18 years of age or older.' %>
</div>
<div class="form-actions">
<ul class="list-inline">
<li><%= f.button :submit, 'Update Profile', :class=> "btn btn-primary btn-sm" %></li>
</ul>
</div>
<% end %>
</div>
<div class="col-md-4">
<ul class="h6c">
<li>Q. How old do I need to be?</li>
<li>You need to be at least 18 years old.</li>
</ul>
</div>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="bio">
<div class="row">
<div class="col-md-8">
</div>
<div class="col-md-4">
</div>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="websites">
<div class="row">
<div class="col-md-8">
</div>
<div class="col-md-4">
</div>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="photos">
<div class="row">
<div class="col-md-8">
</div>
<div class="col-md-4">
</div>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="videos">
<div class="row">
<div class="col-md-8">
</div>
<div class="col-md-4">
</div>
</div>
</div>
</div>
edit.html.erb
完全相同,只是它包含来自所有其他三个文件的代码。它没有什么不同。
在user.controller.rb
def edit
@user = User.find(params[:id])
case params[:form]
when "profile"
render "edit_profile"
when "account"
render "edit_account"
when "settings"
render "edit_settings"
else
render :action => :edit
end
end
上述每个页面上都有4个或更多标签。每个选项卡包含一部分用户表单。在我的导航中(不是单个页面上的标签),我有这样的link_to。
<li><%= link_to 'My Profile', current_user %></li>
<li><%= link_to 'Write' %></li>
<li><%= link_to 'Upload Photos', edit_user_path(current_user, :form => "profile") %></li>
<li><%= link_to 'Upload Videos', edit_user_path(current_user, :form => "profile") %></li>
<li><%= link_to 'Edit My Profile', edit_user_path(current_user, :form => "profile") %></li>
<li><%= link_to 'My Settings', edit_user_path(current_user, :form => "settings") %></li>
<li><%= link_to 'My Account', edit_user_path(current_user, :form => "account") %></li>
然后我在user.controller.rb
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render request.referer <---- this gives me template missing error.
**render "edit"** <---- this doesn't work either as it takes me to the wrong page
end
端
如果用户在三个页面中的一个页面上出错,则应该回显到引用表单页面,并在其上显示验证错误。
地址栏网址。
在个人资料编辑页面http://localhost:3000/users/2/edit?form=profile
缺少模板页面http://localhost:3000/users/2
完全错误。
Missing template http://localhost:3000/users/2/edit?form=profile with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "D:/Development/projects/#/app/views"
答案 0 :(得分:0)
注意 - 如果您为每种编辑模式(profile_controller,account_controller,settings_controller)都有单独的控制器,则不必执行此操作。将条件逻辑放在控制器动作中并没有太多帮助 - 而是将逻辑放在路径中,控制器动作保持简单。
所以我通过创建三个独立的控制器
来做到这一点 rails g controller account
rails g controller setting
rails g controller profile
在routes.rb中,我添加了。
resources :profile, :only => [:edit, :update]
resources :account, :only => [:edit, :update]
resources :settings, :only => [:edit, :update]
然后将编辑和更新方法从用户控制器复制到这些新控制器。在每个视图目录中创建了edit.html.erb
,所有这些都按照应有的方式工作。