So what I have is a report model and an entries model. The report has many entries and entries belong to the report. What I am wanting to do is be able to click a button on the show page of the report and add new entries. Is there a way of doing this? A non-ajax solution is fine.
Cocoon is an excellent gem for this.
It gives you a Remove button for each entry and an Add button to add as many entries as you want. Add accepts_nested_attributes_for :entries to your Report model. You need to put the entries form in their own partial but that's not a hardship, and cocoon automatically creates the show/hide jquery for you. Be sure to include and whitelist the entries' id column as a hidden field otherwise you can end up with duplicate entries, and you also need to whitelist _delete (that's underscore + delete) so that the entries can be deleted by rails when required.
Related
In rails_admin, I have 1 model, for example User and I'm showing the whole user count on the Dashboard statistics.
I would also like to separately show 1 particular column from User on the Dashboard statistics and also able to create, update and delete them.
Is that possible? If yes, may I ask how?
You need to create a new rails admin action replacing the one called dashboard.
Check out how the "controller" looks
https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/config/actions/dashboard.rb
And the view
https://github.com/sferik/rails_admin/blob/master/app/views/rails_admin/main/dashboard.html.haml
A guide on how to create actions
https://github.com/sferik/rails_admin/wiki/Custom-action
What i would do is basically copy paste the files and add the custom column to the main loop iterating on the models.
You'll get the CSS and JS for free if you leave everything else as is.
I have a PurchaseOrder Model that has_many Items. The form for PurchaseOrder needs variable input fields that will also save Items, where clicking the Add button will increase the fields in the page.
Here's what it will look like:
In order to achieve this:
How do I create a simple_form that will post the result of these multiple fields as an array to my existing PurchaseOrdercontroller where I can process and add these records?
Bonus: how would I handle this via Cucumber?
Edit: Why would you downvote a self contained question? Leave comments to explain when you downvote, please.
Use Cocoon as your nested forms for creating multiple fields.
You need to use simple_fields_for from simple_form gem. This allows you to work with attributes of associated models.
If you want to add new associated models via Add new button you need to create a new row. There is no code in pure simple_form that will help you with that. I found gem cocoon. It looks like it's what you're looking for. You can take a look how it's implemented there and make your own light solution.
I'm not sure what's the best approach in my situation, I would like an opinion.
My situation is:
I have a "Ticket" model, having several fields of many kinds: text, numerical and associations. Tickets support comments through the acts_as_commentable gem.
The tickets are generated by users, who can comment and modify their own tickets.
Because the fields of a ticket can change over time I would like to allow my users to modify several of them. What I need though is to keep a commented history of all the changes, so that at any moment they can see in a ticket what, why and when was changed, in the comment, timestamp and list of changes that they can see together with the comment.
I was thinking to solve this by generating a "TicketUpdate" model, have the TicketUpdates generated in the Comments form (using fields_for and accepts_nested_attributes_for).
Basically the user could select in a drop down list (i.e. a select tag) the field they want to change, changing the value of the drop down would trigger an event to show an appropriate input field (input for the text and numeric fields, select for the associations) with the old value pre-populated
I could intercept the TicketUpdates in the "comment/create" controller performing the updates.
This approach would look nice and sweet to the user, but I don't see how to implement in a neat or DRY way.
Because ticket has many fields and they are mixed decimals and associations, I would have to implement specific logic for each field, both in the view and in the controller.
I'm not sure if there's maybe a better approach, or there's actually any gem or trick to get this done easily.
anyone got anything to recommend me here?
I'm using rails 3.2.8.
I'm building an app called "CourseWork to dig into rails/develop my skills and I have a question about how to structure it. Users have a resource called "CourseGrading" that is able to create categories and belongs to "Course". Each "category" should have a name, a percentage out of 100 and a course_id. I need to add these percentages together and alert users if the total isn't 100 while still saving.
Then the user's generated "categories" should populate an enum_string specific to that user in a resource called "CourseAssignment" which has a name, description, category and finalgrade.
Can anyone give hints or resources for how best to accomplish this? Thanks
You probably want to take a look at Active Record Callbacks. These will allow you to insert some code to be run when creating/validating/updating/deleting models.
You should probably make use of the ActiveRecord validations.
Check out this guide that explains how to write your own custom validator. Your custom validator would run when the form gets submitted, and in it, you would grab the percentage params and do your check. If it's not what you expect, you can just add an error to the form and the validation process will just kick the user back to the form page and display the error.
So far, I've not been able to find a solution that is able to allow a form to interact with two models that have a one-to-many relationship. Say for example, a customer has many items.
Currently, I've implemented solutions using Javascript that enables users to add/remove rows to the item list. Let's keep it simple, we're creating new records here.
In improving this to be degradable, I've instead created a 'Add Item' button that commits a different value, so that in my create action, I'm able to capture the new attributes and add a new Item to the customer object.
However, since item is new and has no id, I have no way of removing a particular item. I've thought of using indexes and added post buttons/links to remove, but this sort of makes everything messy and I can't use my partial for collections. So I feel like it's a hack; is there a better alternative?
You want to use accepts_nested_attributes_for. The Railscasts episode henrikloevborg mentions covers a method of doing this kind of thing that's been obsoleted by accepts_nested_attributes_for.
You can do it without accepts_nested_attributes_for. Which might be necessary if you don't want to upgrade to Rails 2.3.
In your javascript to remove an item, you should add a hidden field to the form called _delete with the value of true. Then in your controller. Ignore any new records with the _delete field, and destroy any existing ones.
All that's built in to accept_nested_attributes_for
Play around with the code in the complex-forms-exmample github repository. It's essentially the code from Railscast #75 updated to use the newer feature, accepts_nested_attributes_for.
You will notice that it doesn't use RJS to accomplish the dynamic addition/removal of records. Also compared to what you've been doing it creates on submit, not during a remote call. The automatic addition/deletion of items is handled using content_for tags to create a template stored in a javascript function and the Prototype library to modify a hidden field, which accepts_nested_attributes_for uses to destroy objects you want to remove.