Basically I'm trying to list all events from my database on a website using this code:
<table class="table">
<thead>
<tr>
<th>Event</th>
<th>Join Event?</th>
</tr>
</thead>
<tbody>
<% #events.each do |e| %>
<p>
<tr>
<td>
<div>
<%= e.name %>
</div>
</td>
<td>
<div class="btn-group" data-toggle="buttons-radio">
<button class="btn" class-toggle="btn-success"><i class="icon-ok"></i></button>
<button class="btn btn-danger active" class-toggle="btn-danger"><i class="icon-remove"></i></button>
</div>
</td>
</tr>
</tbody>
<% end %>
</table>
You can easily join an event by clicking on a radio button. Now my problem is how can I save this form in Rails? In this example I have joined(=clicked) Event 2. What is the best way to save this form back into the database. Ideally I have a "Update" or "Save" button at the end of the website, which does that for me.
But when I add a simple submit button like this to the my form
...same code as above
</td>
</tr>
</tbody>
<%= f.submit 'Save form' %>
<% end %>
</table>
then my submit button will be triggered everytime I click on one of my radio buttons. Does anyone know a fix to this problem?
well, it is fairly simple to do a event[] radio button that when submitted will submit an array of the selected options.
The button tags would look like this, adjusting for your classes etc.
<%= radio_button_tag event[], e.id %>
then the controller would look like
def create
...
user.events = params[:event] unless params[:event].empty?
user.save
...
end
This ofcoarse assumes that you have a column in your user model to store events they are going to. A more flexible model would be to have user :has_many events :through => user_events. The same controller logic would apply, except it would look like
def create
...
if params[:event]
params[:event].each do |e|
user.user_event.new(:event_id => e)
end
end
....
end
Resources:
Form tag helpers: here
Using :has_many :through associations : here (section 2.4)
Related
In my html I'm displaying some data from two tables. I provided an edit button for each row of the table. When I click on it, it has to check if the name is existing in table1 or table2 and take all the details of that particular name.
HTML:
<div class="col-md-12">
<h5 style="text-align: center;">List of SNMP OIDs</h5>
<table id="myPersonTable" class="table table-striped" >
<thead>
<tr>
<th>Person Name</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody id="table_body">
<% #all_persons.each do |person|%>
<tr>
<td>
<%= person['name'] %>
</td>
<td>
<%= link_to '<button type="button" class="btn btn-info">Edit</button>'.html_safe, edit_oid_path(person['id'])%>
</td>
<td>
<%= form_tag(contoller: "configuration", action: "delete_person") do%>
<%= hidden_field_tag(:person_id, person['id'])%>
<%=submit_tag "Delete", class: "btn btn-danger", data: {confirm: "Are you sure?"}%>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
Rails Controller:
def edit
person_f = Person.find_by(name: params[:name])
person_s= HardPerson.find_by(name: params[:name])
if person_f.present?
#person = Oid.find(params[:id])
elsif person_s.present?
#oid = HardPerson.find(params[:id])
end
end
Here is a problem: I click on edit button for a person name from person2
table having id=1. This id exists in person1 and person2 tables both. Instead of taking details from person2 it is checking that id in person1 table and it is getting values for id=1 person details from person1 table
Here in controller params[:name] is getting null value. Help me to get params[:name] in my rails controller
To get params[:name] in your controller use the :param option in your routes to override the default resource identifier :id
resources :people, param: :name
This will allow you to use:
Person.find_by(name: params[:name])
Keep in mind that you might need to to override ActiveRecord::Base#to_param of a related model to construct a URL.
See 4.10 Overriding Named Route Parameters for more information.
It's a bit hard to understand what is your issue, but I'll try. Am I right that you want to see value from <%= person['name'] %> as a params[:name] inside edit action? Just pass it as additional argument to path helper:
<%= link_to '<button type="button" class="btn btn-info">Edit</button>'.html_safe,
edit_oid_path(person['id'], name: person['name'])%>
I am relatively new to programming and Rails, and I have been playing around with an idea, without much success, and did not really find any answers on here, railscasts or forums, so I am turning to the community ! So here is the problem:
For the purpose of the question, I have 2 models: Supplier and Supplier Quote:
class Supplier < ActiveRecord::Base
has_many :supplier_quotes
class SupplierQuote < ActiveRecord::Base
belongs_to :supplier
When I create a new Supplier Quote, there is a dropdown from which the user would select the Supplier linked to that Quote, with:
<%= f.select(:supplier_id, #supplier_select.map {|supplier| [supplier.name, supplier.id]}, include_blank: true) %>
If the supplier doesn't exist yet, instead of going to another page to create the record and come back, I wanted to have a modal in which the form for a new Supplier would come. So I have this link next to my dropdown:
<%= link_to "Add Supplier", '#supplierModal', class: 'small-link', "data-toggle" => "modal" %>
And its modal:
<div class="modal fade" id="supplierModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add Supplier</h4>
</div>
<div class="modal-body">
<%= form_for(#supplier, url: {controller: 'suppliers', action: 'remote_create'} ) do |f| %>
<h3 class="edit-title">Contact details</h3>
<table class="pretty editlist">
<tr>
<th><%= f.label(:name) %></th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th><%= f.label(:no_street, "Address") %></th>
<td><%= f.text_field(:no_street) %></td>
</tr>
<tr>
<th><%= f.label(:city) %></th>
<td><%= f.text_field(:city) %></td>
</tr>
<tr>
<th><%= f.label(:country) %></th>
<td><%= f.text_field(:country) %></td>
</tr>
</table>
<h3 class="edit-title">Banking details</h3>
<table class="pretty editlist">
<tr>
<th><%= f.label(:bank, "Bank") %></th>
<td><%= f.text_field(:bank) %></td>
</tr>
<tr>
<th><%= f.label(:IBAN, "IBAN") %></th>
<td><%= f.text_field(:IBAN) %></td>
</tr>
<tr>
<th><%= f.label(:BIC, "SWIFT/BIC") %></th>
<td><%= f.text_field(:BIC) %></td>
</tr>
</table>
<div class="form-button">
<%= submit_tag("Save") %>
</div>
<% end %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
For now I created an action, 'remote_create' so as to not mix it up with the create action that is used in the classical way.
def remote_create
#supplier = Supplier.new(supplier_params)
if #supplier.save
redirect_to controller: 'supplier_quotes', action: 'new'
end
end
Finally within my 'new' action in supplier_quotes_controller I have the following line that allows the Supplier creation form to be generated in the modal on the Supplier Quotes 'new' view:
#supplier = Supplier.new
This doesn't work obviously, but the bigger problem is that when I submit the form in my modal, not only is the Supplier record not created, but a Supplier Quotes record is submitted and created with blank fields. So not the expected behaviour at all.
Does anyone know how I would tackle this problem? Is it a problem with the form_for, or the actions?..
Secondary question: after the creation of the form in the modal, I am using a redirect_to to the page I was already on (supplier_quotes, 'new') to refresh it, as I want the new record to show up in the dropdown. If there is a way to dynamically update the dropdown then I would just need to make the modal go away without refreshing any page. Is that possible?
Hope I was clear enough! Thanks for the help!!
Best.
A.M.
Question 1:
My general strategy with things like this: make sure all the plumbing is connected, then do fancy stuff. So until the create is working, ditch the modal and just worry about creating a supplier from a supplier quote view. Then, get fancy with the modal.
With the form_for action, just use the 'classical' create route, and don't worry about defining a new one. Reference the supplier scaffolding that rails generated for you within the suppliers views (probably in the new.html.erb file and the _form.html.erb partial). You can even render the partial that rails generates from the suppliers views folder instead of writing out the form again.
Question 2:
Then when part one is creating appropriately (but still redirecting not how you want), you can take a look at remote forms, which submit in the background. The resource will be created, and you can define which pieces of the page (your dropdown menu) you want to update after a successful create action.
Read up on remote forms here: http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#form-for
I am having check box for selecting students in a particular class.But the unselected student id's are not passing via check box.Please help me. And also I have included coding below.
In View
<%= label_tag :students %>
<button type="button" id="check_all">
Check / Uncheck All
</button>
<table id = "mark">
<tr>
<td>
<table >
<thead>
<tr>
<td><strong>Students List</strong></td>
</tr>
</thead>
<tbody>
<% #student.each do |i| %>
<tr>
<td><%= check_box_tag :student, i.id %><%= i.first_name.capitalize %></td>
</tr>
<% end %>
</tbody>
</table>
<table>
<tr>
<td>
</tr>
</td>
</table>
</td>
</tr>
</table>
Js file
$('#check_all').on("click", function(){ $('input[type="checkbox"]').click(); });
You can achieve that in two ways at least.
one) deducting the submitted students from the students collection ids:
unselected_ids = #student.collect(&:id) - params[:student].collect(&:to_i)
two) setting another hidden field from client site using javascript
You can use hidden input with the same name before checkbox and zero value.
<input type='hidden' value='0' name='student'>
<input type='checkbox' value='1' name='student'>
When form submitted and checkbox is checked previous 0 value overwrites by 1. If checkbox not checked then 0 value sends to the controller.
Bur its works only if you have one value, but you have a lot of ids. And you must use student[] name in checkboxes, if you use student name then only last checked value sends in controller by in params[:student]
In this case use can use this construction in your code:
<% #student.each do |s| %>
<tr><td>
<%= hidden_field_tag "student_all[]", s.id %>
<%= check_box_tag "student[]", s.id %><%= s.first_name.capitalize %>
</td></tr>
<% end %>
And in controller get unchecked students like:
student_unchecked = params[:student_all] - params[:student]
I have a table that displays a name, and two values corresponding to each name that I have calculated using a loop and a progress bar for each value.
The code looks something like this,
<table>
<thead>
<th>Name</th>
<th>Value1</th>
<th>Value2</th>
</thead>
<tbody>
<% i=0 %>
<% j=0 %>
<% #department.members.each do |members| %>
<td><td><% members.name %><td>
<% members.evals.each do |evals| %>
<% i+=value1 * something %>
<% j+=value2 * something %>
<% end %>
<tr>
<td>
<div class="progress"><div class="bar" style="width: <%= (i*10).round() %>%;"></div></div>
<%= i %>
</td>
<td>
<div class="progress"><div class="bar" style="width: <%= (j*10).round() %>%;"></div></div>
<%= j %>
</td>
<% end %>
</tbody>
</table>
I wish to allow the user to click the heads and sort this information dynamically without changing the page on the client side, as well as create a drop-down menu above the table that allows the user to do the same. "Sort by value2" and "Sort by value1".
I am using bootstrap, so I don't want to use DataTables. Even their bootstrap theme doesn't look like boostrap. I just want a simple sort while preserving my current styling.
Datatable (http://datatables.net/release-datatables/examples/basic_init/zero_config.html)
You would need to sort the table with JavaScript, take a look at sortable table with jQuery.
You can always edit the CSS file to match your layout or to match with bootstrap's layout.
I'm fairly new to Rails.
What is the best way to design/structure a multi-page form in Rails? I would like the form to create a new record in the database immediately when the first page of the form is submitted, and for each subsequent page of the form to update that record in the database.
The reason I want the record created immediately after the first page is submitted is so that there can be the notion of an unfinished record that the user comes back to later to finish.
Once created, I would like to allow the user to edit any part of the model by going directly to that section.
I know enough Rails where if you guide me with the best way to structure this conceptually, I should be able to figure out the code myself.
Thank you.
I have a multi-step signup process that works this way. I create the record the first time and then the other steps are edit/updates on that record. I use multiple controllers for this; it is a much cleaner approach than trying to cram all the logic into one controller action (although you could use multiple actions from the same controller and it would work just as well, but don't forget to create routes for your custom actions). This approach makes validation more difficult for the data added in steps after the first, but you can always add your own errors by calling errors.add on your model, essentially rolling your own validations. You can also write logic in your sessions controller to direct the user back to the same step in the multi-step form if they return later and have not completed it.
Ryan Bates explains this in one of his Railscasts => MultiSteps Forms
I've inherited a 'multi-page' form along those lines - but it was built for Rails 2.2 and I'm only just adapting the approach for Rails 3.
Essentially we used a tabbed layout with the entire form in one view - although this approach was heavily weighted towards one controller in Rails 2.2 I think it can be broken down better.
The layout meant that each section of the form could be accessed by the tabs - but each tabbed section also had a link_to action to the next section, at the bottom of that section (section A -> section B, for example) that saved the entire form each time you moved onto a new section - I've heavily edited the view just to give an idea but if it's a new form it will only reveal each section after the submit button for each section has been pressed.
<ul id="tabs">
<li>Section A</li>
<li>Section B</li>
<li>Section C</li>
<li>Section D</li>
<li>Section E</li>
<li>Section F</li>
<li>Section G</li>
<li>Section H</li>
<li>Section I</li>
<li>Section J</li>
</ul>
<%=hidden_field_tag 'active_fabtabulous_tab'%>
<% form_for(#detail) do |f| %>
<%= f.error_messages %>
<div class="panel" id="SectionA">
<b><u>Section A: Questionnaire Details</u></b>
<br></br>
<table>
<tr>
<td><div id="field_name">Questionnaire received on (dd/mm/yyyy):</div></td>
<td><%= date_select("questionnaire", :received_on, :order=>[:day,:month,:year],:use_month_numbers=>true,:start_year=>Date.today.year,:end_year=>2008,:include_blank => true) %></td>
</tr>
<tr>
<td><div id="field_name">Interviewer name:</div></td>
<td><%=text_field("questionnaire",:intervieweename)%></td>
</tr>
</table><!-- end questionnaire div -->
<%= f.submit "SectionB" , :class => "questButton" %>
</div>
<!--- Page 2 --->
<div class="panel" id="SectionB">
<b><u>Section B: Case Classification</u></b>
<br></br>
<% fields_for :patient, #patient do |p| %>
<table>
<tr>
<td class="sectionA_is_this_case"><div id="field_name">Epidemiology definition:</div></td>
<td><%= #patient.epidef %>
</td>
</tr>
</table>
<% end %>
<table>
<tr>
<% fields_for :patient, #patient do |p| %>
<td><div id="field_name">Asymptomatic:</div></td>
<td><% if #patient.asymptomatic %>Yes<% else %>No<% end %></td>
<% end %>
<tr>
<tr>
<td><div id="field_name">Investigation is:</div></td>
<td><%=select("detail", "invstatus", INVESTIGATION_IS)%></td>
</tr>
<tr>
<td><div id="field_name">Outbreak keyword or number:</div></td>
<td><%= f.text_field :outbreakid ,:cols => 40, :rows => 1 %></td>
</tr>
</table>
</div>
<%= f.submit "SectionC" , :class => "questButton" %>
</div>