我正在使用Simple Forms(w/Rails 3.2),并希望创建一个带有select的表单.
例如:
Email Format :Text and HTML
:Text Only
:HTML Only
Run Code Online (Sandbox Code Playgroud)
我不想为这样一个简单的选择创建一个具有关联的模型.如何在没有模型的情况下将其添加到表单中?
我不明白为什么会这样。我有以下迁移:
def self.up
create_table :leakages do |t|
t.integer :feature_id
t.integer :project_id
t.float :total
t.date :apt_date
end
add_index :leakages, [:feature_id, :apt_date]
end
Run Code Online (Sandbox Code Playgroud)
当我第一次运行它时它运行正常,但是当我再次运行迁移时会抛出一个错误,说leakages表已经存在。为什么会发生此错误?我正在使用 mysql2 gem。
我有这个代码:
if @locale
HighVoltage.content_path = "#{@template_to_use}/pages/#{@locale}/"
else
HighVoltage.content_path = 'pages/'
end
Run Code Online (Sandbox Code Playgroud)
现在我有这个版本:
HighVoltage.content_path = @locale ?
"#{@template_to_use}/pages/#{@locale}/" :
'pages/'
Run Code Online (Sandbox Code Playgroud)
编写这段代码的建议方法是什么,第一版或第二版,或者可能是其他版本
User.rb
has_many :votes
has_many :photos
Photo.rb
has_many :votes
belongs_to :user
Vote.rb
belongs_to :user
belongs_to :photo
Run Code Online (Sandbox Code Playgroud)
我想列出投票的用户.但是此列表必须包含有关用户投票的次数的信息.例如,当我去url photos/5/statistics必须有如下列表:
1) User1 voted 30 times
2) User432 voted 15 times
3) User43 voted 12 times
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
Rails给我的网址有错误的ID.它将父ID传递给子,将子ID传递给父.
在我的routes.rb我有
resources :calendars do
resources :events
end
Run Code Online (Sandbox Code Playgroud)
这rake routes告诉我正在建立良好的路线,例如
calendar_event GET /calendars/:calendar_id/events/:id(.:format) {:controller=>"events", :action=>"show"}
Run Code Online (Sandbox Code Playgroud)
所以当我从我的视角中询问该路线的路径时,
<%= link_to "#{event.name} @ #{event.calendar.name} #{event.calendar.year}", calendar_event_path(event) %>
Run Code Online (Sandbox Code Playgroud)
它给了我一个ids反转的网址...
http://localhost:3000/calendars/<-eventid->/events/<-calendarid->
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激!!!
编辑:在我的模型中,事件belongs_to :calendar和日历has_many :events.我也使用Mongoid作为我的ORM,而不是ActiveRecord,但我希望这不是问题.
def checkdomains
@domains = Domain.all
#@domains.where(:confirmed => "yes").each do |f|
@domains.each do |f|
r = Whois.whois(f.domain)
if r.available? == true
EmailNotify.notify_email(f).deliver
end
end
end
Run Code Online (Sandbox Code Playgroud)
当它出现一个无效的url(whois gem发出错误)时,此方法崩溃,并且不会继续检查其余的域.有什么方法可以让它继续检查其余的域,即使它崩溃了吗?至少在我可以解决每个域的搜索问题之前.
我知道该方法Element#wait_until_present(t),但是如果此方法超时,则会引发timeOut异常。
有没有一种方法可以等待t几秒钟,然后在元素存在时返回true,否则返回false?
我知道可以通过一个简单的begin..rescue..end语句来完成,但是我正在寻找不使用异常的东西。
如果用户在浏览器中启用了javascript,我该如何检查我的布局代码?如果未启用javascript,我想显示错误页面.例如404或500错误页面.我怎么能用Rails做到这一点?
在我的Rails应用程序,我想记录的时间user是last_seen.
现在,我在下面这样做SessionsHelper:
def sign_in(user)
.....
user.update_column(:last_seen, Time.zone.now)
self.current_user = user
end
Run Code Online (Sandbox Code Playgroud)
但这不是很精确,因为用户可能会在上午8点登录,而在晚上,last_seen数据库列仍将包含该时间.
所以我想last_seen在用户采取行动时更新:
class ApplicationController
before_filter :update_last_seen
private
def update_last_seen
current_user.last_seen = Time.zone.now
current_user.save
end
end
Run Code Online (Sandbox Code Playgroud)
但我不喜欢这种方法,因为数据库会因用户采取的每个操作而受到攻击.
那么什么可能是一个更好的替代品呢?