Carrierwave save nil value on database

110 views
Skip to first unread message

Adham El-Deeb

unread,
Oct 2, 2014, 11:10:58 PM10/2/14
to rubyonra...@googlegroups.com
I'm using Rails-api with mongoid.
I try to upload user avatar using Carrierwave from a remote url.
I got theremote url form omniauth-facebook image params.
The problem is the avatar field in Databass have nil value after save.
This is my app on github https://github.com/adham90/pp

Alexandre Calvão

unread,
Oct 3, 2014, 11:20:16 AM10/3/14
to rubyonra...@googlegroups.com
From the carrierwave documentation 

<%= form_for @user, :html => {:multipart => true} do |f| %>
  <p>
    <label>My Avatar URL:</label>
    <%= image_tag(@user.avatar_url) if @user.avatar? %>
    <%= f.text_field :remote_avatar_url %>
  </p>
<% end %>

It means that the parameter avatar should have the '_url' sufix.

You can make a before_filter to change that, or a Service Object that would properly save the User with the correct parameter.

===================
Alexandre Mondaini Calvão

"Nossa recompensa se encontra no esforço e não no resultado. Um esforço total é uma vitória completa." [Ghandi]

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5d0eee57-fa44-4a34-b692-ee394733e2fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexandre Calvão

unread,
Oct 3, 2014, 11:20:59 AM10/3/14
to rubyonra...@googlegroups.com
It also requires the 'remote' prefix.


===================
Alexandre Mondaini Calvão

"Nossa recompensa se encontra no esforço e não no resultado. Um esforço total é uma vitória completa." [Ghandi]

Adham El-Deeb

unread,
Oct 3, 2014, 11:54:05 AM10/3/14
to rubyonra...@googlegroups.com
there is no html on my app it is an API and i want to save the user from the User Model it self

Hassan Schroeder

unread,
Oct 3, 2014, 1:04:44 PM10/3/14
to rubyonrails-talk
On Fri, Oct 3, 2014 at 8:54 AM, Adham El-Deeb <adham.e...@gmail.com> wrote:
> there is no html on my app it is an API and i want to save the user from the
> User Model it self

1) What client are you using?
2) What params are being logged for a create request?
3) What does the log show for a request sequence?

I have no experience with Carrierwave but are you sure that it's not
expecting a "remote_avatar_url" param by default, as Alexandre
suggested? That has nothing to do with whether you're using HTML
or not...

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Alexandre Calvão

unread,
Oct 3, 2014, 3:25:23 PM10/3/14
to rubyonra...@googlegroups.com
Adham,

The code I posted was just to show you that Carrierwave requires that you add a suffix "remote_" and a prefix '_url' to the parameter managed by carrierwave, in order for it to work with urls.

In your case the following code is not doing that, you are passing the the parameter 'avatar' without the sufix and the prefix.

user_controller.rb

28- @user = User.new(user_params)
 
def user_params
params.require(:user).permit(:username, :name, :email, :password, :password_confirmation, :age, :gender, :avatar)
end


You gotta find a way to pass 'remote_avatar_url' => 'http:://www.test.com/someimage.jpg'

That is what carrierwave requires to work with url instead of files.

[]`s


===================
Alexandre Mondaini Calvão

"Nossa recompensa se encontra no esforço e não no resultado. Um esforço total é uma vitória completa." [Ghandi]

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Adham El-Deeb

unread,
Oct 3, 2014, 4:37:58 PM10/3/14
to rubyonra...@googlegroups.com
Thanks! Alexandre it works :D
Reply all
Reply to author
Forward
0 new messages