Reputation: 85
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 :)
Upvotes: 4
Views: 3461
Reputation: 3080
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.
Upvotes: 4