How to pass an array of string through the rails' form - ruby-on-rails

I have a trouble with the rails' form, I need to pass to the form of Event an array of emails, I have this attribute members that I want to have like array. I try in this way but it doesn't work, can you help me?
<div class="field">
<%= form.fields_for :members do |p| %>
<% #group.list_accepted.each do |part| %>
<%= p.label :email , User.find(part.user_id).username %>
<%= p.check_box :email, {} ,User.find(part.user_id).email, nil %>
<br>
<%end%>
<% end %>
</div>
params.require(:event).permit(:title, :description,:user_id, :group_id, :start_date, :end_date, members: [:email])
the result of this is a set with just the first email {email=> "email#.com"}

Related

My nested attributes are not passing through the strong Params

I have a form for a delivery order that contains a form for a meal inside of it. A meal is made up of items, which are also objects. The form for a delivery looks as so...
<%= form_for #delivery do | f | %>
<%= f.label :address %>
<f.text_field :address $>
<% if #meal != nil %>
<% meal = #meal %>
<% else %>
<% meal = Meal.new %>
<% end %>
<%= f.fields_for meal %>
<%= render partial: "meals/form", locals: {selected_meal: meal} %>
<% end %>
<br>
<%= f.submit "Continue" %>
<% end %>
and the form for the meal looks as so
<label>Meal Name: (Optional) </label>
<%= f.text_field :name %>
<br>
<h4>-----------Pizzas------------</h4>
<%= f.collection_check_boxes :meal_item, Item.pizza_items, :id, :show %>
<br>
<h4>-------Cold Sandwiches-------</h4>
<%= f.collection_check_boxes :meal_item, Item.cold_items, :id, :show %>
<br>
<h4>-------Hot Sandwiches-------</h4>
<%= f.collection_check_boxes :meal_item, Item.hot_items, :id, :show %>
<br>
<h4>-----------Salads-----------</h4>
<%= f.collection_check_boxes :meal_item, Item.salad_items, :id, :show %>
<br>
<h4>------------Pastas------------</h4>
<%= f.collection_check_boxes :meal_item, Item.hot_items, :id, :show %>
<br>
<h4>-----------Chicken-----------</h4>
<%= f.collection_check_boxes :meal_item, Item.hot_items, :id, :show %>
<br>
<%= f.submit "Continue" %>
<% end %>
When the delivery form with the nested meal form is passed, it goes to the delivery # confirm action, and with the strong param, it looks like this...
def confirm
binding.pry
#delivery = Delivery.new()
if (SessionHelpers.is_logged_in?(session))
#credit = SessionHelpers.current_user(session).credit
end
end
private
def delivery_params
params.require(:delivery).permit(:address, :order_user_id, :total_price, :delivered, meal_attributes: [:name, items:[]])
end
Whenever the form is passed, the delivery_params only has the address passed, none of the meal attributes go through, yet they exist in the regular params. How can I fix this?
Probably coming from how you call your nested form.
Maybe try smething like :
<%= f.fields_for :meal do |meal_f| %>
And in your partial :
<%= meal_f.text_field :name %>

no implicit conversion of Symbol into Integer in view forms

I am working on configuring the apartment gem for my rails app to give users the functionality to create subdomains. I have a nested form and when attempting to access "accounts/new" I am getting the following error:
no implicit conversion of Symbol into Integer in view forms
accounts/new.html.erb
<div>
</div>
<h2>Create an Account</h2>
<%= form_for #account do |f| %>
<%= f.fields_for :owner do |o| %>
<%= form_for o, :email do %>
<%= o.text_field :email, class: 'form-control' %>
<% end %>
<%= form_for o, :password do %>
<%= o.password_field :password, class: 'form-control' %>
<% end %>
<%= form_for o, :password_confirmation do %>
<%= o.password_field :password_confirmation, class: 'form-control' %>
<% end %>
<% end %>
<%= form_for f, :subdomain do %>
<div class="input-group">
<%= f.text_field :subdomain, class: 'form-control' %>
<span class="input-group-addon">.scrumteam.com</span>
</div>
<% end %>
<%= f.submit class: 'btn btn-primary' %>
<% end %>
</div>
</div>
accounts_controller.rb
private
def account_params
params.require(:account).permit(:subdomain, :owner_attributes => [:email, :password, :password_confirmation])
end
You are nesting multiple forms into each other which is not supported in HTML:
See this question for more details: Can you nest html forms?
These form_for lines look invalid in particular:
<%= form_for o, :field_name do %>
Here, o is a special FormBuilder object which should not be fed to form_for. Try this instead:
<%= f.fields_for :owner do |o| %>
<%= o.fields_for :email do %>
Unlike form_for, it is possible to nest fields_for blocks.
Not sure if this is your route problem, but form_for creates an actual form tag in html. You only need (and should have) 1, the fields_for allows you to switch the form helpers to a different object, but you don't need to call form_for again within it. If you're just trying to group your form fields, you can just add some divs and/or labels.

How to use integer radio buttons

What is the simplest way to represent an integer attribute with a limited number of valid values as radio buttons?
How can I use the formhelper to achieve this? I only see examples that use string values.
You can try
<%= form_for :model do |f| %>
<% 1.upto(10) do |i| %>
<%= f.radio_button :integer, i %> #integer is the model field
<% end %>
<% end %>
or
<%= form_tag do %>
<% 1.upto(10) do |i| %>
<%= radio_button_tag :name, i %>
<% end %>
<% end %>
Something like:
<% (1..10).each do |value| %>
<%= f.radio_button_tag(:method_name, value) %>
<% end %>
Where (1..10) might belong into the model as a constants. What also makes validations easier:
# in the model
FOOS = (1..10)
validates :foo, inclusion: { in: FOOS }
# in the view
<% Model::FOOS.each do |values| %>
<%= f.radio_button_tag(:foo, value) %>
<% end %>
My way is to use an enum in your model. E.g the role field will be saved as number 0 if the role is investor, number 1 if the role is trader in my example.
You need to declare your enum in your model .rb file like this:
enum role: [:investor,:trader]
Then in the view file:
<%= f.radio_button :role, :investor , class: 'form-control underlined' %>
<%= label :role_investor, 'I wanna be an investor' %>
<%= f.radio_button :role, :trader , class: 'form-control underlined' %>
<%= label :role_trader, 'I wanna be a trader' %>

Rails way to edit multiple attributes on a model from a view

I have a model Person with the following attributes:
:name, :state, :age, :town
Let's say I want to be able to edit all of the attributes except for :name from that Person's edit view. Is there a "rails" way to do this, and if so what would I write without looping through each attribute and creating a form?
Right now, I've got something like this:
<%= form_for #person do |person_form| %>
<%= person_form.fields_for :age do |age_form| %>
<%= age_form.text_field :age %>
<% end %>
<% end %>
And I would do that for each attribute.
It would be just a standard form since the object you're wrapping the form around has all of the attributes.
<%= form_for #person do |f| %>
<%= f.text_field :state %>
<%= f.text_field :age %>
<%= f.text_field :town %>
<%= f.submit %>
<% end %>
Of course, you can add labels and whatever else you need to the form.

How create new instance of a model in relation many to many?

I try to create a furniture object, which is in relation by a has_many_and_belongs_to with stores, this is my model:
class Furniture < ActiveRecord::Base
attr_accessible :area, :description, :name, :size
has_and_belongs_to_many :stores
end
My problem is that I don't know how create a new furniture, because i try to associate furniture with one or more store with check box, but I obtain this error: undefined method merge for #<Store:0x007ff16ae27e40>.
These are my view with form and my controller with new and create action:
View:
<%= form_for #furniture do |f| %>
<%= f.label :name %>
<%= f.text_field :name %> <br><br>
<%= f.label :description %>
<%= f.text_field :description %> <br><br>
<%= f.label :size %>
<%= f.text_field :size %> <br><br>
<% #store.each do |store| %>
<div>
<%= f.check_box :stores, store %>
<%= store.name %>
</div>
<% end %>
<%= f.submit %>
<% end %>
Controller:
def new
#furniture = Furniture.new
#store = Store.order('name ASC')
end
def create
#furniture = Furniture.create(params[:furniture])
redirect_to admins_path
end
How can I solve it?? Have you some suggestion to create a new object with this relation ship??
Thank you very much
EDIT:
I have a join table between furniture and store
The has_and_belongs_to association adds a method collection_singular_ids= that for the current case will be #furniture.store_ids=. According to the docs
The collection_singular_ids= method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.
So, you can use this idea to add the stores to your furniture. Replace
<% #store.each do |store| %>
<div>
<%= f.check_box :stores, store %>
<%= store.name %>
</div>
<% end %>
with
<% #store.each do |store| %>
<div>
<%= f.check_box :store_ids, {:multiple => true}, store.id, nil %>
<%= store.name %>
</div>
<% end %>

Resources