Html + Javascript + Ruby

B

Bla ...

Hi,

I'm with the following code in html + javascript in my ruby application:


<script type=3D"text/javascript">
$(document).ready(function(){
$('#numero_tarefas').change(function(){
$('#tarefas').html('');
for (var tarefa =3D 0; tarefa < $(this).val(); tarefa++) {
$('#tarefas').append('Nome da tarefa ' + (tarefa + 1) + ': '
+ '<input type=3D"text" id=3D"tarefas['+ tarefa +'][input]" name=3D"taref=
as['+
tarefa +'][input]" />&nbsp;');

// copiando as opcoes das sub-tarefas modificando o id
$('[name=3Dsubtarefas-options] select').attr('id', tarefa);
$('#tarefas').append('N=C3=BAmero de subtarefas:'
+$('[name=3Dsubtarefas-options]').html() + '<br /><div
name=3D"div-subtarefas" id=3D"div-subtarefas"></div><br />');

// criacao dos inputs das subtarefas
$('[name=3Doptions_subtarefas]').change(function(){
// recuperar a primeira div
var first_subtarefa =3D $('#' + this.id + ' ~
[name=3Ddiv-subtarefas]').first();
first_subtarefa.html('');
for (var sub_tarefa_id =3D 0; sub_tarefa_id < $(this).val()=
;
sub_tarefa_id++) {
var t =3D parseInt(this.id) + 1;
first_subtarefa.append('&nbsp;&nbsp;&nbsp;&nbsp;Nome da
subtarefa '+ (sub_tarefa_id+1) + ' para a tarefa ' + (t) +
': ' +
<%#*'<input type=3D"text" id=3D"tarefas[' + this.id +
'][sub][' + sub_tarefa_id + ']" name=3D"tarefas[' + this.id + '][sub][' +=

sub_tarefa_id + ']" /><br />');%>
'<input type=3D"text" id=3D"tarefas[' + this.id + '][sub]=
['
+ sub_tarefa_id + ']" name=3D"tarefas[' + this.id + '][sub][' +
sub_tarefa_id + ']" /><br />');
}
});
}
});
});
</script>

How can I take the array witch name is tarefas['+ tarefa +'][input] in
my controller using params?

-- =

Posted via http://www.ruby-forum.com/.=
 
B

blazeroot

Hi,

You can use:

params["+ tarefa +".to_sym][:input]

i guess.

2011/2/18 Bla ... said:
Hi,

I'm with the following code in html + javascript in my ruby application:


=C2=A0 =C2=A0<script type=3D"text/javascript">
=C2=A0 =C2=A0 =C2=A0$(document).ready(function(){
=C2=A0 =C2=A0 =C2=A0 =C2=A0$('#numero_tarefas').change(function(){
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$('#tarefas').html('');
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0for (var tarefa =3D 0; tarefa < $(this)= val(); tarefa++) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$('#tarefas').append('Nome da ta= refa ' + (tarefa + 1) + ': '
+ '<input type=3D"text" id=3D"tarefas['+ tarefa +'][input]" name=3D"taref= as['+
tarefa +'][input]" />&nbsp;');

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0// copiando as opcoes das sub-ta= refas modificando o id
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$('[name=3Dsubtarefas-options] s= elect').attr('id', tarefa);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$('#tarefas').append('N=C3=BAmer= o de subtarefas:'
+$('[name=3Dsubtarefas-options]').html() + '<br /><div
name=3D"div-subtarefas" id=3D"div-subtarefas"></div><br />');

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0// criacao dos inputs das subtar= efas
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$('[name=3Doptions_subtarefas]')= change(function(){
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0// recuperar a primeira div
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0var first_subtarefa =3D $= ('#' + this.id + ' ~
[name=3Ddiv-subtarefas]').first();
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0first_subtarefa.html('');
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0for (var sub_tarefa_id =
=3D 0; sub_tarefa_id < $(this).val();
sub_tarefa_id++) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0var t =3D parseInt= (this.id) + 1;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0first_subtarefa.ap=
pend('&nbsp;&nbsp;&nbsp;&nbsp;Nome da
subtarefa '+ (sub_tarefa_id+1) + ' para a tarefa ' + (t) +
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0': ' +
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0<%#*'<input type=
=3D"text" id=3D"tarefas[' + this.id +
'][sub][' + sub_tarefa_id + ']" name=3D"tarefas[' + this.id + '][sub][' +
sub_tarefa_id + ']" /><br />');%>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0'<input type=3D"te=
xt" id=3D"tarefas[' + this.id + '][sub]['
+ sub_tarefa_id + ']" name=3D"tarefas[' + this.id + '][sub][' +
sub_tarefa_id + ']" /><br />');
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0});
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
=C2=A0 =C2=A0 =C2=A0 =C2=A0});
=C2=A0 =C2=A0 =C2=A0});
=C2=A0 =C2=A0</script>

How can I take the array witch name is tarefas['+ tarefa +'][input] in
my controller using params?
 
B

Brian Candler

Bla ... wrote in post #982469:
How can I take the array witch name is tarefas['+ tarefa +'][input] in
my controller using params?

You can use <input type="hidden"> to put any extra variables you want
into the FORM, then you can POST them, then you'll usually get a params
object in your Ruby controller.

But since you don't show your full HTML I can't be sure you're building
a FORM, and you don't show any ruby code nor say which ruby framework
you're using, it's hard to be more specific.

It would be much better if you could build a simple, minimal and
complete example which demonstrates your problem.
 
B

blazeroot

What parameters do You get in your server log?

There should be something like:

Started POST "/session/" for 127.0.0.1 at Mon Feb 21 09:46:30 +0100 2011
Processing by SessionController#create as HTML
Parameters: {"name"=3D>"admin", "commit"=3D>"logowanie",
"authenticity_token"=3D>"[FILTERED]", "utf8"=3D>"=E2=9C=93",
"password"=3D>"[FILTERED]"}
 
B

Bla ...

getting this:

[4;36;1mSQL (1.3ms)[0m [0;1mINSERT INTO "nome_tarefas_fluxos" =

("fluxo_id", "nome_tarefa", "numero_subtarefas") VALUES(110, =

E'0sub0ccccccccccinputcccccccccc', NULL) RETURNING "id"[0m


in this:


Parameters: {"x"=3D>"12", "numero_tarefas"=3D>"1", "y"=3D>"11", =

"action"=3D>"create", =

"authenticity_token"=3D>"id8C+Wlqc90ryf4479bFISkyCmLhgxr+h8n7A8osLlY=3D",=
=

"tarefas"=3D>{"0"=3D>{"sub"=3D>{"0"=3D>"cccccccccc"}, "input"=3D>"ccccccc=
ccc"}}, =

"options_subtarefas"=3D>"Selecione quantidades...", =

"controller"=3D>"fluxos", "fluxo"=3D>{"nome"=3D>"ccccccccccc"}}
[4;35;1mPermissao Load (2.5ms)[0m [0mSELECT * FROM "permissoes" =

WHERE (usuario_id =3D 2 AND perfil =3D E'Avan=C3=A7ado' AND projeto_id =3D=
0) [0m
[4;36;1mSQL (0.3ms)[0m [0;1mBEGIN[0m
[4;35;1mSQL (1.2ms)[0m [0mINSERT INTO "fluxos" ("created_at", =

"nome", "numero_tarefas", "updated_at", "numero_subtarefas") =

VALUES('2011-02-21 11:20:06.285885', E'ccccccccccc', 1, '2011-02-21 =

11:20:06.285885', NULL) RETURNING "id"[0m
[4;36;1mSQL (7.1ms)[0m [0;1mCOMMIT[0m
[4;35;1mSQL (0.3ms)[0m [0mBEGIN[0m
[4;36;1mSQL (1.3ms)[0m [0;1mINSERT INTO "nome_tarefas_fluxos" =

("fluxo_id", "nome_tarefa", "numero_subtarefas") VALUES(110, =

E'0sub0ccccccccccinputcccccccccc', NULL) RETURNING "id"[0m
[4;35;1mSQL (11.0ms)[0m [0mCOMMIT[0m
Redirected to http://localhost:3001/fluxos
Completed in 61ms (DB: 25) | 302 Found [http://localhost/fluxos]

-- =

Posted via http://www.ruby-forum.com/.=
 
B

blazeroot

So, You have array 'terefas'. You can get that two values "cccccccccc" this=
way:

params[:terefas][0][:sub][0]

and

params[:terefas][0][:input]

as You can see it there:

tarefas"=3D>{"0"=3D>{"sub"=3D>{"0"=3D>"cccccccccc"}, "input"=3D>"cccccccccc=
"}}
 
B

Bla ...

i get this:

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

So, You have array 'terefas'. You can get that two values "cccccccccc" this
way:

params[:terefas][0][:sub][0]

and

params[:terefas][0][:input]

as You can see it there:

tarefas"=>{"0"=>{"sub"=>{"0"=>"cccccccccc"}, "input"=>"cccccccccc"}}
If this doesn't work, try

params["tarefas"]["0"]["sub"]["0"]
and
params["tarefas"]["0"]["input"]
 
B

Bla ...

params["tarefas"]["0"]["input"] works perfectly...


now... i'm whit a while to take all the names... how can I take the
number of "subtarefas" knowing that the html is like this:


<select id="numero_tarefas" name="numero_tarefas">
<option>Selecione quantidades...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
</select>
<div id='tarefas'></div>

<div name="subtarefas-options" style="display:none;">
<select id="options_subtarefas" name="options_subtarefas">
<option>Selecione quantidades...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
</div>



I want to take the number selected in "options_subtarefas" for each
"tarefa" that I toke the "input".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top