ruby on rails - Combine scoped fields_for and one additional form rendering -
let's i'm writing simple todo application. there 3 hardcoded projects user able add tasks (using fields_for
method) basically, form looks that:
# task.first_project, task.second_project , task.third_project scopes # retrieving tasks, belongs chosen project. = form_for setup_project(@project) |f| h3 first project's tasks: = f.fields_for :tasks, f.object.tasks.first_project |ff| = ff.text_field :title = ff.hidden_field :project_id, value: 1 h3 second project's tasks: = f.fields_for :tasks, f.object.tasks.second_project |ff| = ff.text_field :title = ff.hidden_field :project_id, value: 2 h3 third project's tasks: = f.fields_for :tasks, f.object.tasks.third_project |ff| = ff.text_field :title = ff.hidden_field :project_id, value: 3
i have form helper adding 1 empty task form @ end of task forms list:
def setup_project(project) 1.times { project.tasks.build } project end
and that's main problem. code form helper works perfect when i'm using fields_for
without scopes. when i'm adding them, additional form build not shown.
please note: above example simplified version of i'm programming. please not advice me rewrite app have more dynamical projects adding functionality or that. trust me, needs work in such strange way :)
thank help!
i've managed make working using following code:
# task.first_project, task.second_project , task.third_project scopes # retrieving tasks, belongs chosen project. = form_for @project |f| h3 first project's tasks: = f.fields_for :tasks, setup_tasks(f.object.tasks.first_project) |ff| = ff.text_field :title = ff.hidden_field :project_id, value: 1 h3 second project's tasks: = f.fields_for :tasks, setup_tasks(f.object.tasks.second_project) |ff| = ff.text_field :title = ff.hidden_field :project_id, value: 2 h3 third project's tasks: = f.fields_for :tasks, setup_tasks(f.object.tasks.third_project) |ff| = ff.text_field :title = ff.hidden_field :project_id, value: 3
form_helper:
def steup_tasks(tasks) tasks << tasks.build end
but have no idea if that's proper solution. if know better, please share :)
Comments
Post a Comment