我使用Rails 4和Devise进行用户身份验证。我想在稍后的阶段(在另一个页面上)从用户那里收集额外的输入。
我得到一个模板错误,这似乎是因为我使用的设计,有一些冲突,我的用户控制器。设计控制器操作在应用程序控制器中(如果您也需要此代码,请使用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]在应用程序/控制器/用户_控制员. In中
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应用程序/视图/用户/ed.html.erb
<%= render 'form' %>在app/view/user/_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 %>发布于 2014-09-02 04:44:28
尝试将更新更改为:
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发布于 2014-09-02 04:38:29
尝试在update方法中添加一个respond_to或redirect_to块,以指定用户在成功更新后被重定向到的路径。
发布于 2014-09-02 05:21:18
我就是这样做的,我复制了设计视图,并向视图、模型等添加了一些内容。
在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结束
-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然后复制我放入app/view/ Devise中的所有te视图,例如,这里有一个app/view/devise/registrations/ed.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 %> »
<%= 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>https://stackoverflow.com/questions/25615757
复制相似问题