我是一个 Rails 新手,我遇到了一个我无法超越的问题。我已经查看了所有内容,但找不到有效的答案。我有一个允许用户建立友谊的应用程序。那部分工作得很好。然而,我最近通过将朋友加入圈子(有点像 Google+)为友谊增加了另一个层次。我遇到的问题是圆圈没有保存在数据库中。以下是正在发生的事情:
Processing by UserFriendshipsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"cupb6S+IdrxqSuEqgfqehJh++POHUMQQ9QqbQVrGtHk=", "user_friendship"=>{"circle_id"=>"9", "friend_id"=>"jimbo"}, "commit"=>"Yes, Add Friend"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."profile_name" = 'jimbo' LIMIT 1
(0.0ms) begin transaction
SQL (0.7ms) INSERT INTO "user_friendships" ("circle_id", "created_at", "friend_id", "state", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?) [["circle_id", nil], ["created_at", Sun, 06 Jan 2013 17:40:30 UTC +00:00], ["friend_id", 2], ["state", "pending"], ["updated_at", Sun, 06 Jan 2013 17:40:30 UTC +00:00], ["user_id", 1]]
SQL (0.2ms) INSERT INTO "user_friendships" ("circle_id", "created_at", "friend_id", "state", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?) [["circle_id", nil], ["created_at", Sun, 06 Jan 2013 17:40:30 UTC +00:00], ["friend_id", 1], ["state", "requested"], ["updated_at", Sun, 06 Jan 2013 17:40:30 UTC +00:00], ["user_id", 2]]
这是表格:
<%= simple_form_for @user_friendship, method: :post do |f| %>
<div class="form-inputs">
<%= f.collection_select :circle_id, current_user.circles.all(:order => 'name'), :id, :name %>
</div>
<div class="form form-actions">
<%= f.hidden_field :friend_id, value: @friend.profile_name %>
<%= f.button :submit, "Yes, Add Friend", class: 'btn btn-primary' %>
<%= link_to "Cancel", profile_path(@friend), class: 'btn' %>
</div>
<% end %>
以下是关联:
class UserFriendship < ActiveRecord::Base
belongs_to :circle
class Circle < ActiveRecord::Base
has_many :friends, through: :user_friendships
在用户到达表单之前已经创建了圆圈。我已经通过迁移将 circle_id 添加到 user_friendships 表中,因此可以保存 id。对于我的生活,我无法弄清楚这里出了什么问题。表格上没有错误,友谊仍然保存,只是没有圈子。我可能做错了什么?
(如果您需要更多信息,请告诉我)
编辑 这是控制器动作
def create
if params[:user_friendship] && params[:user_friendship].has_key?(:friend_id)
@friend = User.where(profile_name: params[:user_friendship][:friend_id]).first
@user_friendship = UserFriendship.request(current_user, @friend)
if @user_friendship.new_record?
flash[:error] = "There was problem creating that friend request."
else
flash[:success] = "Friend request sent."
end
redirect_to profile_path(@friend)
else
flash[:error] = "Friend required"
redirect_to root_path
end
end
这是 user_friendships 表:
create_table "user_friendships", :force => true do |t|
t.integer "user_id"
t.integer "friend_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "state"
t.integer "circle_id"
end
add_index "user_friendships", ["circle_id"], :name => "index_user_friendships_on_circle_id"
add_index "user_friendships", ["state"], :name => "index_user_friendships_on_state"
add_index "user_friendships", ["user_id", "friend_id"], :name => "index_user_friendships_on_user_id_and_friend_id"
这是请求方法:
def self.request(user1, user2)
transaction do
friendship1 = create!(user: user1, friend: user2, state: 'pending')
friendship2 = create!(user: user2, friend: user1, state: 'requested')
friendship1.send_request_email
friendship1
end
end