jquery - Can't display dynamic content with Ajax in my rails project -
jquery - Can't display dynamic content with Ajax in my rails project -
i doing simple app , trying implement ajax when creating new task. trying render partial contain new task's info , append existing table, maintain getting next error:
post http://localhost:3000/projects/4/tasks 500 (internal server error)
here task create form
# app/view/tasks/_create_task.html.erb <div class="row"> <div class="col-md-5"> <%=form_for [@project, @task], remote: true |f| %> <div class="form-group"> <%=f.label :task_name %> <%=f.text_field :task_name, class: "form-control"%> </div> <div class="form-group"> <%=f.label :deadline %> <%=date_select :task, :deadline, class: "form-control" %> </div> <%=f.submit "create task", class: 'btn btn-info' %> <%end%> </div> </div>
here controller code task create action
#app/controllers/tasks_controller.rb before_action :set_project def create @task = @project.tasks.build(set_params).save respond_to |format| format.html if @task.valid? flash[:notice] = "your task has been saved" redirect_to project_path(@project) else render 'projects/show' end end format.js end end private def set_params params.require(:task).permit! end def set_project @project = project.find(params[:project_id]) end
and corresponding create.js.erb template
#app/view/tasks/create.js.erb $('#task-table').append("<%= j render 'task', task: @task, project: @project%>");
and partial trying render:
#app/views/tasks/_task <% if task.erased %> <% row_class = 'hidden'%> <% end %> <% if task.erased == nil && task.checked %> <% row_class = 'row-disabled success' %> <% icon_class = 'glyphicon glyphicon-ok check-icon' %> <%elsif task.erased == nil && task.checked == nil %> <% row_class = '' %> <% icon_class = 'glyphicon glyphicon-unchecked check-icon ' %> <% end %> <tr class = "<%= row_class %>" id = 'task-row' > <td> <%= link_to project_check_task_path(project, task, checked: true), method: 'patch' %> <span class= "<%=icon_class %>"></span> <% end %> </td> <td><%= task.task_name %></td> <td><%= task.created_at.strftime("%b %d, %y")%></td> <td><%= task.deadline.strftime("%b %d, %y")%></td> <td> <%= link_to project_erase_task_path(project, task, erase: true), method: 'patch' %> <span class = "glyphicon glyphicon-trash"></span> <% end %> </td> </tr>
note: instead of rendering partial i'have tried append dynamic content such @task.title etc, , throwing same error.
thanks help.!
the issue have might next line:
@task = @project.tasks.build(set_params).save
because @project.tasks.build(set_params).save
returns either true
or false
, while treating task object.
it should be:
@task = @project.tasks.build(set_params) @task.save
or
@task = @project.tasks.create(set_params)
jquery ruby-on-rails ruby ajax
Comments
Post a Comment