How to create new nested object with checkboxes - ruby-on-rails

I want to create a nested object of comments in page form
This returns all comments as checkboxes
<% for comment in Comment.find(:all)> %>
<%= check_box_tag "page[comment_ids][]", comment.id,
#page.comments.include?(comment) %>
<%= comment.navn %>
<br />
<% end %>
When I submit the page form. I want it to take those checked checkboxes and create new comments precise as those there is checked and also a association to page.
**Example:
I get a checkbox list of comments in the page form
Comment 1
Comment 2
I check checkbox 2
When i submit the page form
There is created a new Comment precise as comment2(not the ID), with HABTM association to the page.**
Can someone please tell me how i should create this form?
PS: I am a rails beginner

Related

colllection check boxes - how to change user attributes

I have users that can choose which 'areas' they cover.
The relationship between 'users' and 'areas' is a has many and belongs to.
I can currently get collection_check_boxes to list the 'area' options for the user but I'm stuck on getting the areas to be updated once the user chooses and submits the area options.
Here's my form:
<%= form_for #user do |f| %>
<%= collection_check_boxes(:user, :area_ilondon_ids, AreaIlondon.all, :id, :postcode) %>
<%= f.submit %>
<%end%>
The form above renders a list of checkboxes showing the users current 'area' selection but when I submit the form the areas do not get updated.
What do your user_params look like? Do you have something like this :area_ilondon_ids[]? You need to let in the array of ids.
As #Stoob pointed out the following was needed:
:area_ilondon_ids=>[]

Passing text_field input value to a certain controller

I am very new to Ruby and Rails, and I have come across a problem I can't seem to be able to solve:
In my rails app i have users which on the index site, called home, are displayed a couple of events.
If the user wants to "bookmark" these events he can click on a button next to the event an add it to his event_items list, which is just an "association"-controller I implented for the association between users and events (e.g. users has_many events and vice versa)
This all works fine, and this is how I have implemented the add functionality:
index.html.erb extract for home:
<h2>Events</h2>
<% #events.each do |event| %>​
​ <div>​<%= event.eventtitle %></div>
<%= button_to 'Add Event', event_items_path(event_id: event, user_id: session[:user_id]) %>
<% end %>
The button_to method works fine to add an event to the event_item list which just records the user_id and the event_id for an association in an even_items db-table.
Now I would like to allow the user to add a comment along to the event to the event_items db-table.
I can't seem to find a way of passing a text_field value to my event_items_path to add the comment to the event_items table in the db.
I guess I can't use the "button_to" method anymore because it is resolved in it's own form and thus I can't transmit any text_field information. So I created a new form... I can't get this form to pass the text_field information as parameters though:
new index.html.erb extract for home:
<h2>Events</h2>
<% #events.each do |event| %>​
<%= form_for :event_items, url: event_items_path(event_id: event, user_id: session[:user_id]) do |f| %>
<div>​<%= event.eventtitle %></div>
<%= f.label :event_comment %><br />
<%= f.text_field :event_comment %>
<%= f.submit %>
<% end %>
<% end %>
When I press the submit button, it still creates the event_item in my db-table, but withouth the event_comment. Checking the post transmission in my browser it doesn't seem to transmit the :event_comment.
It can only access the :event_id and :user_id in the parameters.
I would appreciate any help!
Update
I have just now realised, that when pressing the submit button from my new form above the :event_comment does get transmitted with the form.
Checking in Chromes DevTools, in Network>>Headers there is a post request which has event_id and user_id listed under Query String Parameters and the event_comment listed under Form Data as event_items[homeprediction]
Does anyone know how I can access the Form Data? Using params[:homepredictions] doesn't work in my event_items controller.
Found a solution now:
The :event_comment was being transmitted on submit. It is stored in Form Data as event_items[homeprediction].
This is what I did in the event_items controller to access the data:
def create
#user = User.find(params[:user_id])
event = Event.find(params[:match_id])
comment = params[:event_items]
#event_item = #user.event_items.build(event: event, event_comment: comment[:event_comment])
...
end
I saved the params[:event_items] hash to a new comment[] hash. And in the new comment[] hash I was able to access the :event_comment passed from the form field_tag.
Don't know if this is the most elegant/right way of doing it. But it works for now :-)

insert multiple records to a single table from a single form

Generally if we can create only 1 record, say 1 user at a time from the form. But what if I want to create multiple users from a single form? There are no associations with other models. How can i do that?
You have to make a form with an array of users params .e.g
<%= from_tag '/users/create_multiple' do %>
<%= text_field_tag "users[][name]" %>
<%= text_field_tag "users[][name]" %>
<% end %>
In UsersController:
def create_multiple
params[:users].each do |user|
user = User.create(user)
end
end
You can add validation code as per your wishes,
visit here how to pass form params for multiple records http://guides.rubyonrails.org/form_helpers.html#basic-structures
One option is, write as many forms that you want for each model and submit all the forms once with Jquery.
This link will help you with submitting multiple forms
Multiple submit buttons/forms in Rails

How to create nested froms to create values in two different table at once in rails 3

I was stuck with a different scenario like say for example i have model movies and the other is releases. I want to create a new form in the apps/views/movies/new.html.erb. In the new i wanna have the fields for movies table eventually ill be having a form for it and at the same time i want to add the movie release information to the other table releases.Like it will be nested form as follows
<%= form_for(#movies)......%>
<% files for movies table %>
2nd form <%=form_for :releases %>
<% fields for releases table%>
so form one carries the data to movies table when user clicks on submit button. But i want to pass the the values to release table also with this click using a nested form. Is it possible to have nested forms. If so i wanted some help on how it can be achieved exactly. Help me out please.
<%= form_for(#movies) do |f| %>
# fields for movie
<%= f.fields_for :releases do |nested_f| %>
#fields for your nested form releases
<% end %>
<% f.submit %>
<% end %>
Also in Movie.rb model add this line. accepts_nested_attributes_for :releases
More information there
I don’t think HTML allows nested forms, see for example this question.

rails - form to disply non-input type fields in nested form

I guess this is a newbie question, but what is the syntax in a form to show contents of fields not as a text box/area, but rather like label would appear.
<% form_for #user do |f| %>
<% f.fields_for :user_ingreds do |builder| %>
<p>
<%= builder.??? %>
</p>
<% end %>
<% end%>
user has_many :user_ingreds and accepts_nested_attributes_for :user_ingreds.
Basically I want to make a list of user_ingreds where the user can't edit the data but can remove the record from the list via a button.
However the fields_for builder doesn't recognize a direct call to to the fields in UserIngred model (ie, builder.user_id throws and error.
If you want to make a non-editable list of the ingreds with a button to remove them, then don't make a form.
Instead, display your data and a button beside them created with link_to and pointing to your delete function.
If ever you want to really display the content of your form variable, you can access it this way:
builder.object.user_i

Resources