简体   繁体   中英

How to Set Up A Form Submission System In Rails That Handles Many Different Fields

I'm trying to set up a page in Ruby on Rails which accepts a lot of information from the user via forms then saves it (& also allows fields to be editable). I've set up forms before but only for a single title/content form, what I want will be a page that will have a form for:

Personal Details: Name, Age, Contact Details

Company Details: Name, Age, Contact Details

I'm not sure at all how to do this, if anyone could link me to a guide that explains how to do this or can explain it to me it would be much appreciated.

For such a task you can just use scaffold rails g scaffold User age name street zip company_name etc.. Type this into your console

This will create a complete scaffold for you. You can create and edit your form. It's a good way to start if you have no other idea.

It sounds like you have "Company" in a separate model. If so, you need something similar to this code. Your controller will need to initiate both @user and @company.

<%= form_for(@user) do |f| %>
  <%= render 'shared/error_messages', object: @user %>

  <h3>Yourself</h3>
  <%= f.label :first_name %>
  <%= f.text_field :first_name, class: 'form-control' %>

  <%= f.label :last_name %>
  <%= f.text_field :last_name, class: 'form-control' %>

  <%= f.label :age%>
  <%= f.text_field :age, class: 'form-control' %>

  <%= f.label :email %>
  <%= f.email_field :email, class: 'form-control' %>

  <h3>Your Company</h3>
  <%= fields_for(@company) do |c| %>
    <%= c.label :name %>
    <%= c.text_field :name, class: 'form-control' %>

    <%= c.label :age%>
    <%= c.text_field :age, class: 'form-control' %>

    <%= c.label :email %>
    <%= c.email_field :email, class: 'form-control' %>
  <% end %>

  <%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM