Im new to ruby on rails so i need some tips please.
Im trying to render some checkboxes on the edit view for a user. I have tried to follow the documentation for the nested_attributes but the checkboes does not render. Here is the relation between the two models:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :softwares
has_many :courses
accepts_nested_attributes_for :softwares
accepts_nested_attributes_for :courses
The Edit view for a user
<div class="container">
<div class="row">
<div class="col-md-12 mt-5">
<%= form_for @student, url: auth_student_path(@student), method: :put do |f| %>
<div class="col-md-12 mb-5 mt-5">
<div class="row">
<h3 class="mb-3 filter_heading">Softwares</h3>
<% @softwares.each do |sf|%>
<%= f.fields_for :softwares do |software| %>
<div class="col-md-3">
<div class="courses">
<%= software.label sf.title %>
<%= software.check_box :title, {multiple: true}, sf.title, nil %>
</div>
<% sf.courses.each do |crs|%>
<%= f.fields_for :courses do |course|%>
<div class="mt-1 courses-checkbox">
<%= course.label crs.name %>
<%= course.check_box :name, {multiple: true}, crs.name , nil %>
</div>
<% end %>
<% end%>
</div>
<% end %>
<% end%>
</div>
<div class="form-group">
<%= f.submit "Save", class:"btn btn-primary"%>
</div>
<% end %>
</div>
</div>
</div>
The Controller
module Auth
class StudentsController < ApplicationController
before_action :authenticate_user!
before_action :set_student, only: %i[delete_certificates]
def edit
authorize! :edit, @user
@softwares = Software.all
@student = User.find(params[:id])
end
def update
authorize! :update, @user
@student = User.find(params[:id])
if @student.update(student_params)
redirect_to edit_auth_student_path(@student)
else
redirect_to edit_auth_student_path(@student)
end
end
def show
def set_student
@student = User.find(params[:student_id])
end
private
def student_params
params.require(:user).permit(
:email,
:firstname,
:lastname,
:phone,
:locked,
:approved,
:role,
badges: [],
certificates: [],
softwares_attributes: [:title],
courses_attributes: [:name],
)
end
end
end
Please help me.
f.fields_for :softwaresisn't actually rendering anything its because you haven't actually assigned anything to@student.softwares.fields_forloops through the assocation and if there is nothing to loop through then nothing happens. You're really just using it very wrong here though.@studen.softwaresin the edit view, because initially the Student dosent have any Softwares.f.fields_for :software(notf.fields_for :softwares) the checkboxes will appear but i get unpermitted params in the controller