4

I'm new to Rails. I'm trying to make an app where students can log in and signup for the exam. I have the following models and controllers which are related to that:

The subject has the following columns:

t.string  "name"  t.integer "ects"
t.integer "year" t.integer "professor_id" 

Its relationship with exam: has_one :exam Its relationship with a professor: belongs_to :professor

Professor has the following columns:

t.string  "first_name"
t.string  "last_name"
t.string  "title" 

Its relationship with exam: has_many :exams Its relationship with subject: has_many :subjects

Exam has the following columns:

t.date    "start_date"  
t.string  "department"  
t.integer "professor_id"  
t.integer "subject_id"

Its relationship with subject: belongs_to :subject Its relationship with a professor: belongs_to :professor Its relationship with signup:

has_many :signups
has_many :signupers, through: :signups, class_name: 'User'

Signup has the following columns:

t.integer "exam_id"
t.integer "user_id"

Its relationship with the exam: belongs_to :exam Its relationship with user: belongs_to :user

I have already connected everything and made a signup button on the exam show view which is linked to new_signup_path. I want data to appear in the form when the user is redirected to it. I would like for the subject name, professor's name and start date to be shown in the form. So, the user has to click the submit button only.

I have made this form, which works, but it's empty:

<%= form_for @signup do |f| %>
    <%= f.fields_for :exam do |e| %>   
       <%= e.label :start_date %>
       <%= e.date_field :start_date %> 
    <% end %> 
    <%= f.submit "Submit", class: "btn btn-large btn-primary" %> 
<% end %>

I also want to have professor's name (Professor model) and subject name (Subject model).

this is my signups controller:

before_action :signed_in_user, only: [:index, :new, :create]

def index
    if current_user.role == 1
        @signups = Signup.all
    else  
        @signups = Signup.where(user_id:current_user.id)
    end
end 

def new
    @signup = Signup.new
end 

def create
    @signup = Signup.new(signup_paramas)
    @signup.user_id = current_user.id
    if @signup.save
        flash[:success] = "You signed up for the exam."
        redirect_to @signups
    else 
        render :new
    end
end
private 
def signup_paramas
    params.require(:signup).permit(:user_id).merge(exam_id: @exam.id)
end

I've been googling this issue for 2 weeks, but I haven't been able to find a solution. I appreciate any kind of help. Thanks in advance :)

Mario Boss
  • 1,784
  • 3
  • 20
  • 43
Aida
  • 85
  • 1
  • 6

1 Answers1

4

On signup button add exam_id to redirect url parameters: new_signup_path(exam_id: @exam.id)

Then in the new action of the signups controller you will be able to access exam:

def new
  @signup = Signup.new
  @exam = Exam.find(params[:exam_id])
end

After this you will be able to use @exam in the view to populate form fields:

<%= form_for @signup do |f| %>
  <%= f.fields_for :exam do |e| %>   
    <%= e.label :start_date %>
    <%= e.date_field :start_date, value: @exam.start_date %> 
  <% end %> 
  <%= f.submit "Submit", class: "btn btn-large btn-primary" %> 
<% end %>

You will also be able to access professor and subject through @exam. This is something to give you an idea from where to start from.

Slava.K
  • 3,073
  • 3
  • 17
  • 28
  • I was trying with this line of code: @exam = Exam.find(params[:exam_id]) before, but it said "couldn't find exam with 'id', because I didn't know I should redirect url parameters. It made sense date_field was empty, since I didn't put any value. I really appreciate your answer. It helped me a lot. – Aida Feb 18 '17 at 16:56
  • I have made the form which shows all the necessary data. http://prntscr.com/eaqwk9 But when I submit it I get: Showing C:/ruby/rails_project/app/views/signups/new.html.erb where line #8 raised: undefined method `subject' for nil:NilClass. It's probably because I didn't allow this in signup_params. I tried putting something like this: params.permit(:exam_id, :user_id).merge(subject_id: @exam.subject.id, professor_id: @exam.professor.id) and I also put the definition of subject and professor in new action, but it doesn't recognize subject or professor i signup_params. – Aida Feb 19 '17 at 17:08