Html + Javascript + Ruby

Hi,

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

<script type="text/javascript">
  $(document).ready(function(){
    $('#numero_tarefas').change(function(){
      $('#tarefas').html('');
      for (var tarefa = 0; tarefa < $(this).val(); tarefa++) {
        $('#tarefas').append('Nome da tarefa ' + (tarefa + 1) + ': '
  • ’ ');

          // copiando as opcoes das sub-tarefas modificando o id
          $('[name=subtarefas-options] select').attr('id', tarefa);
          $('#tarefas').append('Número de subtarefas:'
    

+$(’[name=subtarefas-options]’).html() + ‘

’);

        // criacao dos inputs das subtarefas
        $('[name=options_subtarefas]').change(function(){
        // recuperar a primeira div
          var first_subtarefa = $('#' + this.id + ' ~

[name=div-subtarefas]’).first();
first_subtarefa.html(’’);
for (var sub_tarefa_id = 0; sub_tarefa_id < $(this).val();
sub_tarefa_id++) {
var t = parseInt(this.id) + 1;
first_subtarefa.append(’    Nome da
subtarefa ‘+ (sub_tarefa_id+1) + ’ para a tarefa ’ + (t) +
‘: ’ +
<%#*’
’);%>
‘<input type=“text” id="tarefas[’ + this.id + ‘][sub][’

  • sub_tarefa_id + ‘]" name="tarefas[’ + this.id + ‘][sub][’ +
    sub_tarefa_id + ‘]" />
    ’);
    }
    });
    }
    });
    });

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

Hi,

You can use:

params[“+ tarefa +”.to_sym][:input]

i guess.

2011/2/18 Bla … [email protected]:

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 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.

nether params[:tarefas]

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”=>“admin”, “commit”=>“logowanie”,
“authenticity_token”=>“[FILTERED]”, “utf8”=>“✓”,
“password”=>“[FILTERED]”}

2011/2/21 Bla … [email protected]:

params["+ tarefa +".to_sym][:input] doesn’t works.

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”=>“12”, “numero_tarefas”=>“1”, “y”=>“11”,
“action”=>“create”,
“authenticity_token”=>“id8C+Wlqc90ryf4479bFISkyCmLhgxr+h8n7A8osLlY=”,
“tarefas”=>{“0”=>{“sub”=>{“0”=>“cccccccccc”}, “input”=>“cccccccccc”}},
“options_subtarefas”=>“Selecione quantidades…”,
“controller”=>“fluxos”, “fluxo”=>{“nome”=>“ccccccccccc”}}
[4;35;1mPermissao Load (2.5ms)[0m [0mSELECT * FROM “permissoes”
WHERE (usuario_id = 2 AND perfil = E’Avançado’ AND projeto_id = 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]

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.[]

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”}}

2011/2/21 Bla … [email protected]:

On Mon, Feb 21, 2011 at 8:40 AM, blazeroot [email protected] wrote:

tarefas"=>{“0”=>{“sub”=>{“0”=>“cccccccccc”}, “input”=>“cccccccccc”}}

If this doesn’t work, try

params[“tarefas”][“0”][“sub”][“0”]
and
params[“tarefas”][“0”][“input”]

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:

Selecione quantidades... 1 2 3 4 5 6 7 8 9 10 11 12 13 14
    <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”.

Paste here code where You’re trying to get that value, please.

2011/2/21 Bla … [email protected]: