无法将following_id插入数据库

时间:2014-10-22 02:43:26

标签: ruby-on-rails ruby

这是我的下载用户的简单网页应用,它有错误,请帮帮我:) 我无法将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 view

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 !@users.blank? %>
<% 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%>

当我点击&#34;关注&#34;已发送following_id :(我的current_user id = 9)

{"utf8"=>"✓",
 "authenticity_token"=>"NxOq/F5tOuElvhJNLOvkt/25enUN1wDI05I0fKp998Q=",
 "relation"=>{"following_id"=>"11"},
 "commit"=>"Follow"}

当我在rails控制台中检查Relation.all时,follow_id已经插入,但是当我检查(作为curent_user帐户)user.following时 - 我什么都没看到,没有follow_id。我认为relation_controller在&#34; current_user.following&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;遵循&#34 ;. 我可以关注我的current_user,但这很荒谬:))。所以,请帮帮我!!!!!!

1 个答案:

答案 0 :(得分:0)

在将内容放入数据库之前,需要清理参数。

了解更多http://guides.rubyonrails.org/security.html