0

I'm currently getting this in my checkboxes data-url "/todos?id=f483e4a8cb1a728f" when it should just be "/todos/f483e4a8cb1a728f" I'm using friendly id for the random slugs.

Currently I'm calling it as data: { remote: true, url: url_for(controller: :todos, id: todo), method: "PATCH" } id: todo and I have tried todo.id but that gives me the number of the post which I don't want - I want the slug.

Does anyone know how I could get around this?

Thank you kind sirs.

EDIT: More Context as Requested

<%= check_box_tag 'todo[completed]', todo.id, todo.completed, data: { remote: true, url: url_for(controller: :todos, id: todo), method: "PATCH" }, id: todo.id %>
<%= label_tag todo.id, "COMPLETE", :class => 'strikethrough' %>

This is how I'm calling it - as I want to complete my todo on the index rather than going in and updating on todos/:id/edit. However, it gives me an error when I click the checkbox because the URL is like this "/todos?id=f483e4a8cb1a728f" when it should just be "/todos/f483e4a8cb1a728f"

EDIT:

My Action

def completed
    if @todo.update_attributes(:completed => params[:completed])
      flash[:success] = "Wowzers."
      redirect_to dashboard_path
    else
      flash.now[:error] = "Not so wowzers..."  
      render :new  
    end
  end

My Routes

resources :todos do
    member do
      # post 'completed'
      patch 'todos/:id' => 'todos#completed'
    end 
  end
Stuart Pontins
  • 285
  • 4
  • 14

1 Answers1

1

First of all, you need a route configured as patch '/todos/:id'.

If you already have, put the name of the action in the url_for parameters, like:

url_for(controller: :todos, action: :something, id: todo.id)

If you haven't that action, you have to create it.

The url_for is returning /todos?id=f483e4a8cb1a728f because it is considering the index action as default, and as this path don't have an :id parameter inside the path, the helper put it as a parameter (query string).

  • I have an action set up already, but adding in patch '/todos/:id' has given me an error 'Missing :controller key on routes definition, please check your routes.' I have never seen it before, could you please clarify? – Stuart Pontins Apr 30 '16 at 19:05
  • How did you defined it? I think the simplest syntax is `patch 'todos/:id' => 'todos#your_action'`. – André Guimarães Sakata Apr 30 '16 at 19:14
  • Ohh sorry, my bad. The only problem now is it displays the id twice '/todos/e3309e65a5f225ec696807ee1f5be3/todos/e3309e65a5f225ec696807ee1f5be3' or 'todos/19/todos/19' if I do id: todo.id as oppose to just todo – Stuart Pontins Apr 30 '16 at 19:19
  • That's weird... isn't the route definition duplicated too? Like: `todos/:id/todos/:id`? – André Guimarães Sakata Apr 30 '16 at 19:27
  • Yeah, the route and the ID seem to be duplicated :/ I can't see why it has, I'll just look over my checkbox_tag again and make sure I didn't delete anything or forget to do something after adding the action: :completed – Stuart Pontins Apr 30 '16 at 19:30
  • upon doing a rake routes it's set to /todos/:id/todos/:id(.:format) but I'm really not sure why. Is this because of how I set up the route? Could you please take a look above at my edit? – Stuart Pontins Apr 30 '16 at 19:38
  • Yes, that's because you put it inside a `member` block. If you want to keep it like that, just put `patch '/' => 'todos#completed'`, or move it outside that block. – André Guimarães Sakata Apr 30 '16 at 19:44