'Undefined local variable or method `form' for class when passing locals: form:form through a partial
I currently have 3 separate views, two using AJAX to create/edit tasks.
I'm trying to set a date that the task is due, and would like to present some options to the user for today, one week from today, two weeks for today, all contained within one partial that I can use across all three forms.
I thought I'd do this using AJAX, and simply update that one area of the form to change the default value of the variable, however, I can't render the form.
If there is a better way of doing this instead please let me know.
I get the following error:
ActionView::Template::Error (undefined local variable or method `form' for #<#<Class:0x00007fe8a4ccd8e8>:0x00007fe8a4b64df8>
Did you mean? for
fork):
1: $("#date-container").html(" <%= j render partial: "goal_tasks/set_due_date", locals: { task: @goal_task, form: form } %>");
edit.html.erb (with extra fields snipped):
<%= render 'layouts/pagetitle', title: "Edit Task" %>
<%= form_for([@goal_task.goal, @goal_task]) do |form| %>
<div class="form-group col-md-10 col-md-offset-1">
<%= render 'layouts/errors', object: @goal_task %>
<div class="form-group col-md-12" id="date-container">
<%= render partial: "goal_tasks/set_due_date", locals: { task: @goal_task, form: form } %>
</div>
<div class="form-group col-md-12">
<%= form.submit class:"btn btn-success", value: "Edit Task" %>
</div>
</div>
<% end %>
routes:
resources :goals do
resources :goal_tasks do
member do
get :set_task_due_date
end
end
_set_due_date.html.erb:
<% if local_assigns[:date] %>
<%= form.hidden_field :taskduedate, value: date %>
<% elsif local_assigns[:due_date] %>
<div class="control-label col-sm-3">
<%= form.label :taskduedate, "Task due:" %>
</div>
<div class="col-sm-9 form-align-left">
<%= form.date_select(:taskduedate, selected: due_date) %>
</div>
<% elsif task.taskduedate.present? %>
<div class="control-label col-sm-3">
<%= form.label :taskduedate, "Task due:" %>
</div>
<div class="col-sm-9 form-align-left">
<%= form.date_select :taskduedate %>
</div>
<% else %>
<div class="control-label col-sm-3">
<%= form.label :taskduedate, "When is your task due? " %>
</div>
<div class="col-sm-9 form-align-left">
<%= form.date_select :taskduedate %>
</div>
<% end %>
<span class="p-4">
<%= link_to "Today", set_task_due_date_goal_goal_task_path(task.goal, task, :due_date => Date.today), remote: true %> |
<%= link_to "Next week", set_task_due_date_goal_goal_task_path(task.goal, task, :due_date => Date.today + 7.days), remote: true %> |
<%= link_to "Two weeks", set_task_due_date_goal_goal_task_path(task.goal, task, :due_date => Date.today + 14.days), remote: true %>
</span>
set_task_due_date.js.erb:
$("#date-container").html(" <%= j render partial: "goal_tasks/set_due_date", locals: { task: @goal_task, form: form } %>");
controller:
def set_task_due_date
due_date = params[:due_date]
respond_to do |format|
format.js
end
end
Solution 1:[1]
The solution, thanks to Muhammad Abdullah Khalil via a Rails slack channel is that I had to declare the form inside of the partial.
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 | Olliedee |
