2014-10-22 2 views
0

이것은 다음 사용자를위한 간단한 웹 응용 프로그램입니다. 오류가 있습니다. 제발 도와주세요 :) 데이터베이스에 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' 

* 성공보기

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를 전송했습니다 "에 따라"를 클릭

: 내가 레일 콘솔에서 Relation.all을 검사 할 때 (내 CURRENT_USER 아이디 = 9)

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

의 following_id이있다 삽입,하지만 내가 (curent_user 계정으로) user.following - 난 아무것도 안돼, 다음 _id를 참조하십시오. 나는 "current_user.following < < follow"에서 relation_controller에 뭔가 잘못되었다고 생각합니다. 나는 current_user를 따라갈 수는 있지만 웃긴다. :)). 그래서 제발 나를 도와주세요 !!!!!!

답변

0

데이터베이스에 항목을 넣기 전에 매개 변수를 살균해야합니다.

자세히 알아보기 http://guides.rubyonrails.org/security.html

+0

내 current_user에 대한 세션을 만들었습니다. follow를 클릭하면 differnt id가 전송되고 my current_user id는 전송되지 않습니다. 나는 그것이 문제가 아니라고 생각한다 – mayoneQD

+0

Google "Rails 4 strong parameters" – OneChillDude

+0

나는 그것을 고치는 법을 모른다. :( – mayoneQD

관련 문제