Inserting form data into SQLite table using Rails - ruby-on-rails

I have created the table 'kids' which will store names as strings and numbers as integers. Inserting the values manually using an SQL query in the terminal works fine.
I am new to Rails and cannot seem to find any simple examples that show how to grab data from an html form and insert it into my SQLite table.
Here is the form I am dealing with:
<form>
Kid #1 Name:<input type="text"><br>
Favorite Number:<input type="text"><br>
Kid #2 Name:<input type="text"><br>
Favorite Number:<input type="text"><br>
<input type="submit" value="Submit">
</form>
What would the Controllers, Models, and Views look like in order to insert data from the form? It is 2 inserts into the table, but 1 form.

Create a method in your controller for making entries into the DB.
Make a method, say add_kids_data in your KidsController. So your URL for form will be /kids/add_kids_data.
Give names to your html text boxes appropriately. Say you name the boxes kid1, num1, kid2, num2, the values will be accessible in the controller as params[:kid1] and so on.
You would want to create a method in you model that will actually create an entry in your DB. You can then simply call the method in your model from the controller with the params that you received from the form.
This RailsGuides post on form helpers will give you step-by-step detail of what you need to do to create a form.

Related

Ruby on Rails - Update multiple data in a table with select (bulk edit)

(Rail 5 beta 3)
I have a table on an index page (action) of a view with around 15 columns. Some of them are for text and some of them are for integers only. Every entry of this list (table) will be filled out by a 'form_for' form (in new or edit action).
For editing or deleting there are links with each list entry in the index view leading to the corresponding show, edit or destroy actions. This all works well. Some of these entries are entered by a select with pulldown on the new or edit view. This works well, too.
But if one of these selects should be changed for more than one entry in the list it takes too much time to click on 'edit', change the select and click on submit at each list item. To make this a better user experience I would like to be able to change the selects in the list (table) directly. It would be good to have the select/pulldown in place. The change of the state or choosen entry should than be saved in place as well or with an extra button ("save changes") above/below the table.
To say it in short:
I want to update multiple entries in a table in an index view without editig each single entry via edit view. The dates will be changed by a select and the data should be saved by a submit button on this page
Has anybody an idea how I can solve this?
Try best_in_place gem. It can solve the problem you have quoted. here are some links to it
https://github.com/bernat/best_in_place
http://railscasts.com/episodes/302-in-place-editing?view=asciicast
Your original text wasn't that confusing to me...
You want what's called a bulk edit feature. Easiest way would be to set up a new target at the controller level specifically to handle this request. I'll give you the pseudocode and it should be easy enough to fill in the blanks, but essentially:
Create a "bulk edit" form (the drop down select menu above the table)
Create a controller method to handle the bulk edit (controller#bulk)
Update routes.rb to direct a URL to that new method
Handle the bulk update in the controller and redirect back to index upon completion (cheap way of updating the page after editing is done).
Note: I'm assuming your model name is "Resource" because you did not specify what it actually was
On your index page, you want HTML like:
<form method="POST" action="/resources/bulk">
<select name="bulk_value">...</select>
</form>
On change/form submit/whatever, submit that form.
If you're using resourceful routing, you can hook this into config.rb via:
resources :resources do
post :bulk, on: :collection
end
Otherwise, hook the route however you see fit.
Then, in your controller:
def bulk
value = params[:bulk_value]
Resource.update_all value: value
redirect_to {resources_path}
end
Now, you said index view, so I am assuming you want all. But if you just want a subset, you can pass the preferred IDs along with the form as a hidden field, then use a where clause to filter, i.e.
Resource.where(id: parmas[:id_array]).update_all(value: value)
Anyway, that should get you down the right path.

Rails - Don't want to create a database with only one field, but don't know any other methods

So I'm trying to have an "address" input field on one of my view page, and i want to use that "address" as one of my parameters in my API call.
I am not sure if I should create a whole new controller/model just for this one address field, and I am sure there are definitely better ways to do this.
The idea came from Postmates Website, where on the homepage you can enter your address and Postmates will give you a list of available delivery services. I am looking for something similar to this.
Thanks in advance.
Create a form who's method points to one of your controller actions and then use the data from the form in that action.
So:
<form action="route/that/handles/delivery/address" method="get">
<input name="address">
<input type="submit">
</form>
Then in your controller action:
def handle_address
params[:address] // do stuff with this
end
Why do you need just one field on the table?
I agree it might be better to just send and update the parameter you want to change by modifying the controller instead.

Rails form helper: Override id of Rails hidden model id

According to the docs for Form Helper's form_for:
The form_for method automatically includes the model id as a hidden
field in the form. This is used to maintain the correlation between
the form data and its associated model. Some ORM systems do not use
IDs on nested models so in this case you want to be able to disable
the hidden id.
This makes sense and is important, but if you have two form_for calls on the same page, it generates two hidden fields with the same markup and the same id. In my case, it generates this twice on the same page:
<input id="clinic_patient_signup_clinic_patient_link_person_attributes_patient_information_attributes_id" name="clinic_patient_signup[clinic_patient_link][person_attributes][patient_information_attributes][id]" type="hidden" value="32" /></div>
Is there a way to overwrite the id attribute of that input? If I remember correctly, it's just the name attribute that is important, and the value can stay the same. Our site has to be WCAG 2.0 accessibility compliant, and it won't allow two tags on the same page with the same id. (That's also not valid HTML.)
Use the form_for :namespace option.
See https://stackoverflow.com/a/26415985/2511083 for a complete answer.

How can I filter the auto populate when editing a form in Rails?

In Rails, when you go to the edit action, it automatically pulls the information from the models and populates the form. If I had a CRUD that saves sensitive information, for example password or ssn, how can I filter the values so that it doesn't just show it in plaintext when editing the form?
I was going to change the value in the controller by setting it to ****, but the potential risk there is people may submit the form and it will update the SSN to ****.
I'm not referring to filtering the params so it doesn't show up in console (config.filter_parameters).
Changing the input field to type="password" should resolve your problem. Just don't set the fields in the controller if they are blank.
If you are using a form builder, change text_field to password_field.

naming form files using square brackets

I've noticed in some php applications, that form fields are labelled with [] in them.
Say a shopping cart page, that lists all the items where you can edit the quantity.
is the [] type naming used to get the correct row?
Just trying to figure out how I should name each textbox?
should it be like:
name="quantity-<%= items.RowId %>"
THen when I loop the form fields, I would get the index number?
This is done in PHP when you need to pass <select multiple="multiple"> to the server, otherwise PHP will catch only the first selected value.
Textboxes don't normally require such naming.
This might answer a few of your questions. Here, Phil Haack uses square brackets in order to bind to a model that contains a list.

Resources