Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The very first thing is that, as your guardian model suggests, you do not have any field to store student's reference i guess. So, please add that first like add a field student_id in guardians model. Also you do not have admission_no field either in guardians model. So, if you want to store admission_no then add a field for this also in DB, if you just want to display the value, in guardian model add </p> <pre><code>attr_accessor :admission_no </code></pre> <p>Now, since in your new action of GuardiansController, you are creating an object which is not yet associated with any record in students table. So using nested resource like bachan said in his answer you can achieve the goal.</p> <p>Create the nested resource :- </p> <pre><code>resources :students do resources :guardians end </code></pre> <p>and then in new action of GuardiansController</p> <pre><code>def new @student = Student.find(params[:student_id]) @guardian = Guardian.new end </code></pre> <p>Now replace your form_for with</p> <pre><code>&lt;%= form_for([@student, @guardian]) do |f| %&gt; ............. &lt;%= f.text_field :admission_no, :value =&gt; @student.admission_no %&gt; ............. &lt;% end%&gt; </code></pre> <p>if you want to store id of the student also in guardians table. If not then you can use</p> <pre><code>&lt;%= form_for(@guardian, :url =&gt; student_guardians_path(@student), :method =&gt; :post) do |f| %&gt; ............. &lt;%= f.text_field :admission_no, :value =&gt; @student.admission_no %&gt; ............. &lt;% end%&gt; </code></pre> <p>Hope it will help you.</p>
    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.
 

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