Tri Nguyen
Tri Nguyen

Reputation: 1808

Save filled form to database - Ruby on rails

I'm trying to submit a form to an existing database in ruby on rails. I have done some research but none of that works. Here's my form: https://pastebin.com/JfXr054y

Here's my controller:

  def landslide_params
      params.require(:landslide).permit(:total_id, :year_id, :start_date, :end_date, :day_number, :continent, :country, :location, :type, :admin_level, :new_lat, :new_long, :mapped, :spatial_area, :fatalities, :injuries, :notes, :sources)
  end

How can I link the form to the controller. And also, the form has less fields than the table columns, is that a potential problem? Thanks!

Upvotes: 2

Views: 1236

Answers (3)

Kermit
Kermit

Reputation: 5992

You can specify form_tag controller: 'your_controller', action: 'your_controller_action'.

Have a look at form_for documentation. I use it to for both create and updateand it links it to the controller automatically based on whether I'm in new or edit view:

<%= form_for(@newsletter) do |f| %>

Upvotes: 0

coreyward
coreyward

Reputation: 80041

Your understanding of what's happening in the code you're using is cloudy, so I'll review the terminology you're using and the extent of the concepts represented first:

  • A form is a concept that is represented in HTML as <form> the element and a series of input elements, and represented in object-oriented programming in various ways as Form Objects. Rails, notably, doesn't have an opinion or recommendation on how or what to use Form Objects for.
  • A database is a piece of software that handles the storage and retrieval of data using an interface; most relational databases use SQL as the interface.
  • When you submit a form, you instruct your browser to create an HTTP request to the action value on the <form> tag that includes the values present in the form inputs.
  • When a browser sends a request to your Rails application, the application uses your routes to determine how to handle that request.
  • The conventional way to handle a request in Rails is to route it to a controller action.
  • In Rails, an action is created by defining a method on the controller.

Regarding your question, there's no “link” between the form and the controller or an action in the controller, but when a form is submitted, the request triggers a controller action that can handle the request.

You simple access the parameters, ideally using StrongParams (as you've shown), and probably create an instance of an ActiveRecord model to validate and persist the input to your database. The ActiveRecord Basics Guide provided by Rails is a great place to learn more about the de facto approach to this.

Yes, it's okay to have a form that submits values that are not representative of a row in a database column. It's up to your code to handle how that looks. You can have a form that represents multiple rows across multiple tables in your database, or one that represents just a single column out of a dozen.

Upvotes: 4

dpalazzari
dpalazzari

Reputation: 102

Alrighty, having strong params is great! There are a couple things you have to do to figure out to get this to work. Firstly, yes, you can only submit information into your database for an EXISTING column. If all those attributes you are allowing through your strong params (landslide_params) are in the database, you should be fine. If not, create a couple migrations to add them:

rails g migration AddAttributesToLandSlide location:string fatalities:integer

etc.

Secondly, you have to figure out what method in the controller you will be posting to when you hit 'submit' on your form. I assume it will be the create method in the landslide_controller, or something along those lines.

Usually, you would have a 'new' method in the controller that passes in an empty Landslide.new object, and then renders the form/new landslide page. Upon filling out the form and hitting submit, Rails would take you to the create method where you can save the landslide with all the attributes.

Keep in mind, you have to have

resources :landslides, only: [:new, :create]

In your routes file for this to work.

Upvotes: 3

Related Questions