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...
Thank you for having answered, I made an update to the question, if you can help a little more I would be grateful...
– pipins