我在ruby-on-rails中用自定义表单设法做了几乎所有的快乐事情,但最后一步已经失踪,无法找到答案因为太多的常用词汇网。RoR:如何处理自定义嵌套表单的提交
我相信我的问题的答案对于一段时间内完成RoR的人来说是微不足道的,但要注意的是,问题的表述会有些复杂。
让我们来看看一个等价的问题!
模式:
publishers (id, name, address)
books (id, title, publisher_id, publishing_year, unit_price, qty)
sell_log (id, user_id, timestamp, book_id, qty, unit_price, comment)
自定义操作:
名称:卖(上下文:一本书)
输入:
qty
,comment
,(隐式输入:book.id
,timestamp
;衍生的输入:user_id
,,book.qty
)结果:
sell_log附加
books.qty降低
可能的错误:
数量是非正数或非整数。
在用户输入的数量超出了可用的数量(book.qty)
更大(FYI:这不是一个有关数据库设计的问题。)
所以我们有一个自定义的表单(隐藏的book-id; qty,comment),我们想要将其作为一个类似于“编辑”书籍的行为来实现(update
)。什么是(几乎所有):
- books_controller.rb:添加custom_qty_display
列。
- books_helper.rb:
def custom_qty_display_column(record)
record.qty.to_label + " ["
link_to("Sell..." \
, { :controller => "books", :action => "sell_form", :id => record.id, :page => false } \
, { :position => "replace", :inline => true, :class => "action" } \
) \
+ "]"
end
- 视图/书籍/ sell_form。ERB(唯一的关键细节)
<%
form_remote_tag(\
:url => { :controller => :books, :action => :sell, :id => params[:id] } \
) do
%>
...
<%= submit_tag 'Submit' %>
<%= link_to as_(:cancel), main_path_to_return, :class => 'cancel' %>
<% end %>
<div id="as_books-messages" class="messages-container" />
- books_controller.rb:
def sell
errors = [] # We will collect error messages here
# Checking parameters ...
# Checking of available qty ...
# If "errors" is still empty here, perform the action
# Produce the output according to the above:
if request.xhr?
if errors.empty?
# Q1: rendering of javascript which replaces the form with the modified row in the table.
else
# Q2: rendering of javascript which provides the "errors" for the user
end
else
if errors.empty?
index
else
# Q3: Redisplay the form and errors
end
end
end
最新研究进展
当我点击 “卖出...” 链接一书列表条目该条目消失,自定义窗体出现,而不是它。在表单上的“取消”链接(和[X]按钮)完美地工作; SUBMIT按钮有效(当输入正确时,操作成功完成)。
什么是不存在的形式仍然存在。理论上,我应该在标有Q1
,Q2
和Q3
的地方返回适当的javascript。我不想逆向工程并用手写javascript,因为在框架升级时我会被迫重做这一步。我想以简单和可维护性最好的方式制作必要的javascript。因为我现在相信我的概念并不差。
版本信息
- 的JRuby 1.5.0
- 宝石
- 轨2.3.4
- 的ActiveRecord 2.3.4
- 的ActiveSupport 2.3.4
(告诉我,如果别人需要什么)
部分结果
# ...
if errors.empty?
render :action => 'on_update.js'
else
# ...
end
# ...
ARGH ...错过了问题中的观点。无论如何,因为这是与部队的一周战斗。这对宇宙是有价值的(我希望)。请参阅编辑日志 – Notinlist 2010-09-02 09:09:25