问题描述
我能够做到几乎走向幸福一路与红宝石的护栏,但最后一步缺失,这是不可能在网络上找到,因为太多的常用词的答案。
我认为,答案我的问题是平凡的谁做回报率在一段时间的人,但被警告说,问题的presentation会有些复杂。
让我们看到一个等同的问题!
模式:
-
出版商(ID,姓名,地址)
-
书(ID,标题,PUBLISHER_ID,publishing_year,UNIT_PRICE,数量)
-
sell_log(ID,USER_ID,时间戳,book_id,数量,UNIT_PRICE,评论)
自定义操作:
-
名称:卖(背景:一本书)
-
输入:
数量
,评论
,(隐式输入:的书。 ID
,时间戳
;衍生输入:USER_ID
,的书。 UNIT_PRICE
,book.qty
) -
结果:
-
sell_log附加
-
books.qty降低
-
-
可能的错误:
-
的数量非正或非整数。
-
在用户输入的数量大于可用数量(book.qty)
-
(供参考:这不是一个关于数据库设计的问题)
因此,我们有一个自定义窗体(隐藏书本ID,数量,评论),这是我们希望实现在一个类似的行为,作为一本书(编辑动作更新
)。什么是做的(几乎所有):
- books_controller.rb:新增 custom_qty_display
列
- books_helper.rb:
高清custom_qty_display_column(记录)
record.qty.to_label +[
的link_to(卖......\
{:控制器=> 书,:操作=> sell_form:ID => record.id,:页=>假 } \
{:位置=> 替换,:直列=>如此,:类=> 行动 } \
)\
+]
结束
- 意见/书籍/ sell_form.erb(唯一的关键细节)
<%
form_remote_tag(\
:URL => {:控制器=> :书籍:动作=> :出售,:ID => PARAMS [:ID]} \
) 做
%>
...
<%= submit_tag提交%>
<%=的link_to为_(:取消),main_path_to_return,:类=> 取消%>
<%结束%GT;
< DIV ID =as_books,邮件级=邮件容器/>
- books_controller.rb:
高清出售
错误= []#我们将收集的错误在这里的消息
#检查参数...
#检查可用数量的...
#如果错误仍然是空的位置,执行的操作
#如上述产生的输出:
如果request.xhr?
如果errors.empty?
#1:渲染的JavaScript它与在表中修改的行替换形式。
其他
#2:JavaScript的,它提供了错误,为用户呈现
结束
其他
如果errors.empty?
指数
其他
#Q3:重新显示形式和错误
结束
结束
结束
当前进度
当我点击一本书列表条目项消失了卖...链接,自定义表单出现,而不是它。在窗体的取消链接(和[X]按钮)完美的作品;提交按钮的作品(动作成功完成当输入是正确的)。
什么是不存在的形式保持在适当位置。从理论上讲,我应该返回相应的JavaScript上标有 Q1
,地方Q2
和 Q3
。我不想进行反向工程的事情,写JavaScript的手,因为在一个框架的升级,我将不得不重做这一步。我想生产就简单性和可维护性的最佳方式所需的JavaScript代码。正如我现在相信我的概念也不错。
版本信息
- 的JRuby 1.5.0
- 宝石
- 在轨2.3.4
- 的ActiveRecord 2.3.4
- 的ActiveSupport 2.3.4
(告诉我,如果什么都需要)
部分结果
#...
如果errors.empty?
渲染:行动=> on_update.js
其他
#...
结束
#...
步骤#1:你必须修改的link_to
你的助手,包括 EID
的link_to(关闭...\
{:控制器=> 书,:操作=> sell_form:ID => record.id,:页=>假:EID => @ controller.params [:EID]} \
{:位置=> 替换,:直列=>如此,:类=> 行动 } \
)
步骤#2:你必须修改 sell_form.erb
,并设置 parent_model
, parent_column
,嵌套
和 EID
在网址
。你必须设置更新
到 book_list
,你必须生成一个正确的HTML ID为形式与 element_from_id()
。
<%
form_remote_tag(\
:URL => {:控制器=> :书籍:动作=> :出售,:ID => PARAMS [:ID]:parent_column => 书,:parent_model => 出版商:嵌套=> 真,:EID => PARAMS [:EID]} \
,:更新=> :book_list \
,:HTML => {:ID => element_form_id(:动作=>:更新)} \
) 做
%>
第三步:修改如果request.xhr
部分以下简单的code? (不完全测试,在最好的情况下正常工作。)
如果request.xhr?
如果@ record.valid?
渲染:行动=> on_update.js
其他
渲染:行动=> form_messages.js
结束
其他
如果@ record.valid?
return_to_main
其他
渲染:行动=> sell_form
结束
结束
I managed to do almost all the way towards happiness with my custom form in ruby-on-rails, but the very last step is missing and it is impossible to find the answer on the net because of too many common words.
I believe that the answers to my questions are trivial for people who have done RoR for a time, but be warned that the presentation of the question will be somewhat complicated.
Let's see an equivalent problem!
Schema:
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)
Custom action:
Name: Sell (context: a book)
Input:
qty
,comment
, (implicit input:book.id
,timestamp
; derived input:user_id
,book.unit_price
,book.qty
)Result:
sell_log is appended
books.qty decreased
Possible errors:
The qty is non-positive or non-integer.
The qty at the user input is greater than the qty available (book.qty)
(FYI: It is not a question about database design.)
So we have a custom form (hidden book-id; qty, comment) which we want to implement as an action in a similar behavior as "Edit" of a book (update
). What is done (is almost everything):
-- books_controller.rb: Added custom_qty_display
column.
-- 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
-- views/books/sell_form.erb (only key details)
<%
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
Current progress
When I click the "Sell..." link at a book list entry the entry disappears, custom form appears instead of it. On the form the "Cancel" link (and [X] button) works perfectly; the SUBMIT button works (the action is completed successfully when the input is correct).
What is not there is that the form remains in place. In theory I should return the appropriate javascript on places marked with Q1
, Q2
and Q3
. I do not want to reverse engineer things and write javascripts with hand because on a framework upgrade I would be forced to redo this step. I want to produce the necessary javascripts in the best possible way regarding simplicity and maintainability. As I believe now my concept is not bad.
Version information
- JRuby 1.5.0
- gems
- rails 2.3.4
- activerecord 2.3.4
- activesupport 2.3.4
(Tell me if anything else needed)
Partial result
# ...
if errors.empty?
render :action => 'on_update.js'
else
# ...
end
# ...
Step #1: You have to modify the link_to
in your helper to include eid
link_to( "Close..." \
, { :controller => "books", :action => "sell_form", :id => record.id, :page => false, :eid => @controller.params[:eid] } \
, { :position => "replace", :inline => true, :class => "action" } \
)
Step #2: You have to modify the sell_form.erb
and set parent_model
, parent_column
, nested
and eid
in the url
. You have to set the update
to book_list
, and you have to generate a proper HTML id for the form with element_from_id()
.
<%
form_remote_tag( \
:url => { :controller => :books, :action => :sell, :id => params[:id], :parent_column => "books", :parent_model => "Publisher", :nested => "true", :eid => params[:eid] } \
, :update => :book_list \
, :html => { :id => element_form_id(:action => :update) } \
) do
%>
Step #3: Modify the if request.xhr?
part to the following simple code. (Not fully tested, the best case works properly.)
if request.xhr?
if @record.valid?
render :action => 'on_update.js'
else
render :action => 'form_messages.js'
end
else
if @record.valid?
return_to_main
else
render :action => 'sell_form'
end
end
这篇关于回报率:如何处理自定义嵌套表格的提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!