0
我目前正在研究我的第一個rails項目。我正在嘗試製作一個Web應用程序,以便與朋友上傳照片。因此,在創建相冊的表單中,他們應該有一個選擇字段,他們想與之共享相冊的朋友。但是,我有顯示在選擇字段名稱麻煩,我得到一個錯誤Rails:用戶模型的屬性「name」是一個未定義的方法?
即使我的用戶數據庫中有列「名」。我誤解「名稱」的來源?我認爲像@users.name
這樣的屬性意味着進入數據庫並找到每個用戶的名稱值?下面是我的文件(讓我知道如果你需要任何其他文件):
_form.html.erb(具有錯誤)
<%= form_for([@user, @album], :remote => true, :html => { :id => "uploadform", :multipart => true }) do |f| %>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_area :description %>
<%= f.select :user, @users.name %>
<%=f.label :avatar, "Upload" %>
<%=f.file_field :avatar %>
<br>
<%=f.submit %>
</div>
<% end %>
相冊樣板
class Album < ActiveRecord::Base
attr_accessible :avatar, :name
has_and_belongs_to_many :users
mount_uploader :avatar, AvatarUploader
end
用戶模型
class User < ActiveRecord::Base
has_secure_password
attr_accessible :email, :name, :password, :password_confirmation
validates_presence_of :password, :on => :create
validates_format_of :name, :with => /[A-Za-z]+/, :on => :create
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
validates_length_of :password, :minimum => 5, :on => :create
has_many :albums
accepts_nested_attributes_for :albums
end
配置/路由
Pholder::Application.routes.draw do
resources :users do
resources :albums
end
resources :albums do
resources :pictures
end
專輯控制器
class AlbumsController < ApplicationController
def index
@albums = Albums.all
respond_to do |format|
format.html
format.json { render json: @albums }
end
end
def show
end
def update
end
def edit
end
def create
@users = Users.all
@user = User.find(params[:user_id])
@album = @user.albums.build(params[:album])
if @album.save
flash[:success] = "Album created!"
end
end
def new
@user = User.find(params[:user_id])
@album = Album.new
end
def destroy
end
end