How to set rails form helper with counter - html

my Model structur is:
<CheckBreak id: 1, procedure_id: "1", br_typ_id: 1,
lok_typ: nil, lok_nr: nil, log_gestellnr: nil, auftrag: nil,
bemerkungen: nil, date1: nil, pruefer: nil, sachkundiger: nil,
freigeber: nil, lok_bremse_soll: nil, lok_bremse_ist: nil,
zug_bremse_soll: nil, zug_bremse_ist: nil, einzeldruck: nil,
dauerdruck: nil, c1_nr: nil, c1_point: nil, c1_io: nil, c1_nio: nil,
c1_inst_io: nil, c1_date: nil, c1_users: nil, c2_nr: nil,
c2_point: nil, c2_io: nil, c2_nio: nil, c2_inst_io: nil,
c2_date: nil, c2_users: nil, c3_nr: nil, c3_point: nil, c3_io: nil,
c3_nio: nil, c3_inst_io: nil, c3_date: nil, c3_users: nil,
c4_nr: nil, c4_point: nil, c4_io: nil, c4_nio: nil, c4_inst_io: nil,
c4_date: nil, c4_users: nil, c5_nr: nil, c5_point: nil, c5_io: nil,
c5_nio: nil, c5_inst_io: nil, c5_date: nil, c5_users: nil,
c6_nr: nil, c6_point: nil, c6_io: nil, c6_nio: nil, c6_inst_io: nil, c6_date: nil, c6_users: nil, c7_nr: nil, c7_point: nil, c7_io: nil,
c7_nio: nil, c7_inst_io: nil, c7_date: nil, c7_users: nil,
c8_nr: nil, c8_point: nil, c8_io: nil, c8_nio: nil, c8_inst_io: nil,
c8_date: nil, c8_users:......
Now i will write my rails form code with a counter var.
like this
<% counter = 1%>
<tr>
<td><%= form.check_box : <%=counter%>_io%> </td>
<td class="th-sm" align=center><%= form.check_box :c1_nio%></td>
...
</tr>
<% counter += 1%>`
How can i do this?
Is there an name for this kind of code reduction?
I found a very dirty solution.
The term called iteration
i can use in the form pure html tags with embeded ruby
<input type="text" name="check_break[c<%=counter%>_users]" id="check_break_c<%=counter%>_users">
But this is so ugly and it feels so wrong.
i have to consider my DB-Model architecture. I will start a new post to that Question.

While you could generate the inputs dynamically with something like:
<%= 1.upto(9).each do |number| %>
<%= form.label :"c#{number}nio" %>
<%= form.check_box :"c#{number}nio" %>
# ...
<%= end %>
Your gut feeling that something is off is very right. Having an insane number of columns and potential nulls per row is not a viable solution.
Instead normalize the data and think of it in terms of tables with relations between them:
# make sure you set the correct types and choose a name that matches your domain
rails g model thing check_break:references nr point io nio inst_io date user
Then create a one to many assocation:
class CheckBreak < ApplicationRecord
has_many :things
accepts_nested_attributes_for :things
end
class Thing < ApplicationRecord
belongs_to :check_break
end
And then use fields_for to create nested inputs for each nested record:
<%= form.fields_for(:things) do |thing_form| %>
<%= thing_form.label :nio %>
<%= thing_form.check_box :nio %>
# ...
<% end %>
This will automatically result in an array of hashes in the params[:things_attibutes] attribute.
def check_break_params
params.require(:check_break)
.permit(
:foo,
:bar,
things_attibutes: %i[nr point io nio inst_io date user]
)
end
See Rails Guides: Building Complex Forms.

Related

Rails - sql commands not working with datetime

I have a strange problem with my web app.
I am trying to build a simple reservation system for a game like "Escape the room". For now I made my own calendar looking a railscast (Calendars revised) for having the dates.
For the possible hours I made a new model and a new table in the database
t.integer "id"
t.datetime "start_time"
t.datetime "end_time"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
In the view I have something like this:
<%= calendar #date do |date| %>
<%= date.day %>
<table class="table">
<tbody>
<% #hours.each_slice(2) do |hour| %>
<tr>
<% hour.each do |h| %>
<td>
<%= link_to h.start_time.strftime('%H:%M'), new_escape_path(date: date, start_time: h.start_time.strftime('%H:%M'), end_time: h.end_time.strftime('%H:%M')) %>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<% end %>
And I write these parameters into the database passing them in a form where user can complete other fields manually:
<%= text_field_tag :date, params[:date] %><br>
<%= text_field_tag :start_time, params[:start_time] %> do
<%= text_field_tag :end_time, params[:end_time] %>
The problem is that in the view I want to show only the hours which are not reserved yet, which means that they are not in the escapes table.
When in the console I write
Escape.all
I have something like:
#<Escape id: 1, date: "2015-08-11 00:00:00", start_time: "2015-08-10 08:30:00", end_time: nil, user_id: nil, created_at: "2015-08-10 15:26:14", updated_at: "2015-08-10 15:26:14">
but when I write
Escape.all.where(date: "2015-08-11 00:00:00")
it returns an empty record
#<ActiveRecord::Relation []>
I tried also to insert something like this in the view, but it's not working:
<% if Escape.where(["date = ? and start_time = ?", "#{date}", "#{h}"])%>
<%= h.start_time.strftime('%H:%M') %>
<% else %>
<%= link_to h.start_time.strftime('%H:%M'), new_escape_path(date: date, start_time: h.start_time.strftime('%H:%M'), end_time: h.end_time.strftime('%H:%M')) %>
<% end %>
Do you have any ideas? What am I doing wrong?

Database data displaying on html (rails)

I am making a rails app that users can use to shop for products. Everything was working fine, then I adjusted the products#index page, and now the data from the database is displaying on the html under the <div> of the page like so;
[#<Product id: 1, name: "Green", description: "coming from the hills of the emerald triangle, thi...", strain: "Sativa", price: #<BigDecimal:7fbe558f8228,'0.2E2',9(18)>, image_file_name: "http://i.imgur.com/NZkyXpr.jpg", created_at: "2015-04-12 22:56:42", updated_at: "2015-04-12 22:56:42", gram_price: nil, eighth_price: nil, quarter_price: nil, product_type: nil>, #<Product id: 3, name: "Sour Diesel", description: "Sour, sometimes called Sour D, is an invigo...", strain: "Hybrid", price: #<BigDecimal:7fbe55902bd8,'0.2E2',9(18)>, image_file_name: "http://i.imgur.com/RUHZAXQ.jpg", created_at: "2015-04-13 01:42:35", updated_at: "2015-04-13 01:42:35", gram_price: nil, eighth_price: nil, quarter_price: nil, product_type: nil>, #<Product id: 2, name: "Grand Daddy Purp", description: "Introduced in 2003 by Ken Estes, Granddaddy Purple...", strain: "Indica", price: #<BigDecimal:7fbe559015d0,'0.2E2',9(18)>, image_file_name: "http://i.imgur.com/8O5kXeL.jpg", created_at: "2015-04-13 01:41:17", updated_at: "2015-04-13 07:31:14", gram_price: nil, eighth_price: nil, quarter_price: nil, product_type: nil>]
Here is my index page, along with my controller
index.html.erb
<section class="product-page">
<div class="container">
<div class="row text-center">
<%= #products.each do |product| %>
<div class="product col-md-4 text-center">
<h2><%= link_to product.name, product_path(product.id) %></h2>
<%= link_to image_tag(product.image_file_name),
product_path(product.id), :class => 'img-responsive' %>
<div class="product-info">
<div class="product-info-left"><%= product.descrip %></div>
<div class="product-info-right"><%= product.price %></div>
</div>
</div>
<% end %>
</div>
</div>
</section>
products_controller.rb
class ProductsController < ApplicationController
def index
#products = Product.all
end
def show
#product = Product.find(params[:id])
end
end
Does anyone know why the data from the database would be displaying on my html page? Did I forget to close a tag, is there something in my controller? I can also attach an image that shows what it looks like if my description isn't clear. Any tips would be much appreciated.
<%= #products.each do |product| %>
must be
<% #products.each do |product| %>

Remove attribute text showing up after post on Rails site

I'm making a random site to post gifs and images to for fun.
After I post something, the attributes show up under it, like this
[#Funpost id: 1, title: "Giphy Test", description: "HAHAHA SO FUNNY", url: "http://i.giphy.com/NuQ1Tz0fhqeEU.gif", created_at: "2015-01-20 00:21:34", updated_at: "2015-01-20 00:21:34">, #<Funpost id: 2, title: "test", description: "", url: "test", created_at: "2015-01-20 03:21:19", updated_at: "2015-01-20 03:21:19">
How do I get rid of it?
Thanks!!
You probably have an extra equals signs in your ERB code. e.g., if you do
<%= #funposts.each do |f| %>
<%= f.title %>
<%= f.description %>
<% end %>
You will end up displaying the funposts attributes as well as the title and description for each. However if you instead remove the equals sign from the first line
<% #funposts.each do |f| %>
<%= f.title %>
<%= f.description %>
<% end %>
You will only show the title and description for each funpost.

Using a before_filter in application_controller to access a model in application.html.erb leads to unexpected html

I want to access a model in my application.html.erb file. For this I defined a before_filter in the application_controller.rb:
before_filter :populate_ages
protected
def populate_ages
#ages= Ages.all
end
In my application.html.erb I have the following:
<%= #ages.each do |age| %>
<p><%= age.name %></p>
<% end %>
The names are rendered correctly but I get additional output which is the array #ages. It looks like this:
[#<ageid: 1, name: "Herren", created_at: "2014-06-26 08:02:58", updated_at: "2014-06-26 08:02:58">, #<ageid: 2, name: "A-Jugend", created_at: "2014-06-26 08:02:58", updated_at: "2014-06-26 08:02:58">, #<ageid: 3, name: "B-Jugend", created_at: "2014-06-26 08:02:58", updated_at: "2014-06-26 08:02:58">] </ul>
I have this code in a navigation bar. So my navigation bar is extended by this additional text. Adding the loop in the body leads to the same output.
You don't want the = sign on the loop, change it to:
<% #ages.each do |age| %>
<p><%= age.name %></p>
<% end %>
By having <%= #ages ... you're telling rails to display the array
Just write as
<% #ages.each do |age| %>
<p><%= age.name %></p>
<% end %>
#ages is an Array instance. Now Array#each returns the receiver itself, when full iteration is completed. <%=.. %> means you are telling execute the each m,method and print the result of the #each method too, which is the problem, you have faced. Thus tell your template executed the method, don't print the result of it, and to do this correct syntax is <%.. %>.

Cannot create assoication between new objects for nested model form in rails

I am watching on railscasts and tried to write the example with my own hands.
But I got an trouble with the step of: Creating The Form.
It requires creating association between Survey and Question.
But this association cannot be established in my rails application so no questions appear in the form
Here are the codes
Survey model:
class Survey < ActiveRecord::Base
attr_accessible :name, :questions
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions
end
Question model:
class Question < ActiveRecord::Base
attr_accessible :context, :survey_id
belongs_to :survey
end
Surveys_Controller method:
# GET /surveys/new
# GET /surveys/new.json
def new
#survey = Survey.new
3.times {#survey.questions.build}
respond_to do |format|
format.html # new.html.erb
format.json { render json: #survey }
end
end
question part in _form.html.erb
<% f.fields_for :questions do |builder|%>
<%= builder.label :context, "Question" %><br />
<%= builder.text_area :context, :rows => 3 %>
<% end %>
Here is what I got from testing in console
irb(main):010:0> #survey = Survey.new
=> #<Survey id: nil, name: nil, created_at: nil, updated_at: nil>
irb(main):011:0> 3.times {#survey.questions.build}
=> 3
irb(main):012:0> #survey.questions
=> [#<Question id: nil, survey_id: nil, context: nil, created_at: nil, updated_a
t: nil>, #<Question id: nil, survey_id: nil, context: nil, created_at: nil, upda
ted_at: nil>, #<Question id: nil, survey_id: nil, context: nil, created_at: nil,
updated_at: nil>]
You have a new object for model. You create 3 question for empty object. You questions must be empty. It no saved or defined.
If I understand your question correctly, you are not seeing questions in your form. You need to have one form for one question in your form. Are you doing questions.each in your form?
You need something like:
<% survey.questions.each do |question| %>
<%= fields_for question do |builder| %>
haha, i am being stupid.
It is a dumb question.
It doesn't matter about the association.
I made a typing mistake on
<% f.fields_for :questions do |builder|%>
it should return some texts inserting into HTML document rather than just processing as ruby code
it should be <%= f.fields_for :questions do |builder|%>
But thank you all of your answers :)