I am working on a new app like SNS. And I stack the case users update the group member with Cocoon. UserGroup model includes current user but current user should not be edited, so I don't want to display current user when the user edit the group member.
I want to skip the column includes current user.
apps/views/group/edit.html.erb
<div class="formItem">
<h3>Group</h3>
<%= f.fields_for :user_products do |m| %>
<div id="links">
<%= render 'maker', f: m %>
</div>
<% end %>
<%= link_to_add_association "Add member", f, :user_products, partial: 'maker' %>
</div>
apps/views/groups/_member.html.erb
<div class="nested-fields">
<div class="formField">
<%= f.collection_select :user_id, #cofounder, :id, :username, {}, class: "founderSelect" %>
<%= link_to_remove_association "Remove member", f %>
</div>
</div>
If I understand it correctly you want to remove current_user from the cofounder selection group right?
You could do it with #cofounder.where.not(id: current_user.id)
<%= f.fields_for :user_products, f.object.user_products.where.not(user_id: current_user) do |m| %>
<div id="links">
<%= render 'maker', f: m %>
</div>
<% end %>
I got it.
Related
My current task is to create simple Quiz web app. App will be structured this way:
- Admin can create, edit, delete and activate quizzes.
- Visitors can take a quiz and submit it. Then receiving quiz results etc.
So far I have created nested form (with Cocoon gem) where admin can create quiz's and add new questions and possible answers, but I have not a single idea how to make that nested form as submit-able quiz for visitors.
My code:
Form:
<%= form_for [:admin, #test] do |f| %>
<div class="field">
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %>
<br/>
<%= f.text_field :description %>
</div>
<h3>Questions</h3>
<div id="question">
<%= f.fields_for :questions do |question| %>
<%= render 'admin/question_fields', f: question %>
<% end %>
<div class="links">
<%= link_to_add_association 'add question', f, :questions %>
</div>
</div>
<%= f.submit %>
<% end %>
_question_fields partial:
<div class="nested-fields">
<div class="field">
<%= f.label :description %>
<br/>
<%= f.text_field :description %>
</div>
<%= link_to_remove_association "remove question", f %>
<h3>Answers</h3>
<div id="answers">
<%= f.fields_for :answers do |answer| %>
<%= render 'admin/answer_fields', f: answer %>
<% end %>
<div class="links">
<%= link_to_add_association 'add answer', f, :answers %>
</div>
</div>
</div>
Question: What is the best way to allow dynamic nested form to be used for taking tests by visitors and store submitted data in database?
Thanks in advance.!
I have only one user model, I want to give add more option on the form.
There are many example have nested form but in my case no need to nested because I have only one user model.
I want to save bulk of users using add more form
You can do something like:
<%= form_tag users_path do %>
<% #users.each do |user| %>
<%= fields_for 'users[]', user do |u| %>
<div class="field">
<%= u.label :name %><br>
<%= u.text_field :name %>
</div>
<% end %>
<% end %>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
def create
params[:users].each do |user|
# create
end
end
new to rails and could use some help figuring out how to allow users to update records in a list without having to leave the page.
Specifically, I have two forms on a page where users enter their children's info.
One form is for the user to add a NEW child's info to create a list of children below.
The list of children displays the user's previously entered children info.
However, within the child list I would like to allow users to both delete and edit an individual child's record.
My DELETE function is working fine, it's the UPDATE functionality I am having trouble with...
Here's the children#update controller:
def update
raise
#user = current_user
#child = Child.find(params[:id])
if #child.update_attributes(child_params)
flash[:notice] = "Child info was updated."
else
flash[:error] = "Sorry. Something went wrong, please try again."
end
respond_with(#child) do |f|
f.html { redirect_to new_child_path }
end
end
Here's the childlist form partial view:
<form role="form">
<% i = 1 %>
<% #user.children.each do |child| %>
<div class="col-md-12 form-align list-line">
<div class="col-md-10 form-align">
<%= label_tag child, "Child #{i}:" %>
<% i += 1 %>
</div>
</div>
<%= form_for(child, method: :put) do |f| %>
<div class="col-md-12 form-align">
<div class='col-md-4 form-align'>
<%= f.label :first_name, class: 'sr-only' %>
<%= f.text_field :first_name, value: child.first_name, class: 'form-control form-control-align' %>
</div>
<div class='col-md-4 form-align'>
<%= f.label :middle_name, class: "sr-only" %>
<%= f.text_field :middle_name, value: child.middle_name, class: 'form-control form-control-align' %>
</div>
<div class='col-md-4 form-align'>
<%= f.label :last_name, class: "sr-only" %>
<%= f.text_field :last_name, value: child.last_name, class: 'form-control form-control-align' %>
</div>
</div>
<div class="col-md-12 form-align">
<div class="col-md-4 form-group form-inline form-align">
<%= f.label :birth_date, "D.O.B." %>
<%= f.date_field :birth_date, value: child.birth_date, class: 'form-control' %>
</div>
<div class="col-md-4 form-group form-inline form-align">
<%= f.label :deceased, "Deceased?" %>
<%= f.select :deceased, value: child.deceased?, class: 'form-control form-control-align' %>
</div>
<%= f.submit "Update" %>
<%= link_to '<i class="glyphicon glyphicon-remove red"></i>'.html_safe, child, method: :delete %>
</div>
<% end %>
<% end %>
</form>
...and the child model: simply belongs_to :user / user model has_many :children
...and routes: resources :children
I think I need some options passed through my form_for, but unable to find what those need to be...
I would suggest using a gem like Best in Place to allow for in-place editing on certain fields in the list.
If you don't want in-place editing then utilize a modal view that contains the edit form.
I have a model with an array column (box_ids). In my view I would like to have a field for each of the values in the array and three extra empty fields to be able to add new values to the array. How to go about this? I have the following:
<%= f.fields_for "box_ids[]", #shop do |bid| %>
<div class="form-group">
<%= bid.label :box_id %> Box ID: <%= bid.text_field [WHAT HERE] %>
</div>
<% end %>
I don't know if this is the right approach but in any case I have no method to supply to text_field.
Any suggestions?
Edit:
This works:
<% #shop.box_ids.each do |bid| %>
<div class="form-group">
<%= label_tag :box_id %> Box ID: <%= text_field_tag "box_ids[]", bid %>
</div>
<% end %>
<% 3.times do %>
<div class="form-group">
<%= label_tag :box_id %> Box ID: <%= text_field_tag "box_ids[]" %>
</div>
<% end %>
But that requires special handling in the controller - I would like to avoid that if possible.
<%= f.fields_for "box_ids[]", #shop do |bid| %>
<div class="form-group">
<%= bid.label :box_id %> Box ID: <%= bid.text_field :box_ids %>
</div>
<% end %>
I am new to rails (and non ASP.NET web development). For creating a user signup page with a radio button, I'm doing the following. However the user.roles is not updated. What could be the problem?
(I edited the user model created by sorcery gem to add a new string field roles.)
view/users/new.html.erb
<%= form_for #user do |f| %>
..
<div class="field">
<%= radio_button_tag(:assistanttype, "TA") %>
<%= label_tag("TA") %>
<%= radio_button_tag(:assistanttype, "RA") %>
<%= label_tag("RA") %>
</div>
Try changing the code to:
<%= form_for #user do |f| %>
..
<div class="field">
<% %w{TA RA}.each do |value|-%>
<% f.radio_button :assistanttype, value %> <% value %><br />
<% end -%>
</div>
%w{} is used to put string inside and automatically change it to an array.
Example:
%w{A B C} = [A, B, C]