Ruby:从上一页的部分表格中预填充新模型表格

时间:2019-03-12 05:40:49

标签: ruby-on-rails simple-form

Ruby on Rails,简单形式。

我在多个页面(产品#index#show,静态“服务”页面)上有一个联系人/消息部分—当某人填写部分表格并提交时,我希望在Message#new页面上加载部分数据较大的联系表单,而无需创建新消息并发送操作邮件。

其他SO解决方案适用于多步骤表单,向导或通过URL传递数据。

这是我的代码:

_Shortcontact(部分)

<div class="short_contact_wrapper">
  <h4 id="short_container_title">Contact Us</h4>
  <div class="contact-container">
    <%= simple_form_for(message) do |f| %>
    <div class="form-row-2">
      <%= f.input :contactfirstname, label: false, placeholder: "* First Name", presence: true, :input_html => { :class => 'input_string' } %>
      <%= f.input :contactlastname, label: false, placeholder: "* Last Name" %>
    </div>
    <div class="form-row-2">
      <%= f.input :email, label: false, placeholder:"* Email" %>
      <%= f.input :phone, label: false, placeholder: "* Phone" %>
    </div>
  </div>

  <div class="contact-container">
    <%= f.input :product_app, label: "Product Application Category",  collection: ["Drones", "5G Telecom", "Thin Battery", "Auto Warehouse", "Robotics", "Wearables", "Electric Vehicles", "Industrial Personal Computers (Military)", "Industrial Personal Computers(Medical)", "Consumer Electronics", "Uninterruptible Power System", "Internet of Things", "Power Bank", "Other" ] %>
    <%= f.input :batteryapp, label: false, placeholder: "Please describe the product and how you\'ll use the lithium ion battery or cell." %>
    <div class="bottom_container" style="display: flex; justify-content: center;">
      <%= f.button :submit, "Submit", class: 'contactbutton' %>

    </div>
     <% end %>
  </div>
</div>

在我的产品控制器中#Edit和#Show:

before_action :build_message, only: [:index, :new, :show]

def index
    @products = Product.all
  end

  def new
    @product = Product.new
    # build nested models
    @product.product_specs.build
    @product.specifications.build

  end

  def create
    @product = Product.new(product_parameters)
    @product.category_id = product_parameters[:category_id]

    # setting product stuff for TESTING
    @product.picture = "products/IPC_tablet.png"
    @product.iconblack = "icons/products/icon_IPC_black.png"

    if @product.save!
      flash[:success] = "You have saved the #{@product.name} product"
      redirect_to product_path(@product)
    else
      flash.now[:error] = "Product was not saved"
      render "new"
    end
  end

  def show
    @product_specs = @product.product_specs
  end

  def edit
      # build nested models
    @product.product_specs.build
    @product.specifications.build
  end

def build_message
    @message = Message.new
  end

消息控制器

def new
    @message = Message.new
    @message.build_company

    puts "#{params[:message].inspect}"
    puts "are there parameters?"
    puts "#{@message.inspect}"
  end

  def create
    @message = Message.new(message_parameters)

    respond_to do |format|
      if @message.save!
        #send through mailer
        MessageMailer.send_message(@message).deliver_now
        format.html {redirect_to message_path(@message)}
      else
        format.html {render action: 'new'}
        format.json {render json: @message.errors, status: :unprocessable_entity }
      end
    end
  end

**编辑:我称为_shortform的示例:布局/产品**

<%= render '/partials/head' %>
<%= render '/partials/header' %>
<%= render 'layouts/messages' %>

<div class="product_layout_container">
<div class="layout_sidebar">
  <%= link_to products_path do %>
    <h4>Products</h4>
  <% end %>
  <%= render 'products/productsidebar', locals: {products: @products, categories: @categories } %>
</div>



  <div class="product_yield">
    <%= yield %>
  </div>
</div>


<div class="page_container bottom_container">
  <% unless user_signed_in? %>
    <%= render partial: 'messages/shortcontact', locals: { message: @message }%>
  <% end %>
</div>



 <%= render '/partials/footer' %>
 <%= render '/partials/foot' %>

我需要帮助弄清楚如何构造我的部分表单,或者如何预填充Message#new表单。...谢谢!

0 个答案:

没有答案