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 database table, I already researched and I think the best option would be to make a hidden form on this page and associate it with a class, where it would have a method in the jQuery file: p>
$('.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 do not know how to write this form code and if this is the best option.
Update 1: I came to the conclusion that it would be better to do a POST or a PUT I still did not understand the difference in the script, but I can not do it, after much searching, I'm with the request like this:
var formData = {entidade_id: 3, utilizador_id: 1, quadrado_id: 1};
$.ajax({
url: '/quadrado_entidades/1',
type: "PUT",
data: formData,
});
Where in the console tells me that it can not verify 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:
Place in 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
});
...
}
Hint: To build formData see in the Ruby console what parameters are passed when a request is made in this update case, if you generated the table controllers with the scaffold command it is fairly easy to see this, then just build it the same way ...