我正在创建一个表单,我想在@post
的新表单中添加@article
编辑按钮。怎么做?我环顾四周但还没有找到解决方案。
我正在为宴会厅创建一个新帖子。客户按“下一步”按钮创建新文章,其中包含Post和要输入的新数据的数据。 然后他们使用个人信息进入下一个表单。这是我表格的一部分。所有脚手架系统。 我需要在f.submit
之后或在控制器中编写什么内容?在我看来,最清楚的是这一切。
文章/ new.html.erb:
<%= form_for @article do |f| %>
<%= f.datetime_select :begin %> # The default is @post.begin, after saving goes to Article table. Customers can edit the date.
<%= f.datetime_select :end %> # The default is @post.end, after saving goes to Article table. Customers can edit the date.
<%= @price_for_banquet_hall %> # Big formula, depends on dates difference. This I want to recalculate, but I'm weak on scripting with Ajax, so i need this edit button of @post only that customers here would see the new price.
<%= f.submit ... %> # This should edit data of table Post (because @article not yet created, its creating here) and reload this page again(just editing data of @post).
<%= f.submit, new_contact_path(@article) %> # This should save data to @article and proceed.
<% end %>
现在是我的表格:
物品/新
<%= form_for @article, remote: true do |f| %>
<table id="table">
<%= f.datetime_select :arrival, :default => Post.last.begin.to_datetime %>
<%= f.datetime_select :departure, :default => Post.last.end.to_datetime%>
<%= @price_for_banquet_hall %>
</table>
<%= f.submit %>
<% end %>
物品/ create.js.erb
$("#table").html("<%=j render partial: "new_article", locals:{@price_for_banquet_hall => @price_for_banquet_hall} %>")
我不会静止
答案 0 :(得分:1)
如果你看一下你的表格
<%= form_for @article do |f| %>
<%= f.datetime_select :begin %> # The default is @post.begin, after saving goes to Article table. Customers can edit the date.
<%= f.datetime_select :end %> # The default is @post.end, after saving goes to Article table. Customers can edit the date.
<%= @price_for_banquet_hall %> # Big formula, depends on dates difference. This I want to recalculate, but I'm weak on scripting with Ajax, so i need this edit button of @post only that customers here would see the new price.
<%= f.submit ... %> # This should edit data of table Post (because @article not yet created, its creating here) and reload this page again(just editing data of @post).
<%= f.submit, new_contact_path(@article) %> # This should save data to @article and proceed.
<% end %>
以您的形式
<%= form_for @article do |f| %>
将生成
<form accept-charset="UTF-8" action="/articles/create" method="post">
所以从这里开始生成表单的路径。 您可以在表单中使用两个提交按钮,但它们始终会让您在控制器中执行相同的操作。
我需要更多关于你如何处理表单的信息,比如模态或什么。如果您使用模态,并且您希望有两个按钮用于创建帐户,第二个用于在单独的模式中呈现不同的表单,那么您可以执行的是而不是第二个提交按钮有一个链接,它将关闭您的新表单模式并打开您的不同模态表单
关于ajax的事情,如果你想通过ajax处理它,你只需要在你的表单中放置remote:true选项,如:
<%= form_for @article, remote: true do |f| %>
#your fields
<% end %>
这将带您在文章控制器中创建操作,您可以像这样处理它:
def create
@article = Article.new(article_params)
respond_to do |format|
if @article.save
format.html{redirect_to your_path}
format.js{} #this will allow rails to looks for a create.js.erb file in your views/article
else
format.html{render new}
end
end
end
最后,您可以在create.js.erb文件中呈现包含编辑表单的新部分。有关详细信息,请参阅Working with Javascript in Rails
<强>更新强>
在create.js.erb文件中,您可以执行以下操作:
$("#some_id_of_parent_element").html("<%=j render partial: 'your_partial', locals:{:your_variable => partial_variable} %>")
<强>更新强>
你的表格应该是
<table id="table">
<%= form_for @article, remote: true do |f| %>
<%= f.datetime_select :arrival, :default => Post.last.begin.to_datetime %>
<%= f.datetime_select :departure, :default => Post.last.end.to_datetime%>
<%= @price_for_banquet_hall %>
<%= f.submit %>
<% end %>
</table>
并且您的create.js.erb文件将是:
$("#table").html("<%=j render partial: 'your_edit_partial', locals:{@price_for_banquet_hall => price_for_banquet_hall} %>")
由于您在js.erb中使用@price_for_banquet_hall
,因此您必须在创建操作中对其进行初始化。