我有一个用户和个人资料模型.一个用户可以拥有许多配置文件 在用户创建过程中,我只需要在我的用户模型中访问配置文件部分中的一个信息(即电话号码).因此,我正试图通过它完成它attr_accessible.我的user.rb看起来像这样.
has_many :profiles
attr_accessible :handle, :email, :password, :profile_mobile_number
attr_accessor : :profile_mobile_number
Run Code Online (Sandbox Code Playgroud)
我面临的问题是,当我尝试在user.rb中的方法中调用getter方法profile_mobile_number时(该方法是私有的,虽然我认为无所谓),但我得到一个空值.我在users/new.html.erb表单中使用以下内容
我的问题是这样做的正确方法是什么?我应该使用<% f.fields_for :profile do |ff| -%>或<% f.fields_for :profiles do |ff| -%>(注意第二个是复数).当我使用复数:配置文件时,我甚至看不到表单上的字段.我在这里错过了什么?什么是需要在模型user.rb中使用的时态?:profile_phone_number或:profiles_phone_number?谢谢.
我在ExtJS表单中使用numberField并且只想输入正数,范围为0-99,它应该只接受2个字符(不超过2个).
{
xtype:"textfield",
allowNegative: false,
allowDecimals: false,
minValue: 0,
maxValue: 99,
maxLength: 2
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中给出了一个错误,但它接受了超过2个字符.
我也在下面试过,但问题是一样的:
{
xtype:"textfield",
regex: /^\d{0,2}$/,
regexText: "<b>Error</b></br>Invalid Number entered.",
validator: function(v) {
return /^\d{0,2}$/.test(v)?true:"Invalid Number";
}
}
Run Code Online (Sandbox Code Playgroud)
如何限制输入2个以上的字符?
我想在上传之前显示图像预览,因为我使用下面给出的代码.
它适用于Firefox,但不适用于IE8
<%= image_tag @image, :id=>"preview-photo" %>
<%= file_field 'image','photo', :onchange => "preview(this);" %>
function preview(this) {
document.getElementById("preview-photo").src = this.value;
return;
}
Run Code Online (Sandbox Code Playgroud)
有没有解决方案在IE8和其他浏览器中预览图像?
我用命令创建了一个模型Anonymous
rails g model Anonymous section_id:integer aid:string fake:bool active:bool
Run Code Online (Sandbox Code Playgroud)
但迁移中的表名称称为匿名
class CreateAnonymous < ActiveRecord::Migration
def change
create_table :anonymous do |t|
t.integer :section_id
t.string :aid
t.bool :fake
t.bool :active
t.timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)
我是对的,Anonymous的复数形式也是Anomymous吗?(英语不是我的母语).我如何才能看到Rails为我的模型提供的复数名称?有点像rake routes?
我想知道什么是解决以下问题的正确方法.
我有两个型号:
Publisher and Issue. Publisher has_many issues.
Run Code Online (Sandbox Code Playgroud)
我想管理发布者列表和问题列表中的问题.
例如 :
在发布者列表用户可以单击每个发布者附近的"问题"链接.然后他进入问题列表,但只过滤适当的发布者.他可以点击"创建新问题"并转到表单添加新问题.在此表单上,我不需要向他显示选择发布者的选择列表
在问题列表中,用户可以单击"创建新问题"并转到表单,但这次他应该从select中选择发布者,这与创建的问题有关.
简单地说,我需要单独的问题和出版商问题的crud行动.
首先我尝试制作一个:
resources :issues
resources :publishers do
resources :issues
end
Run Code Online (Sandbox Code Playgroud)
并在问题控制器:
before_filter :find_issue
def find_issue
@publisher = Publisher.find(params[:publisher_id]) if params[:publisher_id]
@issues = @publisher ? @publisher.issues : Issue
end
Run Code Online (Sandbox Code Playgroud)
但我必须在我的观点和控制器中做出很多条件.
例如,如果问题是从发布者创建的,则成功时我想重定向publisher_issues_path而不是issue_path反之亦然.与"返回列表"等类似的所有链接都存在同样的问题.所以代码在我看来不是很透明.
现在我想知道使用命名空间.
namespace :publishers, do
resources :issues
end
Run Code Online (Sandbox Code Playgroud)
和做
# app/controllers/publishers/issues_controller.rb
module Publishers
class IssuesController < ApplicationController
# actions that expect a :publisher_id param
end
end
# app/controllers/issues_controller.rb
class IssuesController < ApplicationController
# your …Run Code Online (Sandbox Code Playgroud) 在Mongoid 3.0.21中,如何将所有模型的属性作为纯Ruby Hash?
呼叫#attributes或#raw_attributes返回Moped::BSON::Document.虽然它实际上延伸Hash,几个哈希方法不能按预期工作.特别是.#except返回未修改self,而不是散列给定键的哈希
更新:Moped::BSON::Document正确继承的行为Hash.我试图用符号命名属性,而不是字符串,这就是为什么#except不起作用.不久:不except('pictures'),不except(:pictures).
在irb我正在尝试Date.today而且它失败了.
我本来想做的:
Date.today.strftime "%b %d, '%y"
Run Code Online (Sandbox Code Playgroud) ruby ×2
extjs ×1
irb ×1
javascript ×1
mongoid ×1
nested-forms ×1
pluralize ×1
regex ×1
validation ×1