Stuart Pontins
Stuart Pontins

Reputation: 285

friendly id path in checkbox_tag rails 4

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

Upvotes: 0

Views: 193

Answers (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).

Upvotes: 1

Related Questions