简体   繁体   English

Rails 4缺少模板错误

[英]Rails 4 Missing Template error

I'm using Rails 4 with Devise for user authentication. 我正在使用带有Devise的Rails 4进行用户身份验证。 I'd like to collect additional inputs from users at a later stage (on another page). 我想稍后再收集用户的其他意见(在另一页上)。

I'm getting a template error and it seems like since I'm using devise, there is some conflict with my user controller. 我收到模板错误,并且似乎由于使用了devise,因此与我的用户控制器存在一些冲突。 Devise controller actions are in the application controller (lmk if you need this code too) 设计控制器动作在应用程序控制器中(如果也需要此代码,则为lmk)

When I submit the form, here is the template error I get: 提交表单时,这是我得到的模板错误:

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"

my routes.rb 我的routes.rb

devise_for :users

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

In app/controllers/users_controller.rb 在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

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

<%= render 'form' %>

In app/views/users/_form.html.erb 在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 %>

Try to change you update to: 尝试将您的更新更改为:

  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

尝试在update方法中添加response_to或redirect_to块,以指定成功更新后用户重定向到的路径。

this is how I did it by copying the devise views andadding stuff to views, model etc 这就是我通过复制设计视图并将内容添加到视图,模型等来做到的

In the application_controller Iadded stuff 在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

end 结束

Model -user 模型用户

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

Then copied copied all te views from Devise which I put in app/views/devise for example here is the one app/views/devise/registrations/edit.html.erb. 然后复制并复制来自Devise的所有te视图,例如,我在app / views / devise中放入了一个app / views / devise / registrations / edit.html.erb。 From memmory username was a field I added as well as currency_id. 从内存用户名开始,我添加了一个字段以及currency_id。 I also added user avatars (Gravatars). 我还添加了用户头像(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>

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

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