我目前遇到了将一个模型连接到另一个模型的问题。基本上我希望用户(用户模型)能够通过表单选择特定学校(学校模型),并且用户将在该特定学校之下,直到他们更改它。我无法弄清楚如何做到这一点,任何帮助将不胜感激!谢谢!
用户模型
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
答案 0 :(得分: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_id
作为attr_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