写真をアップロードするためのアプリを作成しています。ユーザー、アルバム、写真があります。ただし、ログインして新しいアルバムを作成すると、リダイレクト時に何らかの理由でログアウトします。誰か助けてもらえますか?
これが私のアルバムコントローラーの作成アクションです
def create
@user = User.find(params[:user_id])
@album = @user.albums.build(params[:album])
@album.users << @user.friends.find(params[:users][:friend_id])
respond_to do |format|
if @user.save
format.html { redirect_to user_album_path(@user, @album), 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
config /routes
Pholder::Application.routes.draw do
resources :users do
resources :friends
resources :albums do
resources :photos
end
end
end
(これが必要だとは思わないが、見たい場合に備えて、album / new.html.erbのフォームを次に示します)
<%= form_for ([@user, @album]), :html => { :id => "uploadform", :multipart => true } do |f| %>
<div class="formholder">
<%= f.label :name %>
<%= f.text_field :name %>
<%= collection_select(:users, :friend_id, @user.friends, :id, :name_with_initial, :multiple => true ) %>
<%= f.label :description %>
<%= f.text_area :description %>
<br>
<%=f.submit %>
</div>
<% end %>
htmlで生成されたフォーム:
<form accept-charset="UTF-8" action="/users/27/albums" class="new_album" enctype="multipart/form-data" id="uploadform" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="cUx2AZc5Cxa+FZxtOWwHDZ8WSnvb4l9B2BqcWhXYmgg=" /></div>
<div class="formholder">
<label for="album_name">Name</label>
<input id="album_name" name="album[name]" size="30" type="text" />
<select id="users_friend_id" name="users[friend_id]"><option value="29">Ben</option>
<option value="30">Bally</option></select>
<label for="album_description">Description</label>
<textarea cols="40" id="album_description" name="album[description]" rows="20">
</textarea>
<br>
<input name="commit" type="submit" value="Create Album" />
ターミナルログ
Started POST "/users/27/albums" for 127.0.0.1 at 2012-10-08 18:51:11 -0400
Processing by AlbumsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"cUx2AZc5Cxa+FZxtOWwHDZ8WSnvb4l9B2BqcWhXYmgg=", "album"=>{"name"=>"world tour", "description"=>"fadsjkafsdjk"}, "users"=>{"friend_id"=>"29"}, "commit"=>"Create Album", "user_id"=>"27"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "27"]]
User Load (0.2ms) SELECT "users".* FROM "users" INNER JOIN "friendships" ON "users"."id" = "friendships"."friend_id" WHERE "friendships"."user_id" = 27 AND "users"."id" = ? AND (status = 'accepted') LIMIT 1 [["id", "29"]]
(0.0ms) begin transaction
(0.7ms) UPDATE "users" SET "remember_token" = '0lIsogcOIkUMMWFbRWerjw', "updated_at" = '2012-10-08 22:51:11.476530' WHERE "users"."id" = 27
SQL (0.2ms) INSERT INTO "albums" ("created_at", "description", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Mon, 08 Oct 2012 22:51:11 UTC +00:00], ["description", "fadsjkafsdjk"], ["name", "world tour"], ["updated_at", Mon, 08 Oct 2012 22:51:11 UTC +00:00]]
SQL (0.2ms) INSERT INTO "album_users" ("album_id", "created_at", "updated_at", "user_id") VALUES (?, ?, ?, ?) [["album_id", 113], ["created_at", Mon, 08 Oct 2012 22:51:11 UTC +00:00], ["updated_at", Mon, 08 Oct 2012 22:51:11 UTC +00:00], ["user_id", 29]]
SQL (0.2ms) INSERT INTO "album_users" ("album_id", "created_at", "updated_at", "user_id") VALUES (?, ?, ?, ?) [["album_id", 113], ["created_at", Mon, 08 Oct 2012 22:51:11 UTC +00:00], ["updated_at", Mon, 08 Oct 2012 22:51:11 UTC +00:00], ["user_id", 27]]
(6.3ms) commit transaction
Redirected to http://localhost:3000/users/27/albums/113