У меня сейчас проблема с подключением одной модели к другой. По сути, я хочу, чтобы пользователь (модель пользователя) мог выбирать конкретную школу (модель школы) с помощью формы, и пользователь будет находиться под этой конкретной школой, пока не изменит ее. Я не могу понять, как именно это сделать, любая помощь будет высоко ценится! Спасибо!
модель пользователя
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
Sessions Controller (Это код входа в систему)
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