2012-07-16 219 views
2

我剛完成了Ruby on Rails 3教程。最後一章相當複雜。整本書的教程基本上創建了一個用戶可以發佈Microposts的網站。另外,每個用戶可以關注任何其他用戶,然後以下用戶的Microposts將顯示在原始用戶的Micropost供稿上。作爲參數值的二維數組

我的問題與爲什麼RelationshipsController內部的創建操作使params變量包含二維數組有關。

這是代碼。

用戶

class User < ActiveRecord::Base 
    attr_accessible :email, :name, :password, :password_confirmation 
    has_secure_password 
    has_many :microposts, dependent: :destroy 
    has_many :relationships, foreign_key: "follower_id", dependent: :destroy 
    has_many :followed_users, through: :relationships, source: :followed 
    has_many :reverse_relationships, foreign_key: "followed_id", 
     class_name: "Relationship", dependent: :destroy 
    has_many :followers, through: :reverse_relationships, source: :follower 
end 

微柱

class Micropost < ActiveRecord::Base 
    attr_accessible :content 
    belongs_to :user 
end 

關係

class Relationship < ActiveRecord::Base 
    attr_accessible :followed_id 

    belongs_to :follower, class_name: "User" 
    belongs_to :followed, class_name: "User" 
end 

我認爲這是創建二維PARAMS變量的代碼(但是爲什麼呢?)

<%= form_for(current_user.relationships.build(followed_id: @user.id), remote: true) do  
    |f| %> 
    <div><%= f.hidden_field :followed_id %></div> 
    <%= f.submit "Follow", class: "btn btn-large btn-primary" %> 
<% end %> 

RelationshipsController

class RelationshipsController < ApplicationController 
    def create 
    @user = User.find(params[:relationship][:followed_id]) 
    current_user.follow!(@user) 
    respond_to do |format| 
     format.html { redirect_to @user } 
     format.js 
    end 
    end 
end 

所以也許我只是回答我自己的問題,但我我從未見過一個params變量的二維數組。有人可以對此有所瞭解嗎?

哦,也許我應該張貼我的routes.rb文件,以及:

SampleApp::Application.routes.draw do 
    resources :users do 
    member do 
     get :following, :followers 
    end 
    end 
    resources :sessions, only: [:new, :create, :destroy] 
    resources :microposts, only: [:create, :destroy] 
    resources :relationships, only: [:create, :destroy] 

    root to: 'static_pages#home' 
end 

感謝, 麥克

回答

3

答案很簡單:

這不是一個2維數組,它是一個嵌套關聯數組。你使用它的原因是爲了得到你真正想要的領域。


龍Aswer:從教程

假設:當用戶點擊關注按鈕,我們的目標是調用current_user.follow!(other_user)。我會告訴你代碼是如何實現這個的。所有的魔法都在關係控制器和視圖中的form_for函數中。

首先,您爲新的關係製作表格。因爲它是嵌套資源,所以通過關聯構建它。

current_user.relationships.build 

但是,僅與一個用戶對應的全新的Relationship對象並不意味着太多。相反,傳遞一個值的關聯數組來初始化該對象。在這種情況下,其他用戶的ID。因此,您將正在構建的Relationship對象的:followed_id屬性分配給@ user.id或您正在嘗試遵循的用戶。

current_user.relationships.build(followed_id: @user.id) 

當你在一個對象上form_for時,你可以訪問該對象的屬性。在這種情況下,如果我們查看關係模型,則只能訪問:followed_id。

class Relationship < ActiveRecord::Base 
    attr_accessible :followed_id 

最後,我們需要捕獲followed_id在表單提交,因爲形式的目標是能夠調用current_user.follow!(other_user)點擊後續按鈕時。因此,我們將followed_id作爲隱藏字段傳遞,因此可以在控制器的params中訪問,但用戶在視圖本身中也看不到它。

<%= f.hidden_field :followed_id %> 

最後,當按鈕被點擊,因爲形式是一種新的關係對象,創建操作被調用關係控制器。在那裏,訪問與形式的關係,對象你這樣做相同的方式,其他形式的教程 -

params[:relationship] 

但你不希望關係的對象,你想要一個的用戶對象跟隨,所以你可以撥打follow!。這很容易。只需從ID中找到數據庫中的用戶。如何獲得followed_id?它是表單中的Relationship對象的一個​​屬性。

params[:relationship][:followed_id] 

我認爲它值得一提的是,當你犯了一個新的用戶對象,請使用PARAMS [:用戶]。這只是一個關聯數組,你可以訪問它的字段,如果你想要

params[:user][:name] 

希望這是有道理的。它只是一個嵌套的關聯數組,Rails用它來跟蹤參數,例如提交表單的參數。

+0

哇,非常感謝你@AJcodez!我一直在討論Rails,幾個月來閱讀了大量的書籍,一直未能到達「aha」時刻。我來自PHP,並且剛剛開始討論Rails(或許我並不像我想象的那麼聰明)。但感謝您的詳細解答。這說得通。 – mikeglaz 2012-07-16 19:24:44

+0

一個簡單的問題,但。 f.hidden_​​field的followed_id中的值與傳遞到current_user.relationships.build中的@ user.id中的值是否相同? – mikeglaz 2012-07-16 19:34:08

+0

就是這樣 - form_for使您可以訪問表單所用對象的屬性。這就是爲什麼你需要傳遞f並且調用f。{field_type} ... 我認爲你會非常喜歡rails和ruby,如果你還沒有:) – AJcodez 2012-07-16 21:03:14