Save completed form to database - Ruby on rails

I am trying to submit a form to an existing database in ruby ​​on rails. I have done some research, but none of this 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 to bind a form to a controller. Also, the form has fewer fields than the table columns, is it a potential problem? Thank!

+3


source to share


3 answers


Your understanding of what's going on in the code you are using is cloudy, so I'll go over the terminology you use and the extent of the concepts presented therein:

  • The form is a concept that is represented in HTML as <form>

    an element and a series of input elements and is represented in object-oriented programming in various ways as form objects. Rails, in particular, has no opinion or recommendation on how or for what to use Form Objects.
  • Database is a piece of software that handles storing and retrieving data using an interface; most relational databases use SQL as their interface.
  • When you submit the form, you instruct your browser to create an HTTP request for the action value of the tag <form>

    that contains 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 usual way to handle a request in Rails is to redirect it to a controller action.
  • In Rails, an action is created by defining a method on a controller.

As for your question, there is no "communication" between the form and the controller or action in the controller, but when the form is submitted, the request triggers a controller action that can handle the request.



You just access the parameters, ideally using StrongParams (as you showed), and perhaps instantiate an ActiveRecord model to validate and save the input to your database. The ActiveRecord Basics Guide provided by Rails is a great place to learn more about the actual approach to this.

Yes , it is ok to have a form that represents values ​​that are not representative of a row in a database column. It's up to your code to deal with what it looks like. You can have a form that represents multiple rows across multiple tables in your database, or one that represents just one column out of a dozen.

+4


source


Okay, strong stats are great! There are a few things you must do to figure out how to make this work. First, yes, you can only post information to your database for the EXISTING column. If all those attributes that you resolve through your strong parameters (landslide_params) are in the database, you should be fine. If not, create a couple of migrations to add them:

rails g migration AddAttributesToLandSlide location:string fatalities:integer

and etc.

Second, you need to figure out which method in the controller you will be submitting when you click submit on your form. I assume it will be the create method in landlide_controller or something along those lines.



Typically you will have a "new" method in your controller that will traverse an empty Landslide.new object and then display the form / new landslide page. By filling out the form and hitting submit, Rails will take you to the create method where you can save the landslide with all the attributes.

Keep in mind that you must have

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

In your routes file for this.

+3


source


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

.

Take a look at the documentation form_for

. I use it for create

both and for update

and automatically binds it to the controller depending on whether I am in mode new

or edit

:

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

0


source







All Articles