2014-09-02 100 views
1

我正在使用Rails 4與Devise進行用戶身份驗證。我想在稍後階段(在另一頁上)收集用戶的額外輸入。Rails 4 Missing模板錯誤

我得到一個模板錯誤,它似乎因爲我使用設計,與我的用戶控制器有一些衝突。設計控制器動作是在應用控制器(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" 

我的routes.rb

devise_for :users 

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

在應用程序/控制器/ 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 

在應用程序/意見/使用者/ edit.html.erb

<%= render 'form' %> 

在應用程序/視圖/用戶/ _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 %> 
+0

可能沒有任何好處瓦努阿圖的我獲取用戶的額外的信息,但只有b。將制定觀點 - 如果你不能做到這一點的方式你的後(EGAS以上),我可以張貼 - 皮埃爾 – user1854802 2014-09-02 04:35:41

+0

請發佈。我嘗試使用設計視圖,但得到了其他錯誤,所以我切換到這種方法 – Moosa 2014-09-02 04:38:31

回答

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 
0

嘗試增加在更新方法的respond_to代碼或redirect_to的塊來指定用戶被成功更新之後重定向到路徑。

0

這是我做到了通過複製andadding東西的看法,模型等

在application_controller的設計意見Iadded東西

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 

然後複製拷貝從設計的所有TE的觀點,我把在應用程序/視圖/設計 例如這裏是一個應用程序/視圖/設計/註冊/ 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> 
+0

謝謝,似乎太多的變化,所以我去了下面的解決方案。 – Moosa 2014-09-02 13:31:23