我正在创建一个图片上传应用。有用户,专辑,图片。但是,当我登录并创建新相册时,在重定向上,它会因为某些原因将我注销。有人可以帮忙吗?
这是我的相册控制器创建操作
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
配置/路由
Pholder::Application.routes.draw do
resources :users do
resources :friends
resources :albums do
resources :photos
end
end
end
(不要认为这是必要的,但这里是专辑/ 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
答案 0 :(得分:2)
查看您的代码(链接在下面的评论中),这是问题所在。保存相册时,您正在调用@ user.save!这会触发用户模型中的create_remember_token,导致注销。
以下是您在相册控制器中调用@ user.save的代码:
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
以下是用户模型中的before_save:
class User < ActiveRecord::Base
....
before_save :create_remember_token
您可能想要消除before_save,然后在用户登录时调用create_remember_token。