Ruby on Rails,简单形式。
我在几个页面上有一个联系人/消息部分(产品#index #show,静态'服务'页面) - 当有人填写部分表格并提交时,我希望Message#new page加载部分数据预填充更大的联系人表单,而不创建新邮件并发送动作邮件程序。
其他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 form ....谢谢!
首先添加an additional route(POST / messages / new):
resources :messages do
post :new, on: :collection
end
这有点可选。也可以只使用GET设置它,但参数将在URL中发送。
然后,您可以通过强大的参数简单地将参数绑定到模型实例。
class MessagesController
# ...
def new
@message = Message.new
# We need a condition here to avoid a parameter missing error
@message.assign_attributes(message_parameters) if params[:message]
end
end
您还必须手动覆盖表单的路径:
simple_form_for(message, url: new_message_path)