Rails form_for in function of another value - ruby-on-rails

I'm trying to use the form_for helper in order to create a form that has a variable number of text fields.
on one page, I have a form that takes in two values :
<%= form_for :specs, url: specs_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :sections %><br>
<%= f.text_field :sections %>
</p>
<p>
<%= f.submit %>
</p>`
<% end %>
This take me to another page where I would like to have a form that would have a variable number of text fields. The number of fields would be = to <%= #specs.sections %>
I haven't added a regex yet ;) (I'll do it once I'm sure this is the best way)
if <%= #specs.sections %> is two, i'd want to have a form that looks like :
<%= form_for :sections, url: sections_path do |f| %>
<p>
<%= f.label :head_chord %><br>
<%= f.text_field :head_chord %>
</p>
<p>
<%= f.label :section_1_chord %><br>
<%= f.text_field :section_1_chord %>
</p>
<p>
<%= f.label :section_2_chord %><br>
<%= f.text_field :section_2_chord %>
</p>
<p>
<%= f.label :foot_chord %>
<%= f.text_field :foot_chord%>
</p>
<p>
<%= f.label :camber %>
<%= f.text_field :camber %>
</p>
<p>
<%= f.label :draft_position %>
<%= f.text_field :draft_position %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
</p>
(section is the only variable aspect.)
I appreciate any suggestion!
Thanks for reading :D

If you want to pass a variable number of parameters to your server, you'd better put them in an array. This way your controller can handle them more easily. So instead of section_1_chord, section_1_chord, ..., go with section_chord[]. You can do that this way:
<%= form_for :sections, url: sections_path do |f| %> <p>
<p>
<%= f.label :head_chord %><br>
<%= f.text_field :head_chord %>
</p>
<%= #specs.sections.each_with_index do |section, index| %>
<p>
<%= label_tag "section_chord-#{index}", "section #{index}"%><br>
<%= text_field_tag 'section_chord[]', nil, id: "section-chord-#{index}" %>
</p>
<% end %>
This way rails treats section_chord as an array. This code is untested an will most likely need some clean up, but it should work.

Related

Displaying an objects properties different from default behavior in form helper rails

How can I go about displaying the :cords property below as a formatted array itself? and not just it's values. Given the form below:
<%= form_for #group do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :cords %><br>
<%= f.text_field :cords, name: "group[cords]" %>
</p>
<p>
<%= f.label :members %><br>
<%= f.text_field :members %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
When editing the object, I'd get something like
But what I want to see is the full raw data, like so:
I think this can be done with to_s, but i'm not sure how to go about getting this behavior
You could try using an input tag instead text_field helper:
<input type="text" name="group[cords]" value="<%= group.cords %>">

How save multiple values from array in field?

In model Project i have next array:
PAYMENT_TYPE = ["in bar", "bargeldlose Zahlung", "E-Geld"]
I have table projects with field payments, type: string
When i create new object(model Project)how me add values from array PAYMENT_TYPE in field payments?
In view new.html.erb i added
<%= Project::PAYMENT_TYPE.each do |type| %>
<%= f.check_box :payment, :name => 'project[payment][]', type %>
<% end %>
Displaying error: /home/ubuntu/workspace/app/views/projects/new.html.erb:35: syntax error, unexpected ')', expecting => ...=> 'project[payment][]', type );#output_buffer.safe_append=' ...
full new.html.erb:
<%= form_for #project do |f| %>
<p>
<%= f.label 'Name' %>
<%= f.text_field :title %>
</p>
<P>
<%= f.label 'Budget' %>
<%= f.text_field :price %>
für
<%= f.select :price_category, Project::PRICE_CATEGORIES %>
</P>
<p>
<%= f.label 'Description' %>
<%= f.text_area :description %>
</p>
<p>
<%= f.label 'Category' %>
<%= f.collection_select :category_id, Category.all, :id, :name %>
</p>
<p>
<%= f.label 'Skills Freelancer (15 pieces) *' %>
<%= f.text_field :skill %>
</p>
<p>
<%= f.label 'Location' %>
<%= f.text_field :location %>
</p>
<p>
<%= Project::PAYMENT_TYPE.each do |type| %>
<%= f.check_box :payment, :name => 'project[payment][]', type %>
<% end %>
</p>
<p>
<b>Anonymity order</b>
</p>
Setup allows for players to remain anonymous. Note that this may reduce the number of responses.
<p>
<%= f.label 'Place Order anonymously' %>
<%= f.check_box :anonymity %>
</p>
<p>
<%= f.fields_for :documents do |d| %>
<%= f.label :attachment %>
<%= f.file_field :attachment %>
<% end %>
</p>
<P>
<%= f.submit 'Create' %>
</P>
<% end %>
I can't google the correct syntax for this form, but you can't have naked hash params like you do except in the last parameter of a method call in Ruby. That is, your code is invalid Ruby.

How to make a player Validation in .erb file of Ruby on Rails solution

In my Ruby on Rails solution (Views) I made a html.erb page in which I have the following code:
<h1>New Game</h1>
<%= form_for #game do |f| %>
<p>
<%= f.label :player_1 %><br/>
<%= f.text_field :player_1 %>
</p>
<p>
<%= f.label :player_2 %><br/>
<%= f.text_field :player_2 %>
</p>
<p>
<%= f.submit "Create Game" %>
</p>
<% end %>
The task is to make a validation of the players inputs (not to be the same and not to leave empty field) and I am not sure how to do that.
For server side validations:
In app/models/game.rb you need to have the following validations,
validates :player_1, :player_2, presence: true
validate :player_names_uniq
def player_names_uniq
errors.add(:base, "Player names can't be equal") if (self.player_1 == self.player_2)
end
For client side validations:
I'm not aware of how to perform the equality validation at client side.
<h1>New Game</h1>
<%= form_for #game do |f| %>
<p>
<%= f.label :player_1 %><br/>
<%= f.text_field :player_1 required: true %>
</p>
<p>
<%= f.label :player_2 %><br/>
<%= f.text_field :player_2 required: true %>
</p>
<p>
<%= f.submit "Create Game" %>
</p>
<% end %>

Basic Ruby On Rails Linking Help

So I am beginning to work with Rails and I get some of the concepts but am stuck on an important one.
Let's say I have customers which has many jobs and jobs which belongs to customers.
How would I go about creating a new job for a customer?
I can create a link that goes to customers/1/jobs/new and I can grab the customer ID but how do I tell it that I am creating a job for customer 1?
I know this is the most basic of things but I just need a push in the right direction.
This is my form so far:
How do I get :customer_id to populate with the customer_id param?
<h1>New job</h1>
<% form_for(#job) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :customer_id %><br />
<%= f.text_field :customer_id %>
</p>
<p>
<%= f.label :manufacturer %><br />
<%= f.text_field :manufacturer %>
</p>
<p>
<%= f.label :serial_number %><br />
<%= f.text_field :serial_number %>
</p>
<p>
<%= f.label :problem %><br />
<%= f.text_area :problem %>
</p>
<p>
<%= f.label :notes %><br />
<%= f.text_area :notes %>
</p>
<p>
<%= f.label :status %><br />
<%= f.text_field :status %>
</p>
<p>
<%= f.label :tech_id %><br />
<%= f.text_field :tech_id %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', jobs_path %>
Just use form_for([#customer, #job]), this should generate the correct URLs (/customers/:customer_id/jobs etc).
You can then fetch params[:customer_id] in your JobsController.create method.
Here's a long, maintained, descriptive list of good ruby programming tutorials. Good luck.

In Rails, how do I generate form labels without symbols that still create correct "for" attributes?

In Rails, how do I generate form labels without symbols that still create correct "for" attributes?
If I take this form:
<% form_for(#thing) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
And alter it to improve the clarity of what's expected in the field:
<% form_for(#thing) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label "What would you like to call your thing?" %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
The for attribute on the label tag would read "thing_What would you like to call your thing?", which obviously destroys its relationship with the intended partner field.
So how do I alter the label text while preserving that relationship?
<%= f.label :name, "What would you like to call your thing?" %>
See label’s documentation (and on APIdock).

Resources