2012-02-14 64 views
2

我目前遇到了將一個模型連接到另一個模型的問題。基本上我希望用戶(用戶模型)能夠通過表單選擇特定的學校(學校模型),並且用戶將在該特定學校之下,直到他們改變它爲止。我無法弄清楚如何去做這件事,任何幫助將不勝感激!謝謝!Rails:通過表單將一個模型連接到另一個模型

用戶模型

class User < ActiveRecord::Base 
    attr_accessible :name, :email, :password, :password_confirmation, :biography, :avatar 
    has_secure_password 
    belongs_to :school, foreign_key: "school_id" 
end 

我已經在用戶模型事業學校的任何定義,我不知道要放什麼東西沒有。

學校模式

class School < ActiveRecord::Base 
    attr_accessible :name, :school_id 

    has_many :users 

    validates :school_id, presence: true 

end 

用戶控制器

def create 
    @user = User.new(params[:user]) 
    if @user.save 
    redirect_to @user 
    else 
    redirect_to current_school 
    end 
end 

def update 
@user = current_user 
if @user.update_attributes(params[:user]) 
    flash[:notice] = "Successfully updated profile." 
    redirect_to home_path 
else 
    redirect_to current_school 
end 
end 

學校控制器

def create 
    school = School.find(params[:name]) 
    if school 
    session[:school_id] = school.id 
    redirect_to school_path(school) 
    end 
end 

def show 
    @school = School.find(params[:id]) 
    @user = User.new 
end 

<%= simple_form_for @user do |f| %> 
<%= f.label :Name %></br> 
<%= f.text_field :name, :class => 'modal_settingfield' %> 
</br></br> 
<%= f.label :Email %></br> 
<%= f.text_field :email, :class => 'modal_settingfield' %> 
</br></br> 
<%= f.label :Password %> <span class='faded'>(Leave blank to not change)</span> 
<%= f.password_field :password, :class => 'modal_settingfield'%> 
</br></br> 
<%= f.label :Password_Confirmation %> <span class='faded'>(Leave blank to not change)</span> 
<%= f.password_field :password_confirmation, :class => 'modal_settingfield'%> 
</br></br> 
<%= f.label :School_id %> <span class='faded'>(Leave blank to not change)</span> 
<%= f.association :school, collection: @schools, label_method: :name, value_method: :id, prompt: "Select a school", :label => false, :required => false %> 
<%= f.label :Biography %> <span class='faded'>(Please limit Biography to <span class='TextCounter'></span>letters)</span></br> 
<%= f.text_area :biography, :class => 'modal_settingfield'%> 
</br></br> 
<%= f.label :Photo %> <span class='faded'>(Add a Profile Picture)</span></br> 
<%= f.file_field :avatar %> 
<%= f.submit 'Update', :class => 'update_button' %> 
<% end %> 

當前學校表格用戶與學校

def current_school 
    @current_school ||= School.find(session[:school_id]) if session[:school_id] 
end 
helper_method :current_school 

會話控制器(這是登錄代碼)

def create 
    user = User.find_by_email(params[:email]) 
    if user && user.authenticate(params[:password]) 
    if params[:remember_me] 
     cookies.permanent[:auth_token] = user.auth_token 
    else 
     cookies[:auth_token] = user.auth_token 
    end 
    redirect_to home_path 
    else 
    flash.now.alert = "Invalid email or password" 
    redirect_to current_school 
    end 
end 

def destroy 
    cookies.delete(:auth_token) 
    redirect_to current_school 
end 

回答

1

我建議simple_form gem這種類型的事情,一般來說更容易,清潔和較少詳細的形式。請參閱集合部分。

你必須像這樣的代碼:

<%= simple_form_for @user do |f| %> 
    <%= f.input :name %> 
    <%= f.input :school_id, collection: @schools, label_method: :name, value_method: :id, prompt: "Select a school" %> 
    <%= f.button :submit %> 
<% end %> 

這假設@schools變量被呈現這種形式的控制器操作設置。我假設這些將是School對象,有:name:id方法。

此外,您School模型可能不應該有school_idattr_accessible,我想你想這對User模式,以驗證規則一起。你也不需要明確設置外鍵,如果你按照常規的命名,即:

class User < ActiveRecord::Base 
    attr_accessible :school_id, :name, :email, :password, :password_confirmation, :biography, :avatar 
    has_secure_password 
    belongs_to :school 

    validates :school, presence: true 
end 
+0

我實現了simple_form_for和一個我正在爲f.input一個未定義的方法:用戶和兩個if我刪除了,值和標籤不在f.input上:school_id,它只是一個文本區域,我很困惑。很抱歉,我是新來的鐵路和學習的不便之處。 編輯 我實際上使用f.association而不是f.input,它現在正在顯示,唯一的問題是當我更新時,它不會更新 – Kellogs 2012-02-14 23:41:25

+0

@BrianKim對不起我的壞。我的複製和粘貼錯誤;本示例直接從simple_form_for自述文件中獲取。 「輸入」的參數是屬性,例如:名稱。我編輯了答案以糾正此問題。抱歉。 – 2012-02-15 04:49:28

+0

@BrianKim關於你的評論的後半部分:f.association可能是更好的選擇,但它只是在下面調用f.select。你現在可以再次發佈你的完整表單嗎?用戶控制器的更新和創建操作,以幫助排除故障? – 2012-02-15 04:53:35

相關問題