• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

264
Vistas
Rails 4: Toggling a boolean value with link_to

I'd like to do a link to change a boolean value by ajax.

First of all, I tried this tutorial, but it didn't work as well for rails 4.

I wish someone could shed light on the best practice of making it work.

I currently have the code below.

#ROUTE
scope 'admin', :module => "admin" do

    resources :users do
      collection do
        put 'toggle/', :action => :toggle_approve

      end
    end

  end

#CONTROLLER
def toggle_approve
  @user = User.find(params[:id])
  @user.toggle!(:approved)
  render :nothing => true
end

<!-- VIEW -->
<% @users.each do |u| %>
                <tr class="gradeA">
                  <td><%= u.name %> <%= u.last_name %></td>
                  <td><%= u.country %></td>
                  <td><%= u.created_at.strftime("%d/%m/%Y - %H:%M") %></td>
                  <td>
                    <%= link_to '<i class="fa fa-pencil"></i>'.html_safe, edit_user_path(u), class: 'btn btn-icon-toggle', "data-toggle "=> "tooltip", "data-placement" => "top", "data-original-title" => "Edit User" %>
                    <%= link_to '<i class="fa fa-trash-o"></i>'.html_safe, url_for(:controller => "users", action: :destroy, id: u.id), method: :delete, data: {confirm: "Are you sure?"}, class: 'btn btn-icon-toggle', "data-toggle "=> "tooltip", "data-placement" => "top", "data-original-title" =>"Delete" %>

                    <%= link_to "approve", toggle_users_path(u), :remote => true %>
                  </td>
                </tr>
              <% end %>

The path toggle_users_path(u) is returning users/toggle.2 instead of users/toggle/2

Someone have a better way to use the remote: true to make it work in the better way?

Thanks!

about 3 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You have not set up a route that accepts an id in the URL, so it's interpreting it as the format placeholder (hence the .2 in the URL, which is usually reserved for a format like json, pdf, or csv).

Instead, the route should look like this, drawing on member instead of collection:

resources :users do
  member do
    put 'toggle', :action => :toggle_approve
  end
end

That will hang the pattern off of the user's id, like this:

/users/:user_id/toggle

Then try this in your view:

<%= link_to "approve", toggle_user_path(u), :method => :put, :remote => true %>

As you can see, you also need to post it along with a put method to hit your put endpoint.

about 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda