rails rjs select method help

本文介绍了一个使用Rails的RJS技术实现的简单应用案例,该应用可以在用户输入两个文本框中的数字时实时显示这两个数字的和。文章讨论了在实现过程中遇到的问题,即如何正确获取HTMLInputElement对象的值,并提出了解决方案。
在一个页面中,有两个text field。里面可以输入数字,然后在边上显示两个texi field里面数字的和。

我的方法大概是这样的。用两个observe field,来观察text field的值,一旦改变就在页面上取得两个text field的值。加在一起之后update 结果。

用的是rjs.

view的代码是:

<%= text_field 'field', "01",:class=>'sum', :value=>0 %>
<%= text_field 'field', "02",:class=>'sum', :value=>0 %>
<div id='result'>0</div>

<%= observe_field("field_01",
:frequency => 1,
:with => "num_1",
:url => {:action =>'sum'})%>

<%= observe_field("field_02",
:frequency => 1,
:with => "num_2",
:url => {:action =>'sum'})%>


controller 代码为空

def sum
end


rjs 代码


#result = 0
page.select("input.sum").each do |ob|
# result += ob
page.alert(ob) #问题在这里
end
page.replace_html 'result', result


问题是那个在page.select 里面得到 ob 其实是个 HtmlInputElement object.
而且没有其他任何属性,也就是说得不到它的值。
ob.value就会报错。not a valid function.

其实就是计算两个数的加和。应该在用javascript就可以搞定吧。
奇怪的就是感觉用rjs也能行。但是为什么只能得到object却没有任何其他属性呢?
module KanbansHelper def kanban_contextual_menu(&block) menu = KanbanContextualMenu.new( kanban: @kanban, user: @user, view_context: self # 传递当前 Helper 实例作为视图上下文 ) concat(menu.display(capture(menu, &block))) end def name_to_css(name) name.gsub(' ','-').downcase end def jquery_dialog_div(title=:field_issue) "<div id='dialog-window' title='#{ l(title) }'></div>" end def render_pane_to_js(pane, user=nil) if Kanban.valid_panes.include?(pane) return render_to_string(:partial => pane, :locals => {:user => user }) else '' end end # Returns the CSS class for jQuery to hook into. Current users are # allowed to Drag and Drop items into their own list, but not other # people's lists def allowed_to_assign_staffed_issue_to(user) if allowed_to_manage? || User.current == user 'allowed' else '' end end def over_pane_limit?(limit, counter) if !counter.nil? && !limit.nil? && counter.to_i >= limit.to_i # 0 based counter return 'over-limit' else return '' end end # Was the last journal with a note created by someone other than the # assigned to user? def updated_note_on_issue?(issue) if issue && issue.journals.present? last_journal_with_note = issue.journals.select {|journal| journal.notes.present?}.last if last_journal_with_note && issue.assigned_to_id != last_journal_with_note.user_id last_journal_with_note else return false end end end def issue_updated_note_icon(issue) if last_journal = updated_note_on_issue?(issue) image_tag('comment.png', :class => "updated-note issue-show-popup issue-id-#{h(issue.id)}", :id => "issue-#{h(issue.id)}", :alt => l(:kanban_text_updated_issue), :title => h(last_journal.notes)) end end def kanban_issue_css_classes(issue) css = 'kanban-issue ' + issue.css_classes if User.current.logged? && !issue.assigned_to_id.nil? && issue.assigned_to_id != User.current.id css << ' assigned-to-other' end css << ' issue-behind-schedule' if issue.behind_schedule? css << ' issue-overdue' if issue.overdue? css << ' parent-issue' if issue.root? && issue.children.count > 0 css end def issue_icon_link(issue) if Setting.gravatar_enabled? && issue.assigned_to img = avatar(issue.assigned_to, { :class => 'gravatar icon-gravatar', :size => 10, :title => l(:field_assigned_to) + ": " + issue.assigned_to.name }) link_to(img, :controller => 'issues', :action => 'show', :id => issue) else link_to(image_tag('ticket.png', :style => 'float:left;'), :controller => 'issues', :action => 'show', :id => issue) end end def column_configured?(column) case column when :incoming KanbanPane::IncomingPane.configured? when :backlog KanbanPane::BacklogPane.configured? when :selected KanbanPane::QuickPane.configured? || KanbanPane::SelectedPane.configured? when :staffed true # always end end # Calculates the width of the column. Max of 96 since they need # some extra for the borders. def column_width(column) # weights of the columns column_ratios = { :incoming => 1, :backlog => 1, :selected => 1, :staffed => 4 } return 0.0 if column == :incoming && !column_configured?(:incoming) return 0.0 if column == :backlog && !column_configured?(:backlog) return 0.0 if column == :selected && !column_configured?(:selected) visible = 0 visible += column_ratios[:incoming] if column_configured?(:incoming) visible += column_ratios[:backlog] if column_configured?(:backlog) visible += column_ratios[:selected] if column_configured?(:selected) visible += column_ratios[:staffed] if column_configured?(:staffed) return ((column_ratios[column].to_f / visible) * 96).round(2) end def my_kanban_column_width(column) column_ratios = { :project => 1, :testing => 1, :active => 1, :selected => 1, :backlog => 1 } # Vertical column if column == :incoming return (KanbanPane::IncomingPane.configured? ? 100.0 : 0.0) end # Inside of Project, max width if column == :finished || column == :canceled return 100.0 end return 0.0 if column == :active && !KanbanPane::ActivePane.configured? return 0.0 if column == :testing && !KanbanPane::TestingPane.configured? return 0.0 if column == :selected && !KanbanPane::SelectedPane.configured? return 0.0 if column == :backlog && !KanbanPane::BacklogPane.configured? visible = 0 visible += column_ratios[:project] visible += column_ratios[:active] if KanbanPane::ActivePane.configured? visible += column_ratios[:testing] if KanbanPane::TestingPane.configured? visible += column_ratios[:selected] if KanbanPane::SelectedPane.configured? visible += column_ratios[:backlog] if KanbanPane::BacklogPane.configured? return ((column_ratios[column].to_f / visible) * 96).round(2) end # Calculates the width of the column. Max of 96 since they need # some extra for the borders. def staffed_column_width(column) # weights of the columns column_ratios = { :user => 1, :active => 2, :testing => 2, :finished => 2, :canceled => 2 } return 0.0 if column == :active && !KanbanPane::ActivePane.configured? return 0.0 if column == :testing && !KanbanPane::TestingPane.configured? return 0.0 if column == :finished && !KanbanPane::FinishedPane.configured? return 0.0 if column == :canceled && !KanbanPane::CanceledPane.configured? visible = 0 visible += column_ratios[:user] visible += column_ratios[:active] if KanbanPane::ActivePane.configured? visible += column_ratios[:testing] if KanbanPane::TestingPane.configured? visible += column_ratios[:finished] if KanbanPane::FinishedPane.configured? visible += column_ratios[:canceled] if KanbanPane::CanceledPane.configured? return ((column_ratios[column].to_f / visible) * 96).round(2) end def issue_url(issue) url_for(:controller => 'issues', :action => 'show', :id => issue) end def showing_current_user_kanban? @user == User.current end # Renders the title for the "Incoming" project. It can be linked as: # * New Issue jQuery dialog (user has permission to add issues) # * Link to the url configured in the plugin (plugin is configured with a url) # * No link at all def incoming_title if Setting.plugin_redmine_kanban['panes'].present? && Setting.plugin_redmine_kanban['panes']['incoming'].present? && Setting.plugin_redmine_kanban['panes']['incoming']['url'].present? href_url = Setting.plugin_redmine_kanban['panes']['incoming']['url'] incoming_project = extract_project_from_url(href_url) link_name = incoming_project.present? ? incoming_project.name : l(:kanban_text_incoming) else href_url = '' link_name = l(:kanban_text_incoming) end if User.current.allowed_to?(:add_issues, nil, :global => true) link_to(link_name, href_url, :class => 'new-issue-dialog') elsif href_url.present? link_to(link_name, href_url) else link_name end end # Given a url, extract the project record from it. # Will return nil if the url isn't a link to a project or the url can't be # recognized def extract_project_from_url(url) project = nil link_path = url. sub(request.host, ''). # Remove host sub(/https?:\/\//,''). # Protocol sub(/\?.*/,'') # Query string begin route = ActionController::Routing::Routes.recognize_path(link_path, :method => :get) if route[:controller] == 'projects' && route[:id] project = Project.find(route[:id]) end rescue ActionController::RoutingError # Parse failed, not a route end end def export_i18n_for_javascript strings = { 'kanban_text_error_saving_issue' => l(:kanban_text_error_saving_issue), 'kanban_text_issue_created_reload_to_see' => l(:kanban_text_issue_created_reload_to_see), 'kanban_text_issue_updated_reload_to_see' => l(:kanban_text_issue_updated_reload_to_see), 'kanban_text_notice_reload' => l(:kanban_text_notice_reload), 'kanban_text_watch_and_cancel_hint' => l(:kanban_text_watch_and_cancel_hint), 'kanban_text_issue_watched_reload_to_see' => l(:kanban_text_issue_watched_reload_to_see) } javascript_tag("var i18n = #{strings.to_json}") end def viewed_user return @user if @user.present? return User.current end def use_simple_issue_popup_form? # TODO: Hate how Settings is stored... @settings['simple_issue_popup_form'] && ( @settings['simple_issue_popup_form'] == '1' || @settings['simple_issue_popup_form'] == 1 || @settings['simple_issue_popup_form'] == true ) end # Load remote RJS/HTML data from url into dom_id def kanban_remote_data(url, dom_id) javascript_tag("Kanban.remoteData('#{url}', '#{dom_id}');") + content_tag(:span, l(:label_loading), :class => 'loading') end # Returns a list of pane names in the configured order. # # @param hash options Method options # @option options Array :only Filter the panes to only include these ones def ordered_panes(options={}) only = options[:only] || [] if only.present? KanbanPane.pane_order.select {|pane| only.include?(pane) } else KanbanPane.pane_order end end class UserKanbanDivHelper < BlockHelpers::Base include ERB::Util def self.parent superclass end def initialize(options={}) @column = options[:column] @user = options[:user] @project_id = options[:project_id] end def issues(issues) if issues.compact.empty? || issues.flatten.compact.empty? render :partial => 'kanbans/empty_issue' else render(:partial => 'kanbans/issue', :collection => issues.flatten, :locals => { :limit => Setting['plugin_redmine_kanban']["panes"][@column.to_s]["limit"].to_i }) end end def display(body) content_tag(:div, content_tag(:ol, body, :id => "#{@column}-issues-user-#{h(@user.id)}-project-#{h(@project_id)}", :class => "#{@column}-issues"), :id => "#{@column}-#{h(@user.id)}-project-#{h(@project_id)}", :class => "pane equal-column #{@column} user-#{h(@user.id)}", :style => "width: #{ helper.my_kanban_column_width(@column)}%") end end class KanbanContextualMenu < BlockHelpers::Base include ActionView::Helpers # 引入 Rails 视图方法 delegate :link_to, :content_tag, to: :@view_context def initialize(options={}) @view_context = options.delete(:view_context) || ActionController::Base.helpers super(options) @kanban = options[:kanban] @user = options[:user] end def color_help link_to_function(l(:kanban_text_color_help), "$('color-help').toggle();", :class => 'icon icon-info') end def kanban_board if User.current.allowed_to?(:view_kanban, nil, :global => true) link_to(l(:text_kanban_board), kanban_url, :class => 'icon icon-stats') end end def my_kanban_requests link_to(l(:text_my_kanban_requests_title), kanban_user_kanban_path(:id => User.current.id), :class => 'icon icon-user') end def assigned_requests link_to(l(:text_assigned_kanban_title), kanban_assigned_kanban_path(:id => User.current.id), :class => 'icon icon-user') end def new_issue if User.current.allowed_to?(:add_issues, nil, :global => true) if Setting.plugin_redmine_kanban['panes'].present? && Setting.plugin_redmine_kanban['panes']['incoming'].present? && Setting.plugin_redmine_kanban['panes']['incoming']['url'].present? incoming_url = Setting.plugin_redmine_kanban['panes']['incoming']['url'] incoming_project = extract_project_from_url(incoming_url) link_name = incoming_project.present? ? incoming_project.name : l(:label_issue_new) else link_name = l(:label_issue_new) end link_to_function(link_name, "void(0)", :class => 'new-issue-dialog icon icon-issue') end end def sync_kanban if User.current.allowed_to?(:edit_kanban, nil, :global => true) link_to(l(:kanban_text_sync), sync_kanban_url, :method => :put, :class => 'icon icon-reload') end end # @param [Hash] options # @option options [String] :url URL that the user switch form should post to # @option options [String] :label Text to use for the Switch User label (i18n'd already) # @option options [String] :users Users to allow switching to. Defaults to all active Users def user_switch(options={}) url = options[:url] label = options[:label] || l(:label_user_switch) users = options[:users] || User.active if kanban_settings["management_group"] && User.current.group_ids.include?(kanban_settings["management_group"].to_i) render :partial => 'kanbans/user_switch', :locals => {:url => url, :label => label, :users => users.sort} end end def display(body) content = call_hook(:view_user_kanbans_show_contextual_top, :user => @user, :kanban => @kanban).to_s content += body content += call_hook(:view_user_kanbans_show_contextual_bottom, :user => @user, :kanban => @kanban).to_s content_tag(:div, content, :class => "contextual") end end end 报错如下 ActionView::Template::Error (undefined method `kanban_contextual_menu' for #<ActionView::Base:0x0002346c0cc248>):
09-11
根据原作 https://pan.quark.cn/s/459657bcfd45 的源码改编 Classic-ML-Methods-Algo 引言 建立这个项目,是为了梳理和总结传统机器学习(Machine Learning)方法(methods)或者算法(algo),和各位同仁相互学习交流. 现在的深度学习本质上来自于传统的神经网络模型,很大程度上是传统机器学习的延续,同时也在不少时候需要结合传统方法来实现. 任何机器学习方法基本的流程结构都是通用的;使用的评价方法也基本通用;使用的一些数学知识也是通用的. 本文在梳理传统机器学习方法算法的同时也会顺便补充这些流程,数学上的知识以供参考. 机器学习 机器学习是人工智能(Artificial Intelligence)的一个分支,也是实现人工智能最重要的手段.区别于传统的基于规则(rule-based)的算法,机器学习可以从数据中获取知识,从而实现规定的任务[Ian Goodfellow and Yoshua Bengio and Aaron Courville的Deep Learning].这些知识可以分为四种: 总结(summarization) 预测(prediction) 估计(estimation) 假想验证(hypothesis testing) 机器学习主要关心的是预测[Varian在Big Data : New Tricks for Econometrics],预测的可以是连续性的输出变量,分类,聚类或者物品之间的有趣关联. 机器学习分类 根据数据配置(setting,是否有标签,可以是连续的也可以是离散的)和任务目标,我们可以将机器学习方法分为四种: 无监督(unsupervised) 训练数据没有给定...
本系统采用微信小程序作为前端交互界面,结合Spring Boot与Vue.js框架实现后端服务及管理后台的构建,形成一套完整的电子商务解决方案。该系统架构支持单一商户独立运营,亦兼容多商户入驻的平台模式,具备高度的灵活性与扩展性。 在技术实现上,后端以Java语言为核心,依托Spring Boot框架提供稳定的业务逻辑处理与数据接口服务;管理后台采用Vue.js进行开发,实现了直观高效的操作界面;前端微信小程序则为用户提供了便捷的移动端购物体验。整套系统各模块间紧密协作,功能链路完整闭环,已通过严格测试与优化,符合商业应用的标准要求。 系统设计注重业务场景的全面覆盖,不仅包含商品展示、交易流程、订单处理等核心电商功能,还集成了会员管理、营销工具、数据统计等辅助模块,能够满足不同规模商户的日常运营需求。其多店铺支持机制允许平台方对入驻商户进行统一管理,同时保障各店铺在品牌展示、商品销售及客户服务方面的独立运作空间。 该解决方案强调代码结构的规范性与可维护性,遵循企业级开发标准,确保了系统的长期稳定运行与后续功能迭代的可行性。整体而言,这是一套技术选型成熟、架构清晰、功能完备且可直接投入商用的电商平台系统。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值