【问题标题】:Rails 4 Missing Template errorRails 4缺少模板错误
【发布时间】:2014-09-02 04:15:01
【问题描述】:

我正在使用带有 Devise 的 Rails 4 进行用户身份验证。我想在稍后阶段(在另一个页面上)收集用户的其他输入。

我收到一个模板错误,似乎因为我使用的是设计,所以与我的用户控制器存在一些冲突。设计控制器操作在应用程序控制器中(如果您也需要此代码,请使用 lmk)

当我提交表单时,这是我得到的模板错误:

Missing template users/update, application/update with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "C:/Users/amoosa/Desktop/mktdemo/app/views" * "C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/devise-3.2.4/app/views"

我的路线.rb

devise_for :users

resources :users, only: [:update, :edit]

在 app/controllers/users_controller.rb 中

class UsersController < ApplicationController

  before_filter :load_user

  def update
    @user.update_attributes(user_params)
  end

  private

  def load_user
    @user = User.find(params[:id])
  end

  def user_params
    params.require(:user).permit(:color)
  end
end

在 app/views/users/edit.html.erb 中

<%= render 'form' %>

在 app/views/users/_form.html.erb 中

<%= form_for @user, url: user_path, html: { method: :put } do |f| %>
 <div class="form-group">
  <%= f.label :what_is_your_favorite_color %>
  <%= f.text_field :color, class:"form-control" %>
 </div>

 <div class="form-group">
    <%= f.submit "Submit", class:"btn btn-primary" %>
 </div>

<% end %>

【问题讨论】:

  • 可能没有任何好处,因为我从用户那里获得了额外的信息,但只能添加到设计视图中 - 如果您不能按照您以后的方式进行操作,我可以发布(例如如上所述) - 皮埃尔
  • 请张贴。我尝试使用设计视图,但遇到其他错误,所以我改用这种方法

标签: ruby-on-rails devise


【解决方案1】:

尝试将您的更新更改为:

  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
      else
        format.html { render action: 'edit' }
      end
    end
  end

【讨论】:

    【解决方案2】:

    尝试在更新方法中添加一个respond_to或redirect_to块来指定用户在成功更新后被重定向到的路径。

    【讨论】:

      【解决方案3】:

      这就是我通过复制设计视图并将内容添加到视图、模型等来实现的

      在application_controller中添加了东西

      class ApplicationController < ActionController::Base
        protect_from_forgery
      
        rescue_from CanCan::AccessDenied do |exception|
          redirect_to root_url, :alert => exception.message
        end
      
        before_action :configure_devise_permitted_parameters, if: :devise_controller?
      
        private
      
        def configure_devise_permitted_parameters
          registration_params = [:username, :email, :password, :password_confirmation, :role, :currency_id]
      
          devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :password, :remember_me) }
      
          if params[:action] == 'update'
            devise_parameter_sanitizer.for(:account_update) { 
              |u| u.permit(registration_params << :current_password)
      }
          elsif params[:action] == 'create'
            devise_parameter_sanitizer.for(:sign_up) { 
          |u| u.permit(registration_params) 
        }
          end
        end
      

      结束

      模型-用户

      class User < ActiveRecord::Base
         # Include default devise modules. Others available are:
         # :confirmable, :lockable, :timeoutable and :omniauthable
         # add :confirmable for email confirmation 
         devise :database_authenticatable, :registerable, :confirmable, 
                 :recoverable, :rememberable, :trackable, :validatable
      
        has_many :posts, dependent: :destroy
        has_many :comments, dependent: :destroy
        has_many :evaluation_assumptions, dependent: :destroy
        has_many :user_positions, dependent: :destroy
        has_many :user_evaluation_results, through: :evaluation_assumptions
        belongs_to :currency
      
        validates :username, :role, :currency_id, presence: true
        validates :username,:uniqueness => true
      
        include RoleModel
      
        roles_attribute :role
      
       roles :admin, :user, :guest
      
      end
      

      然后复制从我放在 app/views/devise 中的 Devise 的所有 te 视图 例如,这里是一个 app/views/devise/registrations/edit.html.erb。从记忆中用户名是我添加的一个字段以及currency_id。我还添加了用户头像(Gravatars)。

      <% content_for :title, "Drill Investor - #{@current_user.username}" %>
      <% content_for :tab_group, "dashboard" %>
      <% content_for :tab_id, "dashboard" %>
      <div class="breadcrumbs">
        <%= link_to 'Users', edit_user_registration_path %> &raquo; 
        <%= link_to current_user.username %>
      </div>
      <section>  
        <article class="single">
          <div class="form">
            <%= simple_form_for(resource, :as => resource_name, 
                :url => registration_path(resource_name), 
                :html => { :method => :put, class: 'infogroup' }) do |f| %>
              <div class="infogroup-header">Modify your account</div>        
              <%= f.error_notification %>
              <% if @user.errors.any? %>
                <%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:
                <% @user.errors.full_messages.each do |msg| %>
                  <li><%= msg %></li>
                <% end %>
              <% end %> <br />
              <div class="infogroup-body">
                <table border="0" cellpadding="0" cellspacing="0" class="info">  
                  <div class="form-inputs">
                    <tr>
                      <td class="lalign largest_column"><span>email</span></td>
                      <td> <%= f.text_field(:email, :required => true, :autofocus => true,
                                class: 'very_largest_column') %></td> 
                    </tr>
                    <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
                      <p>Currently waiting confirmation for: <%= resource.unconfirmed_email %>  </p>
                    <% end %>
                    <tr>
                      <td class="lalign largest_column"><span>username</span></td>
                      <td> <%= f.text_field(:username, :required => true, class: 'column') %></td>   
                    </tr> 
                    <tr>
                      <td class="lalign largest_column"><span>Currency to use</span></td>
                  <td> <%= f.collection_select(:currency_id, Currency.all, 
                            :id, :name, class: 'data') %></td>
                </tr>
                <tr>
                  <td class="lalign largest_column"><span>password</span></td>
                  <td> <%= f.text_field(:password, :autocomplete => "off", 
                          :hint => "leave it blank if you don't want to change it", :required => false,
                          type: 'password', class: 'column') %></td>
                </tr>
                <tr>
                  <td class="lalign largest_column"><span>password-confirmation</span></td>
                  <td> <%= f.text_field(:password_confirmation, :required => false,
                          type: 'password', class: 'column') %></td>  
                </tr>
                <tr>
                  <td class="lalign largest_column"><span>current password</span></td>
                  <td> <%= f.text_field(:current_password, 
                          :hint => "we need your current password to confirm your changes", :required => true,
                          type: 'password', class: 'column') %></td>
                    </tr>
                  </div>
                </table>
                <div class="form-actions">
                  <%= f.button :submit, "Update" %>
                </div>
              </div>
              <%= render 'user_gravatar' %>        
            <% end %>
            <%= link_to "Back", :back %>
          </form>
       </article>
      </section>
      

      【讨论】:

      • 谢谢,好像改动太多所以我选择了下面的解决方案。
      猜你喜欢
      • 2015-09-08
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多