簡體   English   中英

Rails渲染request.referer錯誤缺少模板

[英]Rails render request.referer error missing template

  1. 導軌4.2.1
    1. 引導程序3
    2. 簡單的形式

我有一個三頁的用戶編輯((配置文件,帳戶,設置)系統)。

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

結束

如果用戶在三個頁面之一上犯了錯誤,則應將其轉回到帶有驗證錯誤的引用表單頁面。

地址欄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 ,所有工作都按應有的方式進行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM