jmike
jmike

Reputation: 481

Carrierwave avatar uploading params

I am having trouble with implementing a avatar with carrierwave for my user profile page. i am getting the error nil is not a valid asset source. I think it because of i am not permitting :avatar params. I don't how to go about permitting the avatar params in my profile page in the Page_controller.rb file. The route of the profile page get 'user/:id' => 'pages#profile'. The route is a get request that finds the user by the username.


Pages_controller.rb

class PagesController < ApplicationController

    def home

    end

    def profile
        #find the user by username id
        if(User.find_by_username(params[:id]))
            #assign the username to the id params
            @username = params[:id]
        else
            redirect_to root_path, :notice => "User Not Found"
        end
        #help me get the user post to only post on his page
         @audios = Audio.all.where("user_id = ?", User.find_by_username(params[:id]).id)

    end

    #this is to upload the audio file
    def upload
        @newaudio = Audio.new

    end


end

User.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  extend Devise::Models
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
         has_many :audios
         has_many :reviews

           mount_uploader :avatar, AvatarUploader
            # User Avatar Validation
            validates_integrity_of  :avatar
            validates_processing_of :avatar
end

Profile.html.erb

  <div class="row">
    <div class="container">
  <div class="col-xs-3">
    <div class="panel panel-default">
     <div class="panel-body">

        <%= image_tag current_user.avatar.url%>
     </div>     
    </div>      

    <div class="panel panel-default">
        <div class="panel-body">
            About
        </div>
    </div>
  </div>

<div class="col-xs-6">
  <% for @a in @audios %>
    <h1><%= link_to @a.title, audio_path(@a) %> </h1>   
  <audio controls src="<%= @a.audio.url %>"></audio>
  <hr>
  <% end %>
    </div>

    <div class="col-xs-3">
        <div class="panel panel-default">
            <div class="panel-body">
                About section
            </div>
        </div>
        </div>
    </div>

    </div>
 </div>

Route.rb

Rails.application.routes.draw do

  devise_for :users
    root 'pages#home'

    resources :audios do
        resources :reviews
    end


    get 'user/:id' => 'pages#profile'
    get '/upload' => 'pages#upload'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

ApplicationController.rb

 class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
   before_action :configure_permitted_parameters, if: :devise_controller?


  # protect the database, while allowing these fields to be updated.
  protected 

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }
    devise_parameter_sanitizer.permit(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
    devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password, :avatar, :avatar_cache) }
  end

end

Upvotes: 0

Views: 560

Answers (1)

Velusamy Venkatraman
Velusamy Venkatraman

Reputation: 736

ActionView::Template::Error (nil is not a valid asset source): 4: <div class="panel panel-default"> 5: <div class="panel-body"> 6: 7: <%= image_tag current_user.avatar.url%> 8: </div> 9: </div>

<%= image_tag current_user.avatar%>

Dont put url in image tag

Upvotes: 2

Related Questions