'Can I make an array / multiple references in ruby on rails
I'm working on a project and I need to be able to set one or more task to an event, for example in garden, there is a Client and we need to cut the grass and clean the pool, instead of creating 2 events I want to create one where the 2 tasks appears and be able to select both of them in the form.
Models
class Client < ...
has_many :events
class Event
belongs_to :user
has_and_belongs_to_many :task
belongs_to :client
class Task
has_many :assigned_tasks
I was thinking that in the controller of the events, do something like this, but it didn't work, it says it can't map
def event_params
params.require(:event).permit(:user_id, :client_id , task_ids:[])
end
in the form I tryed using collection but it didnt work, I would like to do a drop down of checkboxes or dots, but I don't know hot to do it and by doing it all with checkboxes, it gave the same error as map, it couldn map nil class nil
form
<%=form.collection_check_boxes :task_id,@tasks, :id,:title %>
or
<%=form.collection_check_boxes :task_ids,@tasks, :id,:title %>
undefined method `map' for nil:NilClass
Solution 1:[1]
I found out how to solve it, In the controller I needed to change :task_id to , task_id:[] and in the form add
<%= form.collection_check_boxes :task_id, @tasks, :id, :title, multiple: true%>
multiple: true now the thing is That it can't show I made
event.task.each and gave me error, it says PG::UndefineTable there is no relation <<events_tasks>>
But when i do
events.task_id. each
it allows it but I can't show the title of the task now, please help
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jose Tomas anabalon |
