0

I'm using CarrierWave for my Avatar Uploading. The uploading and deletion of the avatar works in the user edit view and displays in otherviews. But when trying to include an Avatar with a comment I run into an error.

TypeError in CommentsController#create
can't cast AvatarUploader to string

app/controllers/comments_controller.rb:10:in `create'

I'm not sure what I've done wrong.

  **comments_controller.rb**

  def create
  @post = Post.find(params[:post_id])
  @comment = @post.comments.create(comments_params)
  @comment.user_name = current_user.user_name
  @comment.avatar = current_user.avatar
  if @comment.save
   redirect_to @post
  else
   flash.now[:danger] = "error"
  end
  end

1 Answers1

1

Edit:

I would suggest not saving the user's avatar against each comment.

Rather, I would do this:

  1. Set up your models so that a comment belongs_to a user, and a user has_many comments
  2. When creating a comment save the user_id as current_user.id
  3. In your partial/view when displaying the comments do something like:

<%= image_tag(@comment.user.avatar) %>

Original:

You shouldn't need to do this in your controller.

Try simply this in your view:

<%= image_tag(current_user.avatar) %>
patrick
  • 9,837
  • 3
  • 20
  • 27
  • I don't need to save the Avatar to the Comment? I did a migration add_avatar_to_comments & added the avatar as a param in the comment. Was any of that needed? The thing is I'm using partials so I've got this view.. `

    <%= comment.avatar %>` `

    <%= comment.user_name %>

    ` `

    <%= comment.created_at %>

    ` `

    <%= comment.content %>

    `
    – Merlin Jackson Mar 05 '15 at 03:54
  • Sorry, I misunderstood the question.. Why do you want to save the avatar against the comment? Why not save the user_id of the user who wrote the comment and then retrieve their avatar displaying the comment? This keeps the avatar up-to-date rather than saving the avatar every time they create a comment (red flag!!!) – patrick Mar 05 '15 at 03:57
  • Thanks for that.. I haven't thought that far ahead. Maybe the script for that action would be best. – Merlin Jackson Mar 05 '15 at 03:59
  • I've gotten the original error to pass with the code. `@comment.avatar = current_user.avatar.to_s` But I cannot get the code right to keep the avatar up to date. ... Saving the user_id and then retrieve the avatar sounds like it would work but I'm confused. – Merlin Jackson Mar 05 '15 at 05:18
  • It might work but it isn't very good practice. What are you confused about? – patrick Mar 05 '15 at 05:24
  • I don't know how to save the user_id as the current_user when I save the post. – Merlin Jackson Mar 05 '15 at 05:27
  • First you will need to create the association between `comment` and `user` (which is in my updated answer). Then you will need to create a new migration for `comments`. In that migration I would remove the current fields you're storing about the user (such as `user_name` and `avatar`) and add a new field for user_id. Then when you save using the controller simply do `@comment.user_id = current_user.id` – patrick Mar 05 '15 at 05:29