rails appでグループを作成していますので、データベースにユーザーを保存する必要があります。ページ。私はcurrent_userを使用することができません。これは、ログインごとに変更されるためです。以下は私が使用しているコードで、結果はActiveRecord::RecordNotFound in GroupsController
です。だから、私はどのように私はこの仕事を正しく行うには、データベースにユーザーを保存する(正しいことなら)知っている必要がありますか、またはそれを正しく動作させる方法は?グループを作成し、ユーザーをデータベースに保存する必要があります。
ユーザモデル
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :groups
end
群モデル
class Group < ActiveRecord::Base
belongs_to :user
end
工夫ユーザ移動(必要な部分のみ)
t.has_many:グループ
グループの移行
t.belongs_to :user
グループコントローラメソッド
def create
@user = User.find(params[:id])
@group = @user.groups.build(params[:id])
respond_to do |format|
if @group.save
format.html { redirect_to @group, notice: 'Group was successfully created.' }
format.json { render json: @group, status: :created, location: @group }
else
format.html { render action: "new" }
format.json { render json: @group.errors, status: :unprocessable_entity }
end
end
end
グループコントローラの新しい方法
def new
@user = User.find(params[:id])
@group = @user.groups.build(params[:id])
respond_to do |format|
format.html # new.html.erb
format.json { render json: @group }
end
end
私は新しいユーザーをここで作成しようとしていません。既存のユーザーを新しいグループに関連付けようとしています。それ、どうやったら出来るの –