这是我的简单网络应用程序,它有一个错误,请帮助我:) 我无法在数据库中插入following_id。我坚持了下来无法在数据库中插入following_id
*这是我application_controller
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include WelcomeHelper
end
* WelcomeHelper
module WelcomeHelper
def login(user)
session[:user_id] = user.id
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
* relation_controller
class RelationController < ApplicationController
def create
follow = User.find(params[:relation][:following_id])
current_user.following << follow
redirect_to current_user
end
* welcome_controller
class WelcomeController < ApplicationController
def index
end
def create
user = User.find_by_username(params[:session][:username])
if user
login user
redirect_to user
else
render 'index'
end
end
def sucess
@users = User.all
@relation = Relation.new
end
end
*关系模型
class Relation < ActiveRecord::Base
attr_accessible :follower_id, :following_id
belongs_to :follower, :class_name => "User"
belongs_to :following, :class_name => "User"
end
*的usermodel
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 CreateRelations < ActiveRecord::Migration
def change
create_table :relations do |t|
t.references :follower
t.references :following
t.timestamps
end
add_index :relations, :follower_id
add_index :relations, :following_id
end
end
*路线
get "welcome/sucess"
get "welcome/error"
root :to => "welcome#index"
get '/users/:id', :to => 'welcome#sucess', :as => "user"
match '/relations', to: 'relation#create', via: 'post'
resources :users
resources :posts
resources :relations
post 'login' => 'welcome#create'
* sucess视图
Following
<ul>
<% current_user.following.each do |u| %>
<li><%= link_to u.username, u %></li>
<% end %>
</ul>
Followed By
<ul>
<% current_user.followers.each do |u| %>
<li><%= link_to u.username, u %></li>
<% end %>
</ul>
List Users<br />
<% if [email protected]? %>
<% for @user in @users %>
<%= @user.username%><br />
<%= form_for @relation do |f| %>
<%= f.hidden_field :following_id, :value => @user.id %>
<%= f.submit "Follow" %>
<% end %>
<%end%>
<%else%>
<%end%>
当我点击 “跟随” following_id已发出:(我CURRENT_USER ID = 9)
{"utf8"=>"✓",
"authenticity_token"=>"NxOq/F5tOuElvhJNLOvkt/25enUN1wDI05I0fKp998Q=",
"relation"=>{"following_id"=>"11"},
"commit"=>"Follow"}
,当我在轨控制台检查Relation.all,该following_id一直插入,但当我检查(作为curent_user帐户)user.following - 我什么都看不到,没有following_id。我认为在“current_user.following < < follow”关系控制器中出现错误。 我只能按照我的current_user,但它是可笑的:))。所以,请帮助我!!!!!!
我为我的current_user创建了会话。当我点击follow时,不同的id已被发送,而不是我的current_user id。我认为这不是问题 – mayoneQD 2014-10-22 03:10:06
谷歌“Rails 4强参数” – OneChillDude 2014-10-22 03:10:33
我不知道如何解决:( – mayoneQD 2014-10-22 06:41:46