Ruby on rails 在新表单中编辑按钮
我正在制作一个表单,希望在新表单中为Ruby on rails 在新表单中编辑按钮,ruby-on-rails,forms,Ruby On Rails,Forms,我正在制作一个表单,希望在新表单中为@文章添加一个@post编辑按钮。怎么做?我环顾四周,但没有找到解决办法 我正在为宴会厅创建一个新职位。客户按下“下一步”按钮创建一篇新文章,其中是Post数据和要输入的新数据。然后他们进入下一个表格,填写个人信息。这是我表格的一部分。所有的脚手架系统在两个f.submit之后,或者在控制器中,我需要写什么?在我看来,这一切都很清楚 Articles/new.html.erb: <%= form_for @article do |f| %> &
@文章
添加一个@post
编辑按钮。怎么做?我环顾四周,但没有找到解决办法
我正在为宴会厅创建一个新职位。客户按下“下一步”按钮创建一篇新文章,其中是Post数据和要输入的新数据。然后他们进入下一个表格,填写个人信息。这是我表格的一部分。所有的脚手架系统在两个f.submit
之后,或者在控制器中,我需要写什么?在我看来,这一切都很清楚
Articles/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 %>
#默认值为@post.begin,保存后转到文章表。客户可以编辑日期。
#默认值为@post.end,保存后转到文章表。客户可以编辑日期。
#大公式,取决于日期的不同。这是我想重新计算的,但我不擅长用Ajax编写脚本,所以我需要@post的这个编辑按钮,只有这样这里的客户才能看到新的价格。
#这应该编辑表Post的数据(因为@article还没有创建,它是在这里创建的)并重新加载此页面(只是编辑@Post的数据)。
#这应该将数据保存到@article并继续。
这是我现在的表格:
文章/新
<%= 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 %>
Post.last.begin.to_datetime%>
Post.last.end.to_datetime%>
articles/create.js.erb
$("#table").html("<%=j render partial: "new_article", locals:{@price_for_banquet_hall => @price_for_banquet_hall} %>")
$(“#table”).html(“@price_for_bateet_hall}%>”)
如果你看你的表格,我就不会安静下来
<%= 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| %>
最后,您可以在create.js.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 %>
在create.js.erb文件中,可以执行以下操作:
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
$("#some_id_of_parent_element").html("<%=j render partial: 'your_partial', locals:{:your_variable => partial_variable} %>")
$(“#父元素的某些_id_”).html(“部分_变量}%>”)
更新:
<%= 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 %>
你的表格应该是
<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>
Post.last.begin.to_datetime%>
Post.last.end.to_datetime%>
您的create.js.erb文件将是:
$("#table").html("<%=j render partial: 'your_edit_partial', locals:{@price_for_banquet_hall => price_for_banquet_hall} %>")
$(“#table”).html(“宴会厅价格%>”)
此外,由于您在js.erb中使用的是宴会厅的
@price\u
,因此您必须在创建操作中对其进行初始化 也许可以在表单中添加一个内部表单来编辑表格帖子?是的,我也这么认为。但是如何在表单中添加表单?@mansim您的编辑表单与新表单不同,在提交新表单后,您应该重定向表单,如果您不想重定向,则使用ajax处理表单,并在创建文章后呈现编辑表单。是的,我想,就像使用ajax计算价格一样。。但是我不知道如何为它制作ajax。。从未尝试过。我需要这个表格来工作,而不是。我不知道它是否起作用,但我会试试。嗯,这是关于ajax的理解部分。如果我想用AJAX计算出的价格来代替@price\u for\u banner\u hall,我需要在.js表单中写些什么?我需要在那里以友好的方式呈现价格-日期之间的差异,所以我甚至不再需要编辑按钮。。但javascript对我来说还不清楚。@mansim抱歉没有得到你,宴会厅的@price是什么?我应该在.js文件中写什么?使用ruby,我将为datebegin创建一个=字段,为date.end创建一个=字段,为c=a-b创建一个=字段。如何在javascript或coffee中绑定所有这3个字段?@mansim它将是一个js.erb文件,你不应该在那里编写任何html,对它做不同的部分,然后像我在回答中所说的那样渲染它是的,读了第五遍,仍然不清楚其中的许多事情:)RoR的每一小步都是4小时+才能工作,因为“不言自明”的东西,他们不显示,我需要他们:)