Change checkbox to change in db in rails? - ruby-on-rails

I have a table "User" with "admin" is "true" or "false". I show it with checkbox_feild. How can i change db if i clicked in checkbox? Thanks all.
<% #users.each do |user| %>
<%= form_for user, do |f| %>
<div class="field">
<%= f.check_box :admin%>
</div>
<% end %>
<% end %>

One way is to write a jquery script that submits the form when checkbox is clicked
<% #users.each do |user| %>
<%= form_for user,:html => {class: 'form'} do |f| %>
<div class="field">
<%= f.check_box :admin%>
</div>
<% end %>
<% end %>
Jquery script would be:
$('.form input[type=checkbox]').change(function(){
$(".form").submit();
});

Related

Check the URL path using ruby on rails

In my project, the gems that I've used are: rails admin, cancancan and devise.When I do the bundle install, lots of views will be added. I added "can :crud, [xxxx, xxxxx]" to have a CRUD in my ability model.
Question: How do I check the current path in my view? because I want to disable some field. Example: I am in the new path method and have 3 fields for that(then I clicked the submit/create, now it will the added data). I clicked the edit button,then there's one field will be disabled.
Note: The both form of new path and edit path is in the same file(activities_types/_form.html.erb).
View
_Form.html.erb
<% if current_page?(new_activity_type_path) %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<% else %>
<div class="field">
<%= f.label :"name" %>
<%= f.text_field :name, disabled: true %>
</div>
<% end %>
<div class="field">
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
if you want to diffenetiate between edit and new actions in view then
<% if params[:action] == "new" %>
...
<% elsif params[:action] == "edit" %>
...
<% end %>
OR
<div class="field">
<%= f.label :name %>
<% if params[:action] == "new" %>
<%= f.text_field :name %>
<% else %>
<%= f.text_field :name, disabled: true %>
<% end %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
I would set up an instance variable in your new and edit methods in your controller, following that up by checking the value in your form.
example:
def new
#whereAmI = "new"
end
def edit
#whereAmI = "edit"
end
In the view:
<% if #whereAmI == "edit" %>
<%= f.text_field :name, disabled: true %>
<% elsif #whereAmI == "new" %>
<%= f.text_field :name %>
<% end %>
You can check the action like this:
if params[:action] == "new"
# doing something
elsif params[:action] == "edit"
# doing something
end

One Model without nested Form Rails

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

rails submit_tags to different actions

<%= simple_form_for#equipment, :url => equipments_path, :method => :post do |f| %>
....
<% if #equipment.id.present? %>
<div class="actions">
//TODO submit_tag to action Update
</div>
<% else %>
<div class="actions">
<%= submit_tag "Adicionar Equipamento" %>
</div>
<% end %>
<% end %>
In this example I have two buttons,if the object exists I have the first button and when not exists I have the second button. The second button send a request to controller Equipments#Create. How can I send a request to Equipments#Update in first button ?
<%= simple_form_for #equipment do |f| %>
<div class="actions">
<%= submit_tag(#equipment.persisted? ? "Create Equipment" : "Update Equipment") %>
</div>
<% end %>
This is the short way to do it.
Usually you would use I18n to translate these labels.
(see: I18n for model-specific Rails submit button)

Showing check box values in edit page

I'm using the following collection_select in the new and edit pages to let users select contacts and associate them to a group:
<%= f.label :contacts %>
<div><span class="ul">
<% current_user.contacts.all.each do |contact| %>
<%= check_box_tag "contacts[]", contact.id %>
<%= f.label contact.name %>
<% end %></div>
</span>
I would like to show the already selected contacts checked in the edit page of the group. Is there another parameter that I can use along with the check_box_tag?
You can just pass in a true/false after the value
<%= f.label :contacts %>
<div>
<span class="ul">
<% current_user.contacts.all.each do |contact| %>
<% checked_logic = some logic for true/false %>
<%= check_box_tag "contacts[]", contact.id, checked_logic %>
<%= f.label contact.name %>
<% end %>
</span>
</div>
<% end %>

How to save uncheked check boxes

how i get to save unchecked chekboxes on rails?
I researched some links, but i as unable to find a working solution for me.
i got:
<% #book.each do |book| %>
<div>
<%= check_box_tag "orb[book_ids][]", book.id, #orb.books.include?(book) %>
<%= book.nome %>
</div>
<% end %>
when i uncheck all checkboxes, it didn't save :P
I tried to use a hidden field, but it gave me the error "there no book with id=0"
Add this:
<%= hidden_field_tag 'orb[book_ids][]', '' %>
You form should looks like:
<% #book.each do |book| %>
<div>
<%= check_box_tag "orb[book_ids][]", book.id, #orb.books.include?(book) %>
<%= book.nome %>
</div>
<% end %>
<%= hidden_field_tag 'orb[book_ids][]', '' %>

Resources