用户图片不会与设计一起保存。我的devise_registration表单或update_form有什么问题吗?

时间:2016-03-02 19:48:10

标签: ruby-on-rails forms devise carrierwave

我正在使用carrierwave和minimagick gem来保存用户在Rails中上传图片。我已将图片字段添加到设计表单并更新了用户模型以包含mountuploader文件。 但是,当我尝试保存时,除了图片之外,所有用户属性都在数据库中保存和更新。 当我在控制台中检查用户时,图片属性为零。有任何想法吗 ?我的其他型号保存图片就好了。

以下是我的设计形式: 的 devise_registerations /新

<div class="authform">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :role => 'form', multipart: true}) do |f| %>
<h3>Sign up</h3>
<%= devise_error_messages! %>
<div class="form-group">
  <%= f.label :name %>
  <%= f.text_field :name, :autofocus => true, class: 'form-control' %>
</div>
<div class="form-group">
  <%= f.label :email %>
  <%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
  <%= f.label :password %>
  <%= f.password_field :password, class: 'form-control' %>
</div>
<div class="form-group">
  <%= f.label :password_confirmation %>
  <%= f.password_field :password_confirmation, class: 'form-control' %>
</div>

  <div class="form-group">
  <%= f.label :country %>
  <%= country_select(resource_name, "country")%> 
</div>


 <div class="form-group">
  <%= f.label :description, "Bio" %>
  <%= f.text_area :description, class: 'form-control', cols: "30", rows: "10" %>
 </div>
 Picture: <%= f.file_field :picture %>
 <%= f.submit 'Sign up', :class => 'button right' %>
 <% end %>
 </div>

我的编辑类似:

<div class="authform">
<h3>Edit <%= resource_name.to_s.humanize %></h3>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :role => 'form', multipart: true }) do |f| %>
<%= devise_error_messages! %>
<div class="form-group">
  <%= f.label :name %>
  <%= f.text_field :name, :autofocus => true, class: 'form-control' %>
</div>
<div class="form-group">
  <%= f.label :email %>
  <%= f.email_field :email, class: 'form-control' %>
  <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
    <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
  <% end %>
</div>
<fieldset>
  <p>Leave these fields blank if you don't want to change your password.</p>
  <div class="form-group">
    <%= f.label :password %>
    <%= f.password_field :password, :autocomplete => 'off', class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :password_confirmation %>
    <%= f.password_field :password_confirmation, class: 'form-control' %>
  </div>
</fieldset>
<fieldset>
  <p>You must enter your current password to make changes.</p>
  <div class="form-group">
    <%= f.label :current_password %>
    <%= f.password_field :current_password, class: 'form-control' %>
  </div>
</fieldset>
Picture: <%= f.file_field :picture %>
<%= f.submit 'Update', :class => 'button right' %>
<% end %>
</div>
<div class="authform">
<h3>Cancel Account</h3>
<p>Unhappy? We'll be sad to see you go.</p>
<%= button_to "Cancel my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete, :class => 'button right' %>
</div>

这是我的UsersController:

 class UsersController < ApplicationController
 before_action :authenticate_user!
 before_action :admin_only, :except => :show
 def index
 @users = User.all
 end
 def show
 @user = User.find(params[:id])
 unless (current_user.admin? || current_user == @user)
  unless @user == current_user
    redirect_to :back, :alert => "Access denied."
  end
 end
 end
 def update
 @user = User.find(params[:id])
 if @user.update_attributes(secure_params)
  redirect_to users_path, :notice => "User updated."
 else
  redirect_to users_path, :alert => "Unable to update user."
 end
 end
 def destroy
 user = User.find(params[:id])
 user.destroy
 redirect_to users_path, :notice => "User deleted."
 end

 private
 def admin_only
 unless current_user.admin?
  redirect_to :back, :alert => "Access denied."
 end
 end
 def secure_params
 params.require(:user).permit(:role, :picture, :name, :email, :password)
 end
 end

最后我包括我的UserModel:

 class User < ActiveRecord::Base
  enum role: [:user, :vip, :admin, :manager]
  after_initialize :set_default_role, :if => :new_record?
  def set_default_role
    self.role ||= :user
  end
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  mount_uploader :picture, PictureUploader
  has_many :projects, dependent: :destroy
  end

1 个答案:

答案 0 :(得分:1)