I am currently running into a problem of connecting one model to another. Basically I want a user (user model) to be able to select a specific school (school model) through a form and the user will be under that specific school until they change it. I can’t figure out how to exactly go about doing this, any help will be much appreciated! Thank you!
user model
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :biography, :avatar
has_secure_password
belongs_to :school, foreign_key: "school_id"
end
I have nothing defined for anything of school in the user model cause I am not sure what to put.
school model
class School < ActiveRecord::Base
attr_accessible :name, :school_id
has_many :users
validates :school_id, presence: true
end
user controller
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
school controller
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
Form for User with school inside
<%= 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 %>
Current School
def current_school
@current_school ||= School.find(session[:school_id]) if session[:school_id]
end
helper_method :current_school
Sessions Controller(This is login code)
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
I suggest the simple_form gem for this type of thing, and in general for easier, cleaner and less verbose forms. See the section on Collections.
You’d have code like this:
This assumes that the
@schoolsvariable is set by the controller action that renders this form. I’m assuming that these would beSchoolobjects, having:nameand:idmethods.Also, your
Schoolmodel should probably not haveschool_idasattr_accessible, I think you want this on theUsermodel, along with the validation rule. You also don’t need to set the foreign key explicitly if you’re following the conventional naming, i.e.: