Rails 3 - data from form are printed twice - ruby

I have simple form:
<%= form_tag('/paymentShops/newww') do -%>
<table class="adm_prehled"><tr>
<% i = 0%>
<% for dopr in #settings %>
<% i += 1 %>
<td class="posl_import">
<%= check_box_tag 'settings['+dopr.id.to_s+']', dopr.id%>
<%= dopr.value %>
</td>
<% if i == 4%>
</tr><tr>
<% i = 0%>
<%end%>
<% end %>
</tr> </table>
<div><%= submit_tag('Sent') %></div>
<% end -%>
and in controller
def newww
params[:settings].each do |page|
puts page
end
end
and in terminal I am getting always the statement from puts twice... e.g.: I will select the inputs with numbers 3, 5, 9, co I will get in terminal:
33
55
99
Could anyone help, me, please, what is wrong?
Thank you

The solution:
<%= check_box_tag 'settings[]', dopr.id%>

Related

How to reduce rails code efficiently?

In Projects controller I have 5 methods:
def day1
end
def day2
end
def day3
end
def day4
end
def day5
end
In the views I have:
day1.html.erb
day2.html.erb
day3.html.erb
day4.html.erb
day5.html.erb
In each individual view, I have a block of code similar to this:
In day1.html.erb:
<% if current_user.speed? || current_user.admin? %>
"Display Day 1"
<% else %>
<% unless current_user.day_count.nil? %>
<% if (Time.now.to_date - current_user.day_count.to_date).to_i + 1 >= 1 %>
"Display Day 1"
<% else %>
"This project cannot open now. Please wait until day1!"
<% end %>
<% end %>
<% end %>
In day2.html.erb:
<% if current_user.speed? || current_user.admin? %>
"Display Day 2"
<% else %>
<% unless current_user.day_count.nil? %>
<% if (Time.now.to_date - current_user.day_count.to_date).to_i + 1 >= 2 %>
"Display Day 2"
<% else %>
"This project cannot open now. Please wait until day2!"
<% end %>
<% end %>
<% end %>
and so on, so on... until day 5.
In the routes.rb:
match 'projects/day-1' => "projects#day1", :via => [:get], as: "day1"
match 'projects/day-2' => "projects#day2", :via => [:get], as: "day2"
match 'projects/day-3' => "projects#day3", :via => [:get], as: "day3"
match 'projects/day-4' => "projects#day4", :via => [:get], as: "day4"
match 'projects/day-5' => "projects#day5", :via => [:get], as: "day5"
And in index.html.erb views I have:
<% if current_user.speed? || current_user.admin? %>
<div class="day_tracking">
<div class="day_passed"><span>✔</span><p>1</p></div>
<div class="day_passed"><span>✔</span><p>2</p></div>
<div class="day_passed"><span>✔</span><p>3</p></div>
<div class="day_passed"><span>✔</span><p>4</p></div>
<div class="day_passed"><span>✔</span><p>5</p></div>
</div>
<% else %>
<% unless current_user.day_count.nil? %>
<div class="day_tracking">
<% if (Time.now.to_date - current_user.day_count.to_date).to_i + 1 >= 1 %>
<div class="day_passed"><span>✔</span><p>1</p></div>
<% else %>
<div class="day_left"><span>✘</span><p>1</p></div>
<% end %>
<% if (Time.now.to_date - current_user.day_count.to_date).to_i + 1 >= 2 %>
<div class="day_passed"><span>✔</span><p>2</p></div>
<% else %>
<div class="day_left"><span>✘</span><p>2</p></div>
<% end %>
<% if (Time.now.to_date - current_user.day_count.to_date).to_i + 1 >= 3 %>
<div class="day_passed"><span>✔</span><p>3</p></div>
<% else %>
<div class="day_left"><span>✘</span><p>3</p></div>
<% end %>
<% if (Time.now.to_date - current_user.day_count.to_date).to_i + 1 >= 4 %>
<div class="day_passed"><span>✔</span><p>4</p></div>
<% else %>
<div class="day_left"><span>✘</span><p>4</p></div>
<% end %>
<% if (Time.now.to_date - current_user.day_count.to_date).to_i + 1 >= 5 %>
<div class="day_passed"><span>✔</span><p>5</p></div>
<% else %>
<div class="day_left"><span>✘</span><p>5</p></div>
<% end %>
</div>
<% end %>
<% end %>
What the code does is very simple. First, I check to see if the current_user is an admin or a speed user or not. If he is, then display:
✔1 ✔2 ✔3 ✔4 ✔5
( It means he can see all the projects )
Else, If the current_user is not a speed user or an admin, then display:
✔1 ✘2 ✘3 ✘4 ✘5
( It means he can only see the number of projects
equivalent to his day_count ---- in case day_count not nil!)
For Example:
If the current_user have day_count = 1, then he can only see project 1:
✔1 ✘2 ✘3 ✘4 ✘5
If the curent_user have day_count = 2, then he can only see project 1 and 2:
✔1 ✔2 ✘3 ✘4 ✘5
... And so on, so on....
The code is simple, but quite repetitive. I know it's a bad way of coding , but still cannot figure out how to reduce it efficiently and effectively. If I have more than 30 days, the code can go on, go on to more than 2,000 lines of code and probably will make the program become slow down!
Can anyone help me fix this? Thanks in advance.
routes
match 'projects/days/(:day)' => "projects#day", :via => [:get], as: "day"
views
day.html.erb
<% if current_user.speed? || current_user.admin? %>
"Display Day #{#day}"
<% else %>
<% unless current_user.day_count.nil? %>
<% if (Time.now.to_date - current_user.day_count.to_date).to_i + 1 >= #day %>
"Display Day #{#day}"
<% else %>
"This project cannot open now. Please wait until day#{#day}!"
<% end %>
<% end %>
controller
def day
#total_days = 5
#day = params[:day]
end
index.html
<% if current_user.speed? || current_user.admin? %>
<div class="day_tracking">
<% #total_days.each do |day| %>
<div class="day_passed"><span>✔</span><p><%= day %></p></div>
</div>
<% else %>
<% unless current_user.day_count.nil? %>
<div class="day_tracking">
<% #total_days.each do |day| %>
<% if (Time.now.to_date - current_user.day_count.to_date).to_i + 1 >= day %>
<div class="day_passed"><span>✔</span><p><%= day %></p></div>
<% else %>
<div class="day_left"><span>✘</span><p><%= day %></p></div>
<% end %>
<% end %>
</div>
<% end %>
<% end %>
You can like this. There are more refactoring point, It is just concept.
It can be easily re-factored. One of the main tenets of Ruby is the DRY principle and your design goes against it.
Change your route according to this.
match 'projects/:days' => "projects#show_project", :via => [:get]
In your controller you should have this.
def show_project
end
Transfer the business logic to the project model.
def has_access?(no_of_days)
return true if current_user.speed? || current_user.admin?
unless current_user.day_count.nil?
if (Time.now.to_date - current_user.day_count.to_date).to_i + 1 >= no_of_days
true
else
false
end
end
Now in your view its as simple as.
<% if #project.has_access?(params[:days]) %>
"Display Day #{params[:days]}"
<% else %>
"This project cannot open now. Please wait until day#{params[:days]}!"
<% end %>

Why isn't this simple elsif working in Rails/Ruby?

If there is a flash notice > "Flash notice"
If there isn't a flash notice and you're on the homepage > "Welcome"
Code:
<% if flash %>
<% flash.each do |name, msg| %>
<%= content_tag :span, msg, id: "flash_#{name}" %>
<% end %>
<% elsif current_page('/') %>
<% print 'Hi' %>
<% end %>
It prints the flashes correctly but not the welcome on the home page. Doesn't seem to matter if I try current_page or root_url or print 'Welcome' or just plain "Welcome" with no code wrapper. Why?
Yes, almost, I wasn't checking the flash properly at the beginning of the block. This works now:
<% if flash.present? %>
<% flash.each do |name, msg| %>
<%= content_tag :span, msg, id: "flash_#{name}" %>
<% end %>
<% elsif current_page?('/') %>
Welcome to The Spain Report.
<% end %>
Thank you all!

How do I read a CSV file into an HTML table using Ruby?

I am trying to read a simple CSV file into an HTML table to be displayed in a browser, but I'm running into trouble. This is what I'm trying:
Controller:
def show
#csv = CSV.open("file.csv", :headers => true)
end
View:
<% #csv.read %>
<% #csv.headers.each do |head| %>
<%= head %>
<% end %>
<table border="1">
<% #csv.each do |row| %>
<tr>
<% row.each do |element| %>
<td> <%= element %> </td>
<% end %>
</tr>
<% end %>
</table>
Output:
Name Start Date End Date Quantity Postal Code
Basically I am only getting the header, and the CSV body isn't being read and rendered.
This ended up being the final solution:
Controller:
def show
# Open a CSV file, and then read it into a CSV::Table object for data manipulation
#csv_table = CSV.open("file.csv", :headers => true).read
end
View:
<table border="0" cellspacing="5" cellpadding="5">
<tr>
<% #csv_table.headers.each do |header| %>
<th><%= header %></th>
<% end %>
</tr>
<% #csv_table.each do |row| %>
<tr>
<% row.each do |element| %>
<td><%= element[1] %></td>
<% end %>
</tr>
<% end %>
</table>
Reading the CSV into a table was the correct way to go about it, but when iterating through the rows, I had to specify element[1], since element is actually an array of [header, element].
I was able to do the same but didn't need to use the ".read"
def show
# Open a CSV file, and then read it into a CSV::Table object for data manipulation
#csv_table = CSV.open("file.csv", :headers => true)
end
and using the key/value for the elements.
<% #csv_table.each do |row| %>
<tr>
<% row.each do |key, value| %>
<td><%= value %></td>
<% end %>
</tr>
<% end %>

Rails how to access attr_name inside other loop? Column method

My loop:
<% #products.first.attributes.except('name', 'created_at','updated_at','id').each do |attr_name, attr_value| %>
<tr>
<td><span><%= t(attr_name) %></span></td>
<td class="middle">Pr. x</td>
<%= #products.each do |f| %>
<td class="last"><%= f.attr_name %> ,-</td>
<% end %>
<tr>
<% end %>
Then I get this error: undefined method attr_name for #<Product:0x3adc850
How do I use the column method with attr_name? I have tried things like f."#{attr_name}" without luck.
Use send
<%= #products.each do |f| %>
<td class="last"><%= f.send attr_name %> ,-</td>
<% end %>
And you should rename f into product (f is generally used for form builders)

Ruby variable each loop substitution

I need to replace X_VARIABLE in 2 places.
For the first X_VARIABLE I want to keep the text 'remove'
For the second X_VARIABLE I want to keep 'd_cars_path'
<% #cars.each do |x| %>
<% #a = #b.send(x) %>
<% if #a == true %>
<%= button_to "removeX_VARIABLE", X_VARIABLEd_cars_path(:id => #user.id), class: "btn btn-large btn-primary" %>
<% end %>
<% end %>
I am looking for some help with the variable substitution syntax. Thanks.
I'd write:
<% #cars.each do |x| %>
<% if #b.send(x) %>
<%= button_to "remove#{x}",
send(:"#{x}d_cars_path", id: #user.id),
class: "btn btn-large btn-primary" %>
<% end %>
<% end %>

Resources