I have the following code:
tasks/_form.html.haml
%h3
Tasks
%span.form-inline.centered
= form_for [@activity, @task], remote: true do |f|
= f.text_field :description, {placeholder: 'Enter task description and click + to create a new task', class: 'input-task'}
= f.submit "+", class: 'btn btn-danger'
controllers/tasks_controller.rb
def create
activity = Activity.find(params[:activity_id])
flash[:error] = I18n.t "errors.task.generic" if !activity.add_task!(params[:task])
@task = activity.tasks.last
respond_to do |format|
format.html { redirect_to activity_path(activity) }
format.js
end
end
tasks/create.js.erb
$('#incomplete-tasks').append("<%= j render partial: 'task', locals: {task: @task} %>");
tasks/_task.html.haml
%li
= task.description
= link_to "Done", activity_task_path(task.activity, task: task), html_options = {method: :put, class: 'pull-right'}
The problem that I have is when it tries to use js to render the partial my restful resource activity_task_path sends a get method instead of the put method that I am specifying. If I don’t use js to render it and i just refresh the browser it works fine. I now get a routing error because I do no have a show page. I don’t however need one. I just need it to update my task via a put method. If anybody can shed some light on why this is happening I would appreciate it.
[EDIT1]
If you have any questions or need me to add any code or anything else that I can do to help let me know. I really don’t understand why this is happening. Thank you.
[EDIT2]
def update
activity = Activity.find(params[:id])
activity.update_task!(params[:task])
redirect_to activity_path(params[:id])
end
This is my update controller.
[EDIT3]
You can find the app online at https://trackit.mlpinit.com to get a better understanding of what I am trying to do.
You can authenticate with a random email address I have confirmation set to 2.days.
[EDIT4]
Hopping to make it more clear… Here is the error I get when I add a new task through ajax.
Routing Error
No route matches
{:action=>"show", :controller=>"tasks", :task=>#<Task id: 64, description: "lalala", complete: false, created_at: "2012-10-11 19:41:22", updated_at: "2012-10-11 19:41:22", activity_id: 6>, :activity_id=>#<Activity id: 6, title: "Lala", description: "this is lala", created_at: "2012-10-10 18:37:18", updated_at: "2012-10-10 18:37:18", user_id: 2, activity_group_id: nil>}
Once again if I don’t do this through an ajax call it works ok. My assumption is that the problem takes place because it doesn’t understand the put method for some reason…
[EDIT5]
If I take of the link from my _task partial the append happens without any errors. I thought I should mention that
[EDIT6]
I will add a visual walkthrough to make sure that I make myself understood…
I have a task form:

I enter a task name

I hit (enter)plus to create it.
I get the following error that is also presented above:


If I hit refresh the task was generated ok:

That is why I believe it has to be in the .js.erb file…
activity_task_path expects two id parameters, I think your last is now mixed with the task: task and the post parameters. You also get mixups because of the form_for and trying this method to use ‘update’ for task. You are providing no data fields.
So I think your solution is: