Stop array of strings from saving with dashes - ruby-on-rails

I'm listing the models in my app so an admin can create custom roles:
<% ActiveRecord::Base.send(:subclasses).each do |model| %>
<tr>
<td width=10>
<label><%= check_box_tag "role[read_models][]", model.name, #role.read_models.include?(model.name) %></label><br />
</td>
<td width=10>
<label><%= check_box_tag "role[write_models][]", model.name, #role.write_models.include?(model.name) %></label><br />
</td>
<td><%= model.name %></td>
</tr>
<% end -%>
It works great by the way. In the log it saves the array properly like so:
"read_models"=>["Slug", "Account", "Category", "Document", "Group", "Location", "Role", "Status", "Task", "Ticket"]
But when outputting the results:
<%= #role.read_models.each do |model| %>
<%= model %><br />
<% end -%>
I get this:
---
- Slug
- Account
- Category
- Document
- Group
- Location
- Role
- Status
- Task
- Ticket
(Including the three dashes in the front)
I've tried doing to_a.join(', ') but it still has the dashes in front of each one.
Any ideas on how I need to change this process? Thanks!

I am guessing you have a Role class and this class is the one used to store these values, if this is your case, here's what you could do:
class Role < ActiveRecord::Base
serialize :read_models, Array
end
This will make ActiveRecord store these values you have at the read_models column as a YAML representation (this one you already have) but then you make #role.read_models you will get the Array back and not a string containing the YAML representation.

Related

Deleting ALL ActiveRecord Join associations through a rails form. (Adding and modifying works fine)

So I think I've narrowed down how to create/modify associations through forms. However, I cannot seem to remove all associations through this same method, because the submitted parameters include a blank array (nothing is selected in the form). When the array is empty, Rails does nothing instead of deleting all the association records.
So here's an example of my application. Here are two models:
#app/models/student.rb
class Student < ApplicationRecord
has_and_belongs_to_many :classes
end
and
#app/models/class.rb
class Class < ApplicationRecord
has_and_belongs_to_many :students
end
Now let's say my form is for Student:
<%= form_with(model: #student, local: true) do |form| %>
<table class="table table-striped table-bordered table-hover student-datatable">
<thead>
<tr>
<th><%= check_box_tag "student_header_checkbox", 0, false %></th>
<th>Class Name</th>
</tr>
</thead>
<tbody>
<% #classes.each do |class| %>
<tr>
<td><%= check_box_tag "student[class_ids][]", class.id, is_student_part_of_class(class) %></td>
<td><%= class.name %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success btn-sm">
<i class='fa fa-save'></i> Save changes
</button>
</div>
<% end %>
Now in my Student controller, I am permitting class_ids by doing this at the bottom:
#app/controllers/students.rb
def student_params
params.require(:student).permit(:class_ids => [])
end
Ok, so all is well. When the user selects many classes, the classes are passed to the Student controller as an array. If there are classes selected, then an appropriate association record is created, and now that student "has_and_belongs_to_many" classes.
Now here's the problem
Let's say you have multiple classes added to this student, if you delete all the classes, then there is basically no array that gets passed to the controller; therefore, the controller does not delete all classes associated with this student.
If you modify the selection by, let's say, adding a class, removing a class, etc. anything but deselecting ALL classes, then everything works just fine.
Does rails not handle deleting all association records automatically like this? Or am I doing something wrong or missing something?
I just made an additional function in the controller to solve this problem as a temporary workaround for now:
# called after update
def delete_all_associations_if_empty
classes = student_params[:classes]
if classes.nil? or classes.empty?
#student.classes = []
end
end

Sorting an index through an associated table?

I have an index that displays all of the current "dprojects" created. I'm working on being able to click the table headers to sort the data by that parameter. I have this all working except for one column. This column displays our customers by the number we've assigned them. For example, "Customer A" is "008". The "dprojects" table has a column for customer ID, and the "schools" table has columns for both the ID and the actual name of the customer.
Because it is a "dprojects" index, the table is sorting by the ID associated with the customer, and not the customer's name. How can I get it to sort alphabetically by name? Here's my code:
view: (dname is the customer name within the school model)
<table>
<thead>
<tr style="border-bottom: 2px solid black;">
<th> <%= sortable "scode", "School" %></th>
</tr>
</thead>
<tbody>
<% #dprojects.where.not(status: "Completed").each do |dproject| %>
<tr>
<td width="20%" class="dotted"><%= dproject.school.dname[0,25] + "..." %></td>
</tr>
<% end %>
</tbody>
</table>
model:
class Dproject < ActiveRecord::Base
belongs_to :school, foreign_key: 'scode'
end
controller:
def index
#dprojects = Dproject.order(params[:sort])
respond_with(#dprojects)
end
helper:
module DprojectsHelper
def sortable(column, title = nil)
title ||= column.titleize
link_to title, :sort => column
end
end
I thought I would be able to modify the view with the line:
<th> <%= sortable "dproject.school.dname", "School" %></th>
but that doesn't work. What am I missing? Thanks for any assistance!
I suppose it is because there is no such column (dproject.school.dname) in #dprojects. You need to join the tables in order to access also the columns of the model School.
I was tested the sorting on a model Book Author, where Book belongs_to :author, pretty similar to your. For the sorting to work I did like what follows:
In controller there is a join table, note the aliases (AS):
sort_by = params[:sort] || 'author_name'
#books = Book.joins(:author).select('books.name AS book_name, authors.name AS author_name').order(sort_by)
I used sort_by to avoid ambiguous column name, as for me there is name column in both models.
Then in view:
<p>id | <%= sortable 'book_name', 'Book' %> | <%= sortable 'author_name', 'Author' %></p>
<% #books.each do |book| %>
<p><%= book.book_name %> | <%= book.author_name %> </p>
<% end %>

Rails: how to render nested form in table

For example, I have two models:
class Task < ApplicationRecord
has_many :task_details
end
class TaskDetail < ApplicationRecord
belong_to :task
end
I want to display a table, each row in table is one TaskDetail and allow user input. After that user submits, all data will put to server. Here is my code:
(Note that: I #data[:task] is a task object because I want to return a hash with some information for view)
<%= form_for #data[:task], :url => tasks_path do |f| %>
<table> ... </table>
<% end %>
My question is: How can I do as my requirement.
thanks
Ensure that your Task model has accepts_nested_attributes_for :task_details and then you can do something like...
<%= form_for #data[:task], :url => tasks_path do |f| %>
<table>
<tr>
<th>Task Name</th>
<th>Task Description</th>
<tr>
<%= f.fields_for :task_details do |task_detail| %>
<tr>
<%= task_detail.hidden_field :id %>
<td><%= task_detail.text_field :name %></td>
<td><%= task_detail.text_field :description %> </td>
<tr>
<% end %>
</table>
<% end %>
Note the use of the hidden field for :id ... you need that so that rails can distinguish data from existing tasks versus a new task you're entering.
In your new method you should ensure there's at least one new task detail to provide an empty line on the form to input the detail
def new
...
#data[:task].task_details.build
...
end

Nested forms in rails for existing objects

In my app, I have a page where I want admin users to be able to update a particular characteristic of my "Package" model, which belongs to both the "Order" model and the "Item" model. It's a little complicated, but I basically want to present in a table all of the Packages belonging to a given Item, ordered in a particular way (that's what my packages_for_log method below does), with two blanks for updating the weight of the item. All the updates should ideally be submitted at once, with a single submit button at the bottom of the page. I've attempted a whole bunch of solutions, and my current one is below, but gives this error when I visit the page in my server:
undefined method `actual_lbs' for #<ActionView::Helpers::FormBuilder:0x007ff67df6c9c8>
The error's confusing to me, cause I was hoping that I was calling that method on the package instance, not a helper. Bit confused. At any rate, my code is below. The relevant section of the view:
<% form_for(#item) do |a| %>
<% #item.packages_for_log.each do |p| %>
<%= a.fields_for p do |i| %>
<tr>
<td><%= p.order.name %></td>
<td><%= p.order.processed_notes %></td>
<% if p.order.user %>
<td><%= "#{p.order.user.name.first(3).upcase}-#{p.id}" %></td>
<% else %>
<td><%= p.order.id %></td>
<% end %>
<td>
<%= i.text_field :actual_lbs %>
</td>
<td>
<%= i.text_field :actual_oz %>
</td>
<%= i.hidden_field :true_weight, value: (i.actual_lbs + i.actual_oz/16) %>
</tr>
<% end %>
<% end %>
<% end %>
Relevant section of the package.rb model file:
attr_accessible :order_id, :price, :true_weight, :actual_lbs, :actual_oz
attr_accessor :actual_lbs, :actual_oz # These two are virtual attributes for the above calc
And I added resources :packages to my routes file.
Any idea what I'm doing wrong? It's important to me that I loop through to create a table based on "p" and then edit that same "p" object. Just not sure how to do it. Pretty new to Rails.
I think your problem is this line:
<%= i.hidden_field :true_weight, value: (i.actual_lbs + i.actual_oz/16)
You need to put p.actual_lbs and p.actual_oz
EDIT: By the way, you probably need to move the true weight calculation to your controller action (CREATE action). I don't think :true_weight will get passed as you intended it to, using the above method.

Rails Update Single Attribute with Multiple Fields

I'm currently trying to make a form called countcash that allows users to input the actual number of quarters, dimes, nickels, pennies, twenties, tens, fives, ones and then the monetary value of any other items in the cash box and then when they hit submit, I want to total those values and set the cashstart field of my current shift record.
The real issue is that I'm not sure what my form is supposed to look like and how the controller/model are supposed to be setup in this instance.
I have a shift_id saved in a session[:shift_id] variable and the shift with that id is the shift that I want to modify with the calculated cashstart.
What does the <%= form_for %> tag need to look like if I want to follow the MVC correctly and how do I set up the controller/model to update the database with the calculated value? And what should I be setting the route to? I can't find anything like this on either stack overflow or other rails forums (Although you'd think that modifying a single attribute through multiple text fields would be pretty straight forward)
Just as a reference, this is the form I was using previously (To no avail)
app/views/countcash.html.erb
<%= form_for #shift do |f| %>
<table>
<tr>
<th>Coins</th>
<th></th>
</tr>
<tr>
<td>Quarters: </td>
<td><%= f.text_field :quarters %><br /></td>
</tr>
<tr>
<td>Dimes: </td>
<td><%= f.text_field :dimes %><br /></td>
</tr>
<tr>
<td>Nickels: </td>
<td><%= f.text_field :nickels %><br /></td>
</tr>
<tr>
<td>Pennies: </td>
<td><%= f.text_field :pennies %><br /></td>
</tr>
<tr>
<th>Bills</th>
<th></th>
</tr>
<tr>
<td>Twenties: </td>
<td><%= f.text_field :twenties %><br /></td>
</tr>
<tr>
<td>Tens: </td>
<td><%= f.text_field :tens %><br /></td>
</tr>
<tr>
<td>Fives: </td>
<td><%= f.text_field :fives %><br /></td>
</tr>
<tr>
<td>Ones: </td>
<td><%= f.text_field :ones %><br /></td>
</tr>
<tr>
<th>Other</th>
<th></th>
</tr>
<tr>
<td>Value (in $): </td>
<td><%= f.text_field :others %><br /></td>
</tr>
</table>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And here's my shifts_controller that doesn't do anything at the moment, since I'm not sure how I need to go about updating the attribute after the submit button is clicked
def countcash
#shift = Shift.find(session[:shift_id])
redirect_to(login_path)
end
I think you are almost there, presumably you have a named route which allows you to do this as your form declaration:
<%= form_for #shift, :url => countcash_shift_path(#shift) do |f| %>
If in doubt, run rake routes in your console to determine if such routes exist. If it doesn't, perhaps you would want to explore RESTful routes, which should look something like this in your routes.rb:
resources :shifts
Inside the countcash action in your controller, it's just a matter of doing this:
#shift = Shift.find(session[:id])
#shift.update_attributes(params[:shift]
This should work assuming that you have you have set up RESTful routes for your shift resource. Also, I am assuming that your model/database is set up with quarters, dimes, etc as individual fields (otherwise you will require some more advanced than update_attributes).
Since you are not having these individual fields in the database (as per your comment), you should not be putting as part of the model's form. So instead of:
<%= f.text_field :dimes %>
You'd do something like:
<%= text_field_tag :dimes, params[:dimes] %>
And inside your controller, you can just access them like so:
params[:dimes] + params[:quarters] # multiply as per currency logic
If you want to get a little more fancy, what you could do is write methods that take in these values, so you still leave the form as your current state, but add methods that take in the values and do something with it like so (for all types of currency, not just dimes):
def dimes=(value)
#running_total += value
end
In your controller, do this instead:
#shift.attributes = params[:shift]
So what happens is that when you pass your model the hash of attributes, it will try to assign the values based on the symbol names. So the value of dimes in params will be passed in as if this was being called:
#shift.dimes = params[:shift][:dimes]
I think that should work for you, assuming that you use #running_total for something. You could use #amount or something if that's what you already have as a model attribute.

Resources