我试图在rails 4中制作应用程序.
我有一个用户模型,上面有一个头像属性.
我也有个人资料模型.
用户:有一个个人资料
个人资料:属于User
我拆分它们的原因是因为配置文件包含所有变量,而用户包含所有未经管理员权限无法更改的固定属性.
例外是我的用户模型具有我想允许用户更改的头像(图像)属性.
我在我的代码的其他部分有用户carrIErwave,它的工作原理.但是,这个用例有些不对劲.
我有一个头像上传者:
class AvatarUploader < CarrIErWave::Uploader::Base # Include RMagick or MiniMagick support: # include CarrIErWave::RMagick include CarrIErWave::MiniMagick # Choose what kind of storage to use for this uploader: # storage :file storage :fog # OverrIDe the directory where uploaded files will be stored. # This is a sensible default for uploaders that are meant to be mounted: def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.ID}" end def cache_dir "#{Rails.root}/tmp/uploads" end # ProvIDe a default URL as a default if there hasn't been a file uploaded: # def default_url # # For Rails 3.1+ asset pipeline compatibility: # # ActionController::Base.helpers.asset_path("fallback/" + [version_name,"default.png"].compact.join('_')) # # "/images/fallback/" + [version_name,"default.png"].compact.join('_') # end # Process files as they are uploaded: # process :scale => [200,300] # # def scale(wIDth,height) # # do something # end # Create different versions of your uploaded files: # version :thumb do # process :resize_to_fit => [50,50] # end process :resize_to_fit => [800,800] # Create different versions of your uploaded files: version :thumb do process :resize_to_fill => [200,200] end version :profile do process :resize_to_fill => [345,245] end version :wIDe do process :resize_to_fill => [951,245] end version :prevIEw do process :resize_to_fill => [90,90] end version :small do process :resize_to_fill => [35,35] end # Add a white List of extensions which are allowed to be uploaded. # For images you might use something like this: def extension_white_List %w(jpg jpeg gif png) end # OverrIDe the filename of the uploaded files: # AvoID using model.ID or version_name here,see uploader/store.rb for details. # def filename # "something.jpg" if original_filename # endend
在我的用户模型中,我有:
class User < ActiveRecord::Base mount_uploader :avatar,AvatarUploaderend
在我的用户控制器中 – 我将强化参数中的头像列入白名单.
在我的个人资料控制器强大的params我试过添加:
params.require(:profile).permit(:Title,:overvIEw,user_attributes: [:avatar])
我还尝试在我的个人资料模型中允许用户属性(如下所示):
belongs_to :useraccepts_nested_attributes_for :user
我的视图用户文件夹中有一个部分表单.
<%= simple_fIElds_for :user,HTML: { multipart: true } do |f| %> <%= f.error_notification %> <div > <%= f.input :avatar,as: :file,:label => "Add a profile image (head shot)" %> </div><% end %>
部分内容包含在我的个人资料表格中.
然后我想在我的个人资料显示视图中显示用户头像:
<div style= "background-image: url(<%= image_url @profile.user.avatar.url if @profile.user.avatar? %>);"> </div>
但是,它只是呈现一个空白.
我试过了:
);“>
但它仍然只是呈现空白.
谁能看到我做错了什么?
当我尝试通过检查user.avatar在rails控制台中测试时,我得到了这个:
u.avatar
=> #,last_sign_in_ip:#,confirmation_token:“73abb47df224dbc3a612b46ced66e1aba …”,confirmed_at:“2016-04-02 07:13:57”,confirmation_sent_at:“2016-04-02 22:59:44”,unconfirmed_email:“testIEst @ gmail.com“,Failed_attempts:0,unlock_token:nil,locked_at:nil,created_at:”2016-04-02 07:13:57“,updated_at:”2016-04-02 22:59:43“,avatar:nil,approved:false> ;,@ mounted_as =:avatar>
采取以下建议
我更改了表单,以便配置文件表单现在具有:
<%= render 'users/profileimgform',f: f %>
users / profileimgform现在具有:
<%= simple_fIElds_for :user,HTML: { multipart: true } do |ff| %> <%= f.error_notification %> <div > <%= ff.input :avatar,:label => "Add a profile image (head shot)" %> </div> <% end %>
我不确定是否需要在我的个人资料表格中将’ff’纳入该行.我尝试用’ff’替换最后的’f’,但是错误地询问我是否应该是单个’f’.
当我尝试这个时,控制台仍然显示user.avatar是’nil’.
解决方法 我猜这段代码有错误:<%= simple_fIElds_for :user,:label => "Add a profile image (head shot)" %> </div><% end %>
看看生成的HTML.我认为应该是这样的:
<%= simple_form_for :profile do |f| %> ... <%= f.simple_fIElds_for :user do |ff| %> <%= ff.input :avatar %> <% end %><% end %>总结
以上是内存溢出为你收集整理的ruby-on-rails – Rails 4 – Carrierwave图像无法上传全部内容,希望文章能够帮你解决ruby-on-rails – Rails 4 – Carrierwave图像无法上传所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)