Tag: 帮手

表单中的Rails选择标记不会在编辑视图中填充值

我有一个由成员模型的脚手架生成的典型rails _form。 对于参数:状态我正在使用选择标记: true, :prompt => ‘State’ }, { :class => ‘form-control’ } %> 使用辅助方法(我们陈述): def us_states [ [‘AK’, ‘AK’], [‘AL’, ‘AL’], #etc 对于参数“member_since”,我使用的是select_year帮助器: 2013, :end_year => 1920, :field_name => ‘member_since’, :prompt => ‘Choose year’, prefix: :member}, {:class => “form-control”} ) %> 现在,这两个选项都可以创建新记录,但在编辑记录视图中都没有预填充任何字段。 有什么想法吗?

从User.rb模型访问ApplicationHelper

这里有一些不起作用的简单代码: module ApplicationHelper def industries industries = [‘Agriculture’,’Food’, etc.] end end class User ApplicationHelper.industries … end 使用undefined method ‘industries’运行用户操作时,此代码失败。 你知道我怎么能以正确的方式引用帮助者(或者最好只是行业)吗? 编辑 根据Mauricio在下面的答案中的代码,如何在控制器和视图中访问INDUSTRIES ? 我无法在任何地方访问行业,但我确实需要为我的应用程序提供服务。

Capybara 2.0和rspec-rails – 帮助程序在规范/function中不起作用

我正在尝试使用辅助模块中的方法,但rspec似乎没有识别spec/features下的测试帮助程序。 请注意,对spec_helper.rb的唯一更改是添加spec_helper.rb require ‘capybara/rspec’ 。 我尝试将helper.rb移动到spec/support , spec/helpers和spec/features (包含我的测试的目录),但没有运气。 测试表明辅助方法未定义。 让它“工作”的唯一方法是将我的测试移动到另一个目录,例如spec/integration 。 但现在水豚将无法工作( visit undefined ),因为它不符合spec/features 。 这是我的帮助模块( authentication_helper.rb ): module AuthenticationHelper def sign_in_as!(user) visit ‘/users/sign_in’ fill_in “Email”, with: user.email fill_in “Password”, with: “password” click_button “Sign in” page.should have_content(“Signed in successfully.”) end end RSpec.configure do |c| c.include AuthenticationHelper, type: :request end

在Rails表单中使用textarea助手

为什么此代码在文本区域显示错误? {:action => ‘create’}) do |f| %> “50×10”) %>

自定义表单助手

有没有办法可以创建自定义表单助手,而不是: special_field_tag :object, :method 我可以实现以下目标: form.special_field :method