Rails 呈现 request.referer 错误缺少模板
Rails render request.referer error missing template
- Rails 4.2.1
- Bootstrap 3
- Simple_form
我有一个三页用户编辑(配置文件、帐户、设置)系统。
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
我将 post 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
结束
如果用户在这三个页面之一上犯了错误,它应该呈现回带有验证错误的引用表单页面。
地址栏 URL。
在个人资料编辑页面上 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"
请注意 - 如果您为每种编辑模式 (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
并且一切正常。
- Rails 4.2.1
- Bootstrap 3
- Simple_form
我有一个三页用户编辑(配置文件、帐户、设置)系统。
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
我将 post 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
结束
如果用户在这三个页面之一上犯了错误,它应该呈现回带有验证错误的引用表单页面。
地址栏 URL。
在个人资料编辑页面上 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"
请注意 - 如果您为每种编辑模式 (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
并且一切正常。