如何使用ROR将表单(scaffold)嵌入到索引页中?

时间:2016-10-10 18:56:47

标签: forms ruby-on-rails-4 scaffolding

我创建了一个名为Contacts的脚手架,并为其提供了条目

name:string phone:string email:string

我希望表单在索引页面上可见,以便app用户提交。

目前,空白表格位于localhost:3000 / contacts / new 提交后,应用程序会显示提交的信息。

首先,如何将表单嵌入索引页面?

index.html.erb

<p id="notice"><%= notice %></p>

<h1>Contacts</h1>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Email</th>
      <th>Phone</th>
      <th>Message</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @contacts.each do |contact| %>
      <tr>
        <td><%= contact.name %></td>
        <td><%= contact.email %></td>
        <td><%= contact.phone %></td>
        <td><%= contact.message %></td>
        <td><%= link_to 'Show', contact %></td>
        <td><%= link_to 'Edit', edit_contact_path(contact) %></td>
        <td><%= link_to 'Destroy', contact, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= render 'contacts/form'  %> 
<%= link_to "Apple", {:controller => 'contacts', :action => 'apple'} %>
<%= link_to 'New Contact', new_contact_path %>

_form.html.erb

<%= form_for(contact) do |f| %>
  <% if contact.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(contact.errors.count, "error") %> prohibited this contact from being saved:</h2>

      <ul>
      <% contact.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>

  <div class="field">
    <%= f.label :email %>
    <%= f.text_field :email %>
  </div>

  <div class="field">
    <%= f.label :phone %>
    <%= f.text_field :phone %>
  </div>

  <div class="field">
    <%= f.label :message %>
    <%= f.text_area :message %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

contacts_controller.rb

class ContactsController < ApplicationController
  before_action :set_contact, only: [:show, :edit, :update, :destroy]

  # GET /contacts
  # GET /contacts.json
  def index
    @contacts = Contact.all
    
  end

  # GET /contacts/1
  # GET /contacts/1.json
  def show
  end

  # GET /contacts/new
  def new
    @contact = Contact.new
  end

  # GET /contacts/1/edit
  def edit
  end

  # POST /contacts
  # POST /contacts.json
  def create
    @contact = Contact.new(contact_params)

    respond_to do |format|
      if @contact.save
        format.html { redirect_to @root_path, notice: 'Contact was successfully created.' }
        format.json { render :show, status: :created, location: @contact }
      else
        format.html { render :new }
        format.json { render json: @contact.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /contacts/1
  # PATCH/PUT /contacts/1.json
  def update
    respond_to do |format|
      if @contact.update(contact_params)
        format.html { redirect_to @contact, notice: 'Contact was successfully updated.' }
        format.json { render :show, status: :ok, location: @contact }
      else
        format.html { render :edit }
        format.json { render json: @contact.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /contacts/1
  # DELETE /contacts/1.json
  def destroy
    @contact.destroy
    respond_to do |format|
      format.html { redirect_to contacts_url, notice: 'Contact was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_contact
      @contact = Contact.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def contact_params
      params.require(:contact).permit(:name, :email, :phone, :message)
    end
end

2 个答案:

答案 0 :(得分:0)

假设您的表单已保存为部分,您可以添加 &lt;%= render&#39; form&#39; %GT;到索引页面。用&#39;形式&#39;被实际的表单路径替换。

答案 1 :(得分:0)

尝试以下

<%= render partial: "contacts/form", locals: { contact: @contacts } %>