I have a situation where I want a user to specify the number of records they want to create, have the app then create these blank entries in database and then be presented with an 'edit' view to populate them with their data. I know this is probably bad practice but can anyone give some guidance on how to approach this?
Why do you want to create blank record on the database? You can do like this :
The user choose the number of new record he wants then, he submits
You display the creation form X times
On general submit, you submit each form to your create action
I don't know if you know phpmyadmin, but they do the same.
A better approach is to create x numbers of new objects (ex. Post.new) in a loop/partial configuration. Doing a Post.new creates a new instance of the appropriate db row, but it stores that information in memory and doesn't create that object in the db until you issue the Post.save command. These virtual database fields have all of the appropriate columns so you will be able to pass them to an edit form and everything should work. The only thing that it's missing is an id which will be assigned to it after you save.
Related
Recently I had to create a couple of records in a non-rails app database table based on a previous record. It got me thinking of how would I do this in a rails app. I tried a couple of things in the Console, but nothing works.
I want to do something like this:
001> user = User.new(User.first)
I know this doesn't work but hopefully it will show you what I an thinking. User is a large table/model, and I only need to change a few fields. So, if I can set up a new record with the same values in User.first, I can then edit the fields I need to before .save-ing the record.
Thanks for any help.
I think what you want is:
user = User.first.dup
user.assign_attributes(email: "myemail#test.test")
user.save
The first line uses dup to create a copy of the object. The copy is not yet saved to the database. Replace dup with clone if you're using an old version of Rails (<3.1).
In the second line, assign_attributes alters the attributes of the object, still without saving it to the database. If you were working with an object already saved in the database, you could use update instead of assign_attributes to change the attributes of the object and save the changes in one go. That won't work here, because we haven't saved our duplicate user yet. More details on that here.
The third line finally saves the new object to the database. It saves time to just do this once, at the end.
I have my project making api fetches for "things" from my react front end and placing them in my redux state. When someone wants to add a review to one these "things" I have an onclick action on "add review" button which posts the "thing" to my database. This is so I can start to build relationships with that thing(I want users to be able to add reviews to it). The issue is the api gives the thing an id that the reviews want to reference when I add it. Active record on the other hand knows nothing about the api id and just gives it a normal primary key type of id and I get an error "Couldn't find a thing with id of 43565" because in my db it has as id of "3" or something. I have already tried serializing id attribute to make it show the api_id, but that didn't help. Any idea how I can address this?
In your create action you could initialize the object with the params.except(:api_id) and then manually assign it.
def create
thing = Thing.new(params.except(:api_id))
thing.id = params[:api_id]
thing.save!
end
And then you could perform whatever serialization or validations you wanted
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'm making an application that involves booking appointments for users. I have a User model and an AvailableDate model. The user has_many availble_dates and the AvailableDate belongs_to user.
I want to present a form for the user so that they can mark a couple of dates in a calendar and each of the dates they mark will become an AvailableDate object tied to that user.
At the moment my solution is to do all the work that a form_for helper would normally do manually. This involves a lot of javascript and is generally just getting far too messy.
I can't figure out how I should make a form_for tag work when I need to create potentially infinitely many dates. In theory a user could keep marking off dates in the future as available. If I knew how many dates I needed to create for a user, I could do user.available_dates.build, N times. But this doesn't work here.
Can anyone help? It like this problem should be pretty common. Am I designing my application wrong?
One technique is to render the fields for your association once, outside the form.
When the user performs whatever interaction that should create a new set of inputs you use javascript to clone the initial set of fields and insert them into the form. The one thing you need to do is change the name of these inputs so that they are unique. Usually people use the current time in milliseconds for this unique identifier.
Been there & have found several resources to help: Tutorial & Cocoon
The bottom line is you need to ensure child_index is unique for each field. The tutorial I use has child_index: Time.now.to_i to create a truly unique id, consequently allowing you to add as many fields as you want
The best way to do this:
Render fields_for as a partial (passing your form builder object)
When you want to add new field, create ajax_field action
Make ajax_field view have its own form_builder
Both your original & ajax_field forms will call the partial
On front-end, you can use JS to GET new form action & append field to page
I can give you code if you want
in the application i am currently creating in ruby on rails. I am trying to do some tests in rails console where i have to destroy data in the database and the database is connected to a server. I am importing an XML and parsing it and putting it into a database with scaffolding.
Now what i need: Basically what i am attempting to do is to destroy the data and replace it with a new one every week..but the problem i am getting, the userid is gone up to 700+ and there are only 50 records :S cause it doesnt reset...
To delete all records i am currently using "whatever.destroy_all" does the trick
Any help?
Btw i am using SQLITE
The ID column created in the table usually is set as unique and to increment by 1 for each new record, which is why each time you destroy and add new data the ID keeps getting higher.
The fact that the ID # is getting larger and larger is not an issue at all.
If you really want to start back at zero, I would think you could drop the table and recreate it, but that seems like overkill for a trivial issue.
Regarding the connection to the other scaffold, how are you connecting the two and what do they both represent?
Ideally the data population for testing should be done through fixtures (or easy tools such as factorygirl etc..)
The main advantage of having a fix data set is you can run your tests in any environment. But as per your requirement you can do something like this,
When you populate the date through the active records pass the id parameter as well
Ex: User.new(:id => 1, :name => "sameera").create
By this way you can have constant id's But make sure you increment the id accordingly.