0

我正在创建一个用于图片上传的应用程序。有用户、相册、图片。但是,当我登录并创建新专辑时,在重定向时它会出于某种原因将我注销。任何人都可以帮忙吗?

这是我的相册控制器创建操作

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="&#x2713;" /><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
4

1 回答 1

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。

于 2012-10-09T00:01:42.730 回答