简体   繁体   English

Rails渲染request.referer错误缺少模板

[英]Rails render request.referer error missing template

  1. Rails 4.2.1 导轨4.2.1
    1. Bootstrap 3 引导程序3
    2. Simple_form 简单的形式

I have a three page user edit, (profile, account, settings) system. 我有一个三页的用户编辑((配置文件,帐户,设置)系统)。

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

I will post the edit_profile.html.erb some proprietary info will be removed. 我将发布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>

The edit.html.erb is exactly the same except it has code from all of the other three files combined. edit.html.erb完全相同,只是它具有来自其他所有三个文件的代码。 There is nothing different in it. 没有什么不同。

In user.controller.rb 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

Each of the above pages has 4 or more tabs on it. 以上每个页面上都有4个或更多选项卡。 each tab containing a part of the user forms. 每个选项卡包含一部分用户表单。 In my nav (not the tabs on the individual pages) I have link_to like this. 在我的导航中(而不是各个页面上的选项卡),我具有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>

Then I have the update also in the user.controller.rb 然后我在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

end 结束

If the user makes a mistake on one of the three pages it should render back to the referring form page with validation errors on it. 如果用户在三个页面之一上犯了错误,则应将其转回到带有验证错误的引用表单页面。

Address bar URLs. 地址栏URL。 On profile edit page http://localhost:3000/users/2/edit?form=profile On missing template page http://localhost:3000/users/2 在配置文件编辑页面上http://localhost:3000/users/2/edit?form=profile在缺少的模板页面上http://localhost:3000/users/2

Full error. 完全错误。 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"

A note - you wouldn't have to do this if you had separate controllers for each of the edit modes (profile_controller, account_controller, settings_controller). 注意-如果每种编辑模式(profile_controller,account_controller,settings_controller)具有单独的控制器,则无需执行此操作。 Putting the conditional logic in the controller action doesn't get you much - instead you'd put the logic in routes and the controller actions stay simple. 将条件逻辑放在控制器动作中并不会带来太多好处,而是可以将逻辑放在路由中,并且控制器动作保持简单。

So I did this by creating the three separate controllers 所以我通过创建三个单独的控制器来做到这一点

rails g controller account rails g controller setting rails g controller profile rails g controller account rails g controller setting rails g controller profile

In the routes.rb I added. 在routes.rb中,我添加了。

resources :profile, :only => [:edit, :update] 
resources :account, :only => [:edit, :update]
resources :settings, :only => [:edit, :update]

Then just copied the edit and update methods from the user controller to these new controllers. 然后只需将编辑和更新方法从用户控制器复制到这些新控制器。 Created the edit.html.erb in each view directory and all is working the way it should. 在每个视图目录中创建了edit.html.erb ,所有工作都按应有的方式进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM