How save multiple values from array in field? - ruby-on-rails

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.

Related

I want to make title required field while creating new row in ruby on rails

I want to make title field mandatory to save new row :-
<h1> Edit Post</h1>
<%= form_for #post do |f| %>
<p>
<br> <%= f.label :title, autofocus: true, placeholder: "title", :required => true %> <br/>
<br> <%= f.text_field :title %> <br/>
</p>
<p>
<%= f.label :body %><br/>
<%= f.text_area :body %> <br/>
</p>
<p>
<%= f.submit "enter" %>
</p>
<% end %>
It is saving anyway , kindly let me know where I am going wrong !
On the frontend view you should have:
<h1> Edit Post</h1>
<%= form_for #post do |f| %>
<p>
<br> <%= f.label :title%> <br/>
<br> <%= f.text_field :title, autofocus: true, placeholder: "title", required: true %> <br/>
</p>
<p>
<%= f.label :body %><br/>
<%= f.text_area :body %> <br/>
</p>
<p>
<%= f.submit "enter" %>
</p>
<% end %>
Add a validation on your model as well, as such:
#models/post.rb
class Post
validates :title, presence: true
end

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

Rails form_for in function of another value

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.

How to check attribute value of current object in edit form?

I have a nested model. How to check attribute value of object(hotel) of neted model in edit form? I can't figure out how to write if/else statement in _hotels_fields.html.erb
edit.html.erb
<% provide(:title, "Edit trip") %>
<h1>Edit trip</h1>
<%= form_for(#trip) do |f| %>
<%= render 'fields_edit', f: f %>
<%= f.submit "Save changes" %>
<% end %>
_fields_edit.html.erb
<p>
<%= f.label :image %>
<%= f.file_field :image %>
</p>
<p>
<%= f.label :content %>
<%= f.text_area :content %>
</p>
<p>Hotel</p>
<%= f.fields_for :hotels do |builder| %>
<%= render 'hotels_fields', f: builder %>
<% end %>
_hotels_fields.html.erb
<% if #trip.hotels.name == "hotel" %>
<p>Render any text</p>
<% end %>
<fieldset>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_field :description %>
</p>
</fieldset>
In the context of _hotels_fields.erb.html one specific hotels (why plural here if you have only one?) is represented by f.object. Therefore this should work:
<% if f.object.name == 'hotel' %>
...
<% else %>
...
<% end %>

Rails 3.2 nested_form gem: how to create link_to edit_model_path() in fields_for partial

I have a nested form, that uses the partial method for rendering the fields for the nested modal
<%= nested_form_for [:admin, #proforma] do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<%= f.fields_for :elements %>
<p><%= f.link_to_add "Add element to proforma", :elements %></p>
<p><%= f.submit %></p>
<% end %>
In my partial I want a link to take you to edit the nested model instance e.g.
<div class='element-children'>
<p>
<%= collection_select(:element, :component_id, Component.all, :id, :name) %>
<%= f.text_field :name %>
<%= f.text_field :detail %>
<%= link_to 'Edit', [:admin, edit_element_path(#element)] %> |
<%= f.link_to_remove "Destroy" %>
</p>
</div>
However #element is not available in the partial. How can I reference the correct element?
Edit...
Solution using comment from Bradley
<div class='element-children'>
<p>
<%= collection_select(:element, :component_id, Component.all, :id, :name) %>
<%= f.text_field :name %>
<%= f.text_field :detail %>
<%= link_to 'Edit', edit_admin_element_path(f.object.id) %> |
<%= f.link_to_remove "Destroy" %>
</p>
</div>

Resources