ruby form data receive - ruby

Guy
this is just simple form in ruby
<%= form_tag("/page/create", method: "get") do %>
<% 5.times do %>
<%= text_field_tag :Name,(params[:Name]), size: 10 %><br>
<% end %>
<%= submit_tag("send") %>
<% end%>
why in my control the #Name=params['Name'] not work with me well ?
always not gives me any thing ?

You are posting the same paramter multiple times. This leads to an empty params['Name'], except you fill out the last of the input fields.
Most frameworks discard multiple parameters with the same name, using only the last one in the parameter list.
If you want to pass an array of fields to your controller, name your field with trailing open-close square brackets like this: text_field_tag 'Name[]'. This makes Rails populate params['Name'] with an array of your input's values.
Note that :Name[] is not a valid Symbol in Ruby, so you'll have to use a String as the first argument to the text_field_tag helper.

I think you have a few things going on here:
Changing <%= text_field_tag :Name,(params[:Name]), size: 10 %><br> to <%= text_field_tag :Name, size: 10 %><br> might be the first step in moving forward.

Related

Retrieve data using for loop in Ruby

I have a question about using foreach loops in Ruby.
I want to display documents and am using a foreach loop in order to display these documents. It returns an error with the i variable inside of data["response"]["docs"][i]["topic"] which is a JSON string I am iterating over.
I do not understand why that is. Can anyone tell me what I am doing wrong?
If I simply do data["response"]["docs"][0]["topic"] it works fine but not with the i. Why is that?
<%
(0..10).each do |i|
%>
<%= i %> <br/>
<%= data["response"]["docs"][i]["topic"] %>
<%
end
%>
My question is, how many items are there in data["response"]["docs"]? Are there exactly 11? Either way I would use the following code instead:
<% data["response"]["docs"].each_with_index do |item, index| %>
<%= index %>
<br/>
<%= item["topic"] %>
<% end %>
This iterates over the data["response"]["docs"] no matter how many there are (whether is is 1 doc or 20 docs) and stores the value in the variable named item. The each_with_index function gives you the index as well, stored in index, so you can display it later. If you only want the first 11 use:
<% data["response"]["docs"].first(11).each_with_index do |item, index| %>
This will grab a maximum of 11 doc items.
It's hard to tell what might be going wrong because you haven't posted the error, but if you're using a 10-element array, you want to do:
(0..9).each do |i|
With 0-based indexes, you should only use the range from 0-9, rather than 0-10. You may be getting an error because you're trying to access an element that isn't there (i.e. at index 10).
Even better is:
<% data["response"]["docs"].each do |document| %>
<%= document["topic"] %>
<% end %>
or if you need to print the index:
<% data["response"]["docs"].each_with_index do |document, index| %>
<%= index %> <br/>
<%= document["topic"] %>
<% end %>

Trying to break an array into three uneven arrays (Ruby on Rails)

Here's the code that will yield an array (I believe it's an array) of 13 page titles. I'd like to have titles 0-5 be in its own div, 6-8 in a second div and 9-12 in a third, for dropdown menus. I couldn't find this exact question/answer here.
<% #cms_site.pages.root.children.published.each. do |page| %>
<%= link_to page.label, page.full_path %>
<% end %>
Thank you!
What have you attempted? #each is not a very good use for this case. You might want to separate it into 3 different loops like so:
<% #cms_site.pages.root.children.published[0,5].each do |page| %>
<%= link_to page.label, page.full_path %>
<% end %>
<% #cms_site.pages.root.children.published[6,8].each do |page| %>
<%= link_to page.label, page.full_path %>
<% end %>
<% #cms_site.pages.root.children.published[9,12].each do |page| %>
<%= link_to page.label, page.full_path %>
<% end %>
EDIT
It seems like you're having some logic problems, it'd be wise for you to attempt it first at least.
The code up there should work but it's not really DRY and it can be extracted into maybe a helper method that uses the chapters for the iterator or possibly use a different iterator (e.g. each_with_index) and handle the check for each index in the block. There's many ways to go about doing what you asked.
Basically, if you're dealing with an array and you want to take the exact same elements from it each time, here's how to slice it:
# Your Array
elements = [1,2,3,4,5,6,7,8,9,10,11,12]
# This will give you three arrays inside one array. The first will be first six
# elements starting from 0, the second is 3 elements starting from 6, etc.
arrays = [ elements[0,6], elements[6,3], elements[9,3] ]
Now you can iterate through the array and reuse the code to generate the code you want.
arrays.each do |ar|
# Now render for each array as you please, and reuse the same code.
end

Rails- Index in form_for field names

I have a form that I am trying to build to edit multiple records. It's complicated, doesn't map straight to the database, and there can be any number of records. I have the code written so all of the data is passed to the view as a hash. Like this:
#formdata = {"datafield_1"=>"value_1", "datafield_2"=>"value_2"}
What I want to do is to create something like:
f.textfield :datafield_1
f.textfield :datafield_2
f.textfield :datafield_3
etc. etc. etc.
But I don't know how to pass the index of my for loop into the variable name. In short, how do I do :datafield_i where i is my index?
<% %w(1 2 3).each do |i| %>
<%= f.textfield(:"datafield_#{i}") -%>
<% end %>
or
<% #formdata.keys.each do |datafield| %>
<%= f.textfield(datafield.to_sym) -%>
<% end %>

Iterate Form fields

I have build a module to add translations for each standard topic. Theses topic got many standard options and you can translate it directly in page.
I got an issue with my form about the edit view.
When i display a translation it's repeat all value of the f.input :value each time he have one and i want it to display with the each of standard value.
The question is how i can iterate my input field :value in the form to display only once per standard value and not repeat all value translated by standard value.
when i want create a new one all workings fine. It's just about the iterate field who is repeated how many times he got a field in the table.
the gist for my code :
https://gist.github.com/266562670cd8dab28548
Change:
<%= #preference_topic.preference_topic_options.each_with_index do |option, index| %>
<%= f.fields_for option.preference_topic_option_translations.first, option do |translate_form| %>
to:
<%= #preference_topic.preference_topic_options.each_with_index do |option, index| %>
<%= f.fields_for option.preference_topic_option_translations.first || option.preference_topic_option_translations.build, option do |translate_form| %>

glue multiples text boxes into one string

I am coding a web application in ruby on rails.
I have a set of text boxes in each one there is a character and i want to glue all these text boxes in order to make one word.
The text boxes are like this :
1 %>
any ideas ??
Well for starters dont use a for loop, they ugly.
Second I wouldn't use the text_field helper rather the text_field tag
<% (1..10).each do |n| %>
<%= text_field_tag "password[#{n}]" %>
<% end %>
That will return the password all nicely chunked up

Resources