Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: Connecting one model to another through forms
    primarykey
    data
    text
    <p>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!</p> <p><strong>user model</strong></p> <pre><code>class User &lt; ActiveRecord::Base attr_accessible :name, :email, :password, :password_confirmation, :biography, :avatar has_secure_password belongs_to :school, foreign_key: "school_id" end </code></pre> <p>I have nothing defined for anything of school in the user model cause I am not sure what to put.</p> <p><strong>school model</strong></p> <pre><code>class School &lt; ActiveRecord::Base attr_accessible :name, :school_id has_many :users validates :school_id, presence: true end </code></pre> <p><strong>user controller</strong></p> <pre><code>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 </code></pre> <p><strong>school controller</strong></p> <pre><code>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 </code></pre> <p><strong>Form for User with school inside</strong></p> <pre><code>&lt;%= simple_form_for @user do |f| %&gt; &lt;%= f.label :Name %&gt;&lt;/br&gt; &lt;%= f.text_field :name, :class =&gt; 'modal_settingfield' %&gt; &lt;/br&gt;&lt;/br&gt; &lt;%= f.label :Email %&gt;&lt;/br&gt; &lt;%= f.text_field :email, :class =&gt; 'modal_settingfield' %&gt; &lt;/br&gt;&lt;/br&gt; &lt;%= f.label :Password %&gt; &lt;span class='faded'&gt;(Leave blank to not change)&lt;/span&gt; &lt;%= f.password_field :password, :class =&gt; 'modal_settingfield'%&gt; &lt;/br&gt;&lt;/br&gt; &lt;%= f.label :Password_Confirmation %&gt; &lt;span class='faded'&gt;(Leave blank to not change)&lt;/span&gt; &lt;%= f.password_field :password_confirmation, :class =&gt; 'modal_settingfield'%&gt; &lt;/br&gt;&lt;/br&gt; &lt;%= f.label :School_id %&gt; &lt;span class='faded'&gt;(Leave blank to not change)&lt;/span&gt; &lt;%= f.association :school, collection: @schools, label_method: :name, value_method: :id, prompt: "Select a school", :label =&gt; false, :required =&gt; false %&gt; &lt;%= f.label :Biography %&gt; &lt;span class='faded'&gt;(Please limit Biography to &lt;span class='TextCounter'&gt;&lt;/span&gt;letters)&lt;/span&gt;&lt;/br&gt; &lt;%= f.text_area :biography, :class =&gt; 'modal_settingfield'%&gt; &lt;/br&gt;&lt;/br&gt; &lt;%= f.label :Photo %&gt; &lt;span class='faded'&gt;(Add a Profile Picture)&lt;/span&gt;&lt;/br&gt; &lt;%= f.file_field :avatar %&gt; &lt;%= f.submit 'Update', :class =&gt; 'update_button' %&gt; &lt;% end %&gt; </code></pre> <p><strong>Current School</strong></p> <pre><code>def current_school @current_school ||= School.find(session[:school_id]) if session[:school_id] end helper_method :current_school </code></pre> <p><strong>Sessions Controller(This is login code)</strong></p> <pre><code>def create user = User.find_by_email(params[:email]) if user &amp;&amp; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload