Ruby: 从上一页的部分表格预填新模型表格
Ruby: Prefill New Model Form From Previous Page's Partial Form
Ruby Rails,简单格式。
我在几个页面上有一个 contact/Message 部分(product#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 的示例:Layout/products **
<%= 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 表单....谢谢!
首先添加 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)
Ruby Rails,简单格式。
我在几个页面上有一个 contact/Message 部分(product#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 的示例:Layout/products **
<%= 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 表单....谢谢!
首先添加 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)