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
【电力系统】单机无穷大电力系统短路故障暂态稳定Simulink仿真(带说明文档)内容概要:本文档围绕“单机无穷大电力系统短路故障暂态稳定Simulink仿真”展开,提供了完整的仿真模型与说明文档,重点研究电力系统在发生短路故障后的暂态稳定性问题。通过Simulink搭建单机无穷大系统模型,模拟不同类型的短路故障(如三相短路),分析系统在故障期间及切除后的动态响应,包括发电机转子角度、转速、电压和功率等关键参数的变化,进而评估系统的暂态稳定能力。该仿真有助于理解电力系统稳定性机理,掌握暂态过程分析方法。; 适合人群:电气工程及相关专业的本科生、研究生,以及从事电力系统分析、运行与控制工作的科研人员和工程师。; 使用场景及目标:①学习电力系统暂态稳定的基本概念与分析方法;②掌握利用Simulink进行电力系统建模与仿真的技能;③研究短路故障对系统稳定性的影响及提高稳定性的措施(如故障清除时间优化);④辅助课程设计、毕业设计或科研项目中的系统仿真验证。; 阅读建议:建议结合电力系统稳定性理论知识进行学习,先理解仿真模型各模块的功能与参数设置,再运行仿真并仔细分析输出结果,尝试改变故障类型或系统参数以观察其对稳定性的影响,从而深化对暂态稳定问题的理解。
本研究聚焦于运用MATLAB平台,将支持向量机(SVM)应用于数据预测任务,并引入粒子群优化(PSO)算法对模型的关键参数进行自动调优。该研究属于机器学习领域的典型实践,其核心在于利用SVM构建分类模型,同时借助PSO的全局搜索能力,高效确定SVM的最优超参数配置,从而显著增强模型的整体预测效能。 支持向量机作为一种经典的监督学习方法,其基本原理是通过在高维特征空间中构造一个具有最大间隔的决策边界,以实现对样本数据的分类或回归分析。该算法擅长处理小规模样本集、非线性关系以及高维度特征识别问题,其有效性源于通过核函数将原始数据映射至更高维的空间,使得原本复杂的分类问题变得线性可分。 粒子群优化算法是一种模拟鸟群社会行为的群体智能优化技术。在该算法框架下,每个潜在解被视作一个“粒子”,粒子群在解空间中协同搜索,通过不断迭代更新自身速度与位置,并参考个体历史最优解和群体全局最优解的信息,逐步逼近问题的最优解。在本应用中,PSO被专门用于搜寻SVM中影响模型性能的两个关键参数——正则化参数C与核函数参数γ的最优组合。 项目所提供的实现代码涵盖了从数据加载、预处理(如标准化处理)、基础SVM模型构建到PSO优化流程的完整步骤。优化过程会针对不同的核函数(例如线性核、多项式核及径向基函数核等)进行参数寻优,并系统评估优化前后模型性能的差异。性能对比通常基于准确率、精确率、召回率及F1分数等多项分类指标展开,从而定量验证PSO算法在提升SVM模型分类能力方面的实际效果。 本研究通过一个具体的MATLAB实现案例,旨在演示如何将全局优化算法与机器学习模型相结合,以解决模型参数选择这一关键问题。通过此实践,研究者不仅能够深入理解SVM的工作原理,还能掌握利用智能优化技术提升模型泛化性能的有效方法,这对于机器学习在实际问题中的应用具有重要的参考价值。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值