0

I'm new to rails and am having what I think is a very simple issue.

I have a list of "tasks". When you click on a task, I want to update its row in the database to mark it as complete (changing the "status" column form 0 to 1).

Here is what the links look like in my view:

<td><%= link_to t.name, change_task_status_path(:id => t.id) %>

And here is what's in my tasks_controller.rb:

def change_task_status
  @t = Task.find_by_id(params[:id])
  @t.status = '1' # 1 = complete
  @t.save
  render :nothing => true
end

I cannot figure out how to format the link correctly! I get this error when loading the view:

undefined method `change_task_status_path' for #<#<Class:0x3a6c144>:0x3a69d54>

EDIT rake routes shows:

       tasks GET    /tasks(.:format)             tasks#index
             POST   /tasks(.:format)             tasks#create
    new_task GET    /tasks/new(.:format)         tasks#new
   edit_task GET    /tasks/:id/edit(.:format)    tasks#edit
        task GET    /tasks/:id(.:format)         tasks#show
             PUT    /tasks/:id(.:format)         tasks#update
             DELETE /tasks/:id(.:format)         tasks#destroy
      phases GET    /phases(.:format)            phases#index
             POST   /phases(.:format)            phases#create
   new_phase GET    /phases/new(.:format)        phases#new
  edit_phase GET    /phases/:id/edit(.:format)   phases#edit
       phase GET    /phases/:id(.:format)        phases#show
             PUT    /phases/:id(.:format)        phases#update
             DELETE /phases/:id(.:format)        phases#destroy
    projects GET    /projects(.:format)          projects#index
             POST   /projects(.:format)          projects#create
 new_project GET    /projects/new(.:format)      projects#new
edit_project GET    /projects/:id/edit(.:format) projects#edit
     project GET    /projects/:id(.:format)      projects#show
             PUT    /projects/:id(.:format)      projects#update
             DELETE /projects/:id(.:format)      projects#destroy
nathan
  • 514
  • 1
  • 7
  • 20
  • Run `rake routes` to see what your route names actually are. – MrDanA Nov 13 '12 at 17:54
  • I editing my question to show my rake routes response. The path doesn't seem to be showing up at all. – nathan Nov 13 '12 at 17:57
  • Try taking a look at this link for some more information: http://guides.rubyonrails.org/routing.html. When you add a controller action, you don't automatically get access to it. You have to define it in your routes.rb file. Then, depending how you define it, it will determine what your helper link looks like. – MrDanA Nov 13 '12 at 18:06

1 Answers1

1

Put this in your routes.rb:

resources :tasks do
  member do
    get :change
  end
end

It will add the helper path change_task passing the task id.
And change your link to this:

<td><%= link_to t.name, change_task_path(:id => t.id) %>

And the controller:

def change

EDITED:

To make it an ajax call, you got it right, add :remote => true to your link like this:

<%= link_to t.name, change_task_path(:id => t.id), :remote => true %>    

This way, the response on your controller is expected to be on a js format.

def change
  # do your thing
  respond_to do |format|
    format.js
  end
end

When you do this, you are expected to have a change.js.erb file on your views folder that make all the changes to the page. Something like this:

$('#tasks_list').children().remove();
$('#tasks_list').html(
"<%= j(render('tasks_list')) %>"
);

Remember that if you do things this way, you will need a partial(_tasks_list.html.erb).

MurifoX
  • 14,991
  • 3
  • 36
  • 60
  • Yeah, change the name of the method to `def change`. Forgot about that, sorry. =] – MurifoX Nov 13 '12 at 18:05
  • You are the man! Thank you!! One last request. Is there a simple way to make this an ajax call that runs in the background? I know there's something about using :remote => true, but am not sure of where that goes in the link format. – nathan Nov 13 '12 at 18:12
  • Perfecto! That did the trick. I actually left out the respond_to as I just need to add a class to the link to show that it has been changed, which i do with jquery onClick. Thanks again! – nathan Nov 13 '12 at 18:36