插入following_id时出错,然后检查了development.log:
这是我的错误:follower_id = nil
[1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 9]]
[1m[36mUser Load (1.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", "11"]]
[1m[36mSQL (3.0ms)[0m [1mINSERT INTO "relations" ("created_at", "follower_id", "following_id", "updated_at") VALUES (?, ?, ?, ?)[0m [["created_at", Thu, 23 Oct 2014 07:19:22 UTC +00:00], ["follower_id", nil], ["following_id", 11], ["updated_at", Thu, 23 Oct 2014 07:19:22 UTC +00:00]]
我想成为:follower_id = 9,在第1行和第2行之间改变
[1m[36mUser Load (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", "9"]]
[1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "11"]]
[1m[36mSQL (3.0ms)[0m [1mINSERT INTO "relations" ("created_at", "follower_id", "following_id", "updated_at") VALUES (?, ?, ?, ?)[0m [["created_at", Thu, 23 Oct 2014 07:19:22 UTC +00:00], ["follower_id", 9], ["following_id", 11], ["updated_at", Thu, 23 Oct 2014 07:19:22 UTC +00:00]]`
那么,我怎样才能将其更改为真?我的代码出了点问题?请帮助我!
*这是我的用户模型
class User < ActiveRecord::Base
attr_accessible :pass, :username
# Who am I following?
has_many :relations, :foreign_key => :follower_id
has_many :following, :through => :relations
# Who am I followed by?
has_many :relations, :class_name => "Relation", :foreign_key => :following_id
has_many :followers, :through => :relations
validates :username, :pass, :presence => true
validates :username, :pass, :length => { :minimum => 4 }
validates :username, :uniqueness => true
*关系模型
class Relation < ActiveRecord::Base
belongs_to :follower, :class_name => "User"
belongs_to :following, :class_name => "User"
end
*关系控制器
class RelationController < ApplicationController
def create
follow = User.find(params[:relation][:following_id])
@current_user.following << follow
redirect_to follow
end
*我的ApplicationController
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include WelcomeHelper
before_filter :set_current_user
private
def set_current_user
@current_user = User.find(session[:user_id]) if session[:user_id]
end
end
*我的行动观点
<h2><%= @user.username%><br /></h2>
<%= @user.pass%><br />
<% if @current_user && @current_user != @user %>
<% if @current_user.following.include?(@user) %>
<%= link_to "Unfollow",
relation_path(@current_user.relation_for(@user)),
:method => :delete %>
<% else %>
<%= form_for @relation do |f| %>
<%= f.hidden_field :following_id, :value => @user.id %>
<%= f.submit "Follow" %>
<% end %>
<% end %>
<% end %>
<br>
<ul>
<% @user.following.each do |u| %>
<li><%= link_to u.username, u %></li>
<% end %>
</ul>
Followed By
<ul>
<% @user.followers.each do |u| %>
<li><%= link_to u.username, u %></li>
<% end %>
</ul>
这是我插入following_id
的动作def create
follow = User.find(params[:relation][:following_id])
@current_user.following << follow
redirect_to follow
end
已发送我的following_id
{"utf8"=>"✓",
"authenticity_token"=>"DIY42kGR9fZ54Pub+CDMuNNaHZ0aFnzSiWrkOFRBsdE=",
"relation"=>{"following_id"=>"11"},
"commit"=>"Follow"}