I am trying to make the following:
Any solution to update the database with the value and with the checkbox without using JavaScript with a click on or click off?
<%= form_for :act_as do |f| %>
<%= f.check_box(:suspend_flag, { checked: (ActAs.find_by(ModifiedID:#misc_data[:user]).SuspendFlag ? false :true), class: 'abcd' }, 0, 1) %>
<%= f.submit %>
<% end %>
Related
I have a model Project and a model ProjectLine. Project has nested attributes for ProjectLine.
On my projects show view I'm listing project info with list of tasks.
<% #project.project_lines.each do |project_line| %>
<%= project_line.user.name %>
<%= project_line.description %>
<%= number_to_currency project_line.amount, precision: 0, unit: "USD ", separator: ",", delimiter: "," %>
<%= project_line.task_status_name %>
<% end %>
Tasks are created on project form using cocoon.
In the view above, after status name, I would like to have a button for each project line that changes the status.
The solution I found so far is to create a form.
<%= semantic_form_for #project_line do |f| %>
<%= f.hidden_field :status, :value=>2 %>
<%= f.submit 'ACCEPT', class: 'btn btn-primary btn-md' %>
<% end %>
I tried to place it on a partial on projects and call it on the loop <% #project.project_lines.each do |project_line| %>but i can't get it work.
How can I get a button for each project line to change project line status?
If i understand your request, somthing along the lines of
view
<% #project.project_lines.each do |project_line| %>
<%= project_line.user.name %>
<%= project_line.description %>
<%= number_to_currency project_line.amount, precision: 0, unit: "USD ", separator: ",", delimiter: "," %>
<%= project_line.task_status_name %>
<button class="accept_to_server">Accept</button>
<% end %>
javascript(jquery)
$.('.accept_to_server').on('click', function(){
$.ajax({
url: 'path/to/accept/route'
method: 'PUT' // or what method your route uses
data: {data:you, need:here}
}).done(function(data){
// update your view
}).fail(function(){
// something went wrong
});
};
Hope this helps out.
I need a way for my form to not be sent if the user didn't bother to select any radio buttons.
I'd like to to that within the view and the controller, not in the model (the data shouldn't even be sent)
<%= form_tag("/bookings/new", method: "get") do %>
<% #flights.each do |flight| %>
<%= radio_button_tag :flight_id, flight.id %>
<% end %>
<%= submit_tag "book now" %>
<% end %>
edit, to clarify
normally I'd do
<%= f.text_field :name, required: true %>
but, as I have many radio buttons and I only need one for the form to work, I don't know how to implement it
You can set validation in the model to see the presence of checkbox if javascript is disabled. This is a more robust method.
validates :flight_id, :acceptance => true
Docs here - http://guides.rubyonrails.org/active_record_validations.html#acceptance
Edit
function validateCheckBox() {
var x = document.getElementById("flight_id").checked;
if(!x) {alert("Not checked")}
}
<%= submit_tag "book now" , :onclick => "validateCheckBox();" %>
<%= f.text_field :name, required: true %>
This still works perfectly for radio buttons, and it's okay if it ends up on all radio items. The form will still only require one input.
I just tested it on my Rails 6 app.
I would like to rewrite a form which is used to update a record on a database.
I want to update the form so that the form input does not show the record, as
the record is outputted by the line
<%= q.object.content %>.
I want the
form input not to display the record, and I want that the record is updated
when the input field is edited, and is not edited when it is left blank.
I am new at working with forms and don't know the best way to achieve this.
Can anyone provide any help on achieving this ? Below is the current form. Any help would be appreciated.
<%= semantic_form_for #bunchOfThings do |f| %>
<%= f.inputs do %>
<%= f.semantic_fields_for :aThing, #aThing do |q| %>
<%= q.object.content %>
<%= q.input :content, label: "A Thing: #{q.object.content}" %>
<% end %>
<% end %>
<%= f.action :submit , label: t('Some Text'), button_html: { class: 'btn btn-primary' } %>
<% end %>
You can manually set the default value of a field to an empty string by changing this line:
<%= q.input :content, label: "A Thing: #{q.object.content}" %>
To this:
<%= q.input :content, label: "A Thing: #{q.object.content}", input_html: {value:''} %>
You would also need to filter out blank fields on the backend within the update controller method. Something like this:
def update
filtered_params = permitted_record_params
filtered_params.keep_if{|k,v| !v.blank? }
record.update(filtered_params)
...
end
Where of course the permitted_record_params method returns your permitted params hash.
I have a form for which I am using checkboxes (Not using radio buttons for my purpose). The problem I run into is when I submit a form, I get an error saying params is missing or value is empty:checkup. I am trying to use hidden filed but get the same error. HOw to have an option of sending only one if selected?
def checkup_params
params.require(:checkup).permit(:eye, :heart)
end
my form:
<%= form_for(#checkup) do |f| %>
<%= hidden_field_tag "checkup[eye]", nil %>
<%= check_box_tag :eye, "eye" %>
<%= hidden_field_tag "checkup[heart]", nil %>
<%= check_box_tag :heart, "heart" %>
<%= f.submit %>
<% end %>
I suggest reading this guide http://guides.rubyonrails.org/form_helpers.html#dealing-with-model-objects
Most specifically section 2.2. You really shouldn't be using tag helpers here.
I am trying to implement a dropdown menu to display all the names I have in my database. I tried unsuccessfully the following code:
<%= form_for #airline, remote: true do |f| %>
<%= f.select :name, [#airlines.names] %>
<%= f.submit %>
<% end %>
Controller:
def index
#airlines = Airline.all
#airline = Airline.new
end
I assume the solution is quite straight forward but I couldn't find it.
This should do
<%= f.select(:name, #airlines.collect { |airline| [airline.name,airline.id] }, {:include_blank => 'Choose 1'},:class=>"class_name") %>