- 滑軌4.2.1
- 引導3
- Simple_form
我有三個頁面用戶編輯,(配置文件,帳戶,設置)系統。滑軌渲染request.referer錯誤丟失模板
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"
您需要明確定義應該呈現哪個模板。 request.referrer是您來自的網址 - 如果您堅持使用此方法,您可能希望將當前模板作爲隱藏變量傳遞給表單。注意 - 如果您爲每種編輯模式(profile_controller,account_controller,settings_controller)分別設置了控制器,則不必執行此操作。把條件邏輯放在控制器動作中並沒有給你帶來太大的幫助 - 相反,你會把邏輯放在路由中,控制器動作保持簡單。 – Swards
我是新來的鐵軌,雖然不完全無能。你的建議聽起來不錯,但我不知道如何實施任何建議。由於所有配置文件,帳戶和設置均與用戶數據庫綁定。一切都存儲在用戶表中。 –
現在明白了。我想出瞭如何製作其他控制器。 –