我正在尝试制作一个应用程序来制作属于许多用户的相册,并且用户可以拥有多个相册。但是,每当我尝试创建相册时,我的 @user.album.count 似乎都没有改变。
专辑/new.html.erb
<%= form_for (@album), url: user_albums_path, :html => { :id => "uploadform", :multipart => true } do |f| %>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_area :description %>
<br>
<%=f.submit %>
</div>
<% end %>
相册 Controller
class AlbumsController < ApplicationController
def index
@user = User.find(params[:user_id])
@albums = @user.albums.all
respond_to do |format|
format.html
format.json { render json: @albums }
end
end
def show
@albums = Album.all
@album = Album.find(params[:id])
@photo = Photo.new
end
def update
end
def edit
end
def create
@user = User.find(params[:user_id])
@album = @user.albums.build(params[:album])
respond_to do |format|
if @album.save
format.html { redirect_to user_path(@user), notice: 'Album was successfully created.' }
format.json { render json: @album, status: :created, location: @album}
else
format.html { render action: "new" }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end
def new
@user = User.find(params[:user_id])
@album = Album.new
end
def destroy
end
end
型号
#album model
class Album < ActiveRecord::Base
attr_accessible :avatar, :name, :description
has_many :user_albums
has_many :users, :through => :user_albums
has_many :photos
end
#user model
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 :user_albums
has_many :albums, :through => :user_albums
accepts_nested_attributes_for :albums
end
#photo model (not up to there yet)
class Photo < ActiveRecord::Base
belongs_to :album
end
配置/路线
Pholder::Application.routes.draw do
resources :users do
resources :albums
end
resources :albums do
resources :photos
end
root :to => "users#index"
这是我的控制台
于 2012-10-01 17:41:59 -0400 开始获取 127.0.0.1 的“/assets/application.js?body=1” 服务 Assets /application.js - 304 未修改(0 毫秒) [2012-10-01 17:41:59] 警告无法确定响应正文的内容长度。设置响应的内容长度或设置 Response#chunked = true
于 2012-10-01 17:42:11 -0400 开始为 127.0.0.1 发布“/users/13/albums” 由 AlbumsController#create as HTML 处理 参数: {"utf8"=>"✓", "authenticity_token"=>"ttqQnEt1KFuwvYRU6MuOLgc5UprSE/zMzGMuzzZiZiE=", "album"=>{"name"=>"fdsafds", "description"=>"fdafdsfafdsfadsfsdsa"}, "commit"=>"创建相册","user_id"=>"13"} 用户负载(0.1ms) SELECT "users".* FROM "users"WHERE "users"."id"= ?限制 1 [["id", "13"]] (0.0ms) 开始事务 SQL (0.3ms) INSERT INTO "albums"("created_at", "description", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 01 Oct 2012 21: 42:11 UTC +00:00],[“描述”,“fdafdsfafdsfadsfsdsa”],[“名称”,“fdsafds”],[“updated_at”,2012 年 10 月 1 日星期一 21:42:11 UTC +00:00 ]] (5.6ms) 提交事务 重定向至http://localhost:3000/users/13 9 毫秒内完成 302 发现(ActiveRecord:6.0 毫秒)
最佳答案
好的,我发现了问题。当您通过用户创建相册时,使用构建方法
@album = @user.albums.build(params[:album])
然后就是你要保存的用户,
@user.save
所以你的新创建方法是
def create
@user = User.find(params[:user_id])
@album = @user.albums.build(params[:album])
respond_to do |format|
if @user.save
print @album.errors
format.html { redirect_to user_path(@user), notice: 'Album was successfully created.' }
format.json { render json: @album, status: :created, location: @album}
else
format.html { render action: "new" }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end
关于ruby-on-rails - rails : form submission isn't properly creating attributes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12680561/