简而言之,我有一个用户模型和一个照片模型,用户有一个个人资料页面,我想让用户使用载波和嵌套文件将各种照片上传到他们的个人资料。
我正在为用户模型使用Devise。不知道我做错了什么,但我整天都在为这一天搞错,所以任何帮助都会非常感激。
先谢谢了!
错误:
First argument in form cannot contain nil or be empty
<%= nested_form_for @user, :html=>{:multipart => true } do |f| %>
<%= f.error_messages %>
<%= f.fields_for :photos do |photos| %>
user_profile.html.erb中的表单代码:
<%= nested_form_for @user, :html=>{:multipart => true } do |f| %>
<%= f.error_messages %>
<%= f.fields_for :photos do |p| %>
<p>
<%= p.label :photo %><br />
<%= p.file_field :photo %>
</p>
<%= p.link_to_remove "Remove this photo" %>
<% end %>
<%= f.link_to_add "Add attachment", :photos %>
<p><%= f.submit %></p>
<% end %>
注意:请记住,user_profile页面由devise支持,即到目前为止,所有form_for内容都在edit.html.erb页面中。这可能或可能与错误有关,但让我们继续。
User.rb snippit:
has_many :photos, as: :index_photos_on_user_id
accepts_nested_attributes_for :photos
mount_uploader :profile_image, ProfileImageUploader
Photo.rb
class Photos < ActiveRecord::Base
attr_accessible :photo, :user_id
mount_uploader :photo, PhotoUploader
belongs_to :user, :polymorphic => true
end
架构:
create_table "photos", force: true do |t|
t.string "photo"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "photos", ["user_id"], name: "index_photos_on_user_id", using: :btree
create_table "users", force: true do |t|
.... the rest here
sessions_controller.rb
class Users::SessionsController < Devise::SessionsController
def new
@user = User.new
end
private
def after_sign_in_path_for(resource)
if user_signed_in?
welcome_path
else
new_user_registration_path
end
end
end
registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
# POST /resource
def new
@user = User.new
end
def update
# For Rails 4
# account_update_params = devise_parameter_sanitizer.sanitize(:account_update)
# For Rails 3
account_update_params = params[:user]
# required for settings form to submit when password is left blank
if account_update_params[:password].blank?
account_update_params.delete("password")
account_update_params.delete("password_confirmation")
end
@user = User.find(current_user.id)
if @user.update_attributes(account_update_params)
set_flash_message :notice, :updated
# Sign in the user bypassing validation in case his password changed
sign_in @user, :bypass => true
redirect_to after_update_path_for(@user)
else
render "edit"
end
end
def account_registration
@user = User.find(current_user.id)
end
routes.rb snippit
devise_scope :user do
# get "edit/edit_account", :to => "devise/registrations#edit_account", :as => "account_registration"
get 'edit/edit_account' => 'users/registrations#account_registration', as: :edit_account
get '/welcome' => 'home#landing_welcome', as: :welcome
end
答案 0 :(得分:0)
您resource
方法为零或空。所以,你有定义资源。
#app/helpers/application_helper.rb
module ApplicationHelper
## Devise helper method for resource
def resource_name
:user
end
def resource
@resource ||= User.new
end
def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end
......
end
并在自定义控制器中包含此ApplicationHelper模块
#app/controllers/passwords_controller.rb
class PasswordsController < Devise::PasswordsController
include ApplicationHelper
.........
end