我在为用户个人资料页面实施带有carrierwave的头像时遇到问题。我收到错误nil is not a valid asset source
。我认为是因为我不允许:avatar
参数。我不知道如何在Page_controller.rb文件中的个人资料页面中允许使用头像参数。个人资料页面get 'user/:id' => 'pages#profile'
的路线。该路由是一个get请求,用于通过用户名查找用户。
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
答案 0 :(得分:2)
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%>
不要将url放入图片标记