Update BD table by clicking a <li> element

Asked

Viewed 210 times

5

I’m developing a mobile application using jqMobile and Ruby. How to update a database table by clicking on an item in a list.

My list is generated like this:

<ul data-role="listview" data-filter="true" data-filter-theme="a" data-divider-theme="a">
    <% @listaEntidades.each do |entidade| %>
          <li data-icon="false" class="itemPersonalizacao" id="Entidade_<%= entidade.id%>">
               <a href="#"><%= entidade.nome_entidade%></a>
         </li>
    <% end %>
</ul>

Now I do not know how to update the table of the database, I already searched and I think the best option would be to make a hidden form on this page and associate to it a class, where would have a method in the jQuery file:

$('.itemPersonalizacao').click(function(){
    onde podia preencher os campos do form escondido, 
    os quais consigo ter a informação
    e depois faria algo do genero $('.classeForm').submit()
}

Now I don’t know how to write this code from form and if this is the best option.

Update 1: I came to the conclusion that it will be better to do a POST or a PUT I still did not understand the difference in the script, but I am not able to do, after a lot of research, I am with the request so:

var formData = {entidade_id: 3, utilizador_id: 1, quadrado_id: 1};

$.ajax({
    url: '/quadrado_entidades/1',
    type: "PUT",
    data: formData,
});

Where on the console tells me you can’t check the token Authenticity:

Started PUT "/quadrado_entidades/1" for 127.0.0.1 at 2014-06-01 23:15:53 +0100
Processing by QuadradoEntidadesController#update as */*
  Parameters: {"entidade_id"=>"3", "utilizador_id"=>"1", "quadrado_id"=>"1", "id"=>"1"}
Can't verify CSRF token authenticity
Completed 422 Unprocessable Entity in 1ms

Solution:

Put in the application.html.erb:

<head>
   ...
   <%= csrf_meta_tag %>
</head>

Then in the script file method:

$('.itemPersonalizacao').click(function(){
     ...
     var formData = {quadrado_entidade: {entidade_id: idEntidade}, authenticity_token: $("meta[name=csrf-token]").attr('content'), commit: "Update Quadrado entidade"};

     $.ajax({
         url: '/quadrado_entidades/' + idEntradaQuadradoEntidade,
         type: "PATCH",
         data: formData
    });
    ...
}

Tip: To build formData see in the Ruby console that parameters are passed when a request is made in this case of update, if the controllers of the table were generated with the scaffold command is quite easy to see that, then just build the same way...

1 answer

1


Yes, this is one way to do it (create a hidden form and use jQuery to send it).

If you don’t want the form submission to update the page use:

<%= form_for (...), data: { remote: true } do |f| do %>
  (...)
<% end %>

But that’s only possible if you have the Unobtrusive scripting Adapter for jQuery activated (it comes activated by default, is the Gem jquery-ujs).

Another alternative would be to make a POST request, using jQuery, for the route and with the desired data.

See also: Button with a value - Ruby on Rails


Update

Authenticity token is a Rails security feature. It blocks GET requests unless you send an authentication token together.

This complete Stack-EN response gives details on: https://stackoverflow.com/questions/941594/understand-rails-authenticity-token

  • Thank you for having answered, I made an update to the question, if you can help a little more I would be grateful...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.