我在我的Rails 3.0.9应用程序中使用Devise进行用户身份验证.由于我希望能够管理用户,因此我创建了以下用户控制器:
class UsersController < ApplicationController
def index
@users = User.all
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = "Successfully created User."
redirect_to users_path
else
render :action => 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
params[:user].delete(:password) if params[:user][:password].blank?
params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank?
if @user.update_attributes(params[:user])
if current_user.update_with_password(params[:user])
sign_in(current_user, :bypass => true)
end
flash[:notice] = "Successfully updated User."
redirect_to users_path
else
render :action => 'edit'
end …Run Code Online (Sandbox Code Playgroud) 以这个答案为指导,我select_year开始创建一个以今天开始并在100年前结束的开始.但我想添加一个:prompt以便下拉菜单以"年份"而不是当前年份开始.所以我使用了以下内容:
<%= select_year(Date.today, {:prompt => "Year", :start_year => DateTime.now.year, :end_year => DateTime.now.year - 115}, {:field_name => 'Year', :id => 'Date.year'}) %>
Run Code Online (Sandbox Code Playgroud)
这会导致一年下降,但提示显示当前年份而不是"年份".如果我点击下拉菜单,它会显示"年份"作为选择当前年份的第一个选项.我怎样才能解决这个问题?我做错了什么?
更新:以下是上述代码的HTML输出:
<select field_name="Year" id="Date.year" name="date[year]">
<option value="">Year</option>
<option selected="selected" value="2011">2011</option>
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做2011年不是自动的"selected"?
在我的应用程序的用户注册活动,这属于一个流.该登记在托管登记模型,其中有一个叫布尔域" 出席 ".
我正在尝试生成排行榜并需要知道:每个用户的注册总数,以及每个单独事件流中用户注册的计数.
我正在尝试这个(在User.rb中):
# returns an array of users and their attendence count
def self.attendance_counts
User.all(
:select => "users.*, sum(attended) as attendance_count",
:joins => 'left join `registrations` ON registrations.user_id = users.id',
:group => 'registrations.user_id',
:order => 'attendance_count DESC'
)
end
Run Code Online (Sandbox Code Playgroud)
当我在数据库中运行时,生成的SQL仅用于返回每个用户的总有人值,但返回的所有内容都是Rails中的用户记录.
我即将放弃并将每个流的counter_cache硬编码(它们相当固定)到User表中,只要注册模型保存的有人参与属性发生变化,就会手动更新.
不过,我真的很好奇如何执行这样的查询.在计算有关系的记录的统计数据和报告时,它必须始终出现.
非常感谢您的时间和考虑.提前致谢.
我知道这似乎是一个非常简单的错误,但它来自一个复杂的过程.
我试图将旧的rails 2应用程序升级到rails 3.在我的routes.rb文件中,我有
root :to => "home#index"
Run Code Online (Sandbox Code Playgroud)
我还有一个文件'app/controllers/home_controller.rb'和'app/views/home/index.html.erb',所以我根本得不到可能导致此错误的原因.升级到rails 3并不容易.
(在home_controller.rb中,我有def index
end)
有什么建议?
**更新 - 完整的路线文件**
SpecimenTracker::Application.routes do
map.resources :users
map.resource :session
# The priority is based upon order of creation: first created -> highest priority.
# Sample of regular route:
# map.connect 'products/:id', :controller => 'catalog', :action => 'view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# map.purchase 'products/:id/purchase', :controller => 'catalog', …Run Code Online (Sandbox Code Playgroud) vendor/assets/是空的.似乎无法在任何地方找到编译的文件.
编辑:不幸的是他们不在公共/资产
问题很简单,update_attributes是否验证了模型的每个可能的验证,即使我不想更新某些属性?
我有一个编辑视图,用户可能会在其中更改密码,但只有在他通过密码时才会更改密码,即如果密码不会更改.
所以我在控制器中执行以下操作:
def update
params[resource_name].delete(:password) if params[resource_name][:password].blank?
params[resource_name].delete(:password_confirmation) if params[resource_name][:password_confirmation].blank?
params[resource_name].delete(:current_password) if params[resource_name][:current_password].blank?
if resource.update_attributes(params[resource_name])
...
end
end
Run Code Online (Sandbox Code Playgroud)
我在模型上定义了以下验证:
validates :password,
:length => { :within => 6..40 }
Run Code Online (Sandbox Code Playgroud)
因此,每当我使用调用更新时,我都会收到一条错误消息,指出密码太短
Ps.:我正在使用Devise来解决这个问题.
编辑:你们有没有人知道如果Devise已经对密码进行了任何验证?因为,我删除了验证,它以正确的方式工作,但如果我输入一个简短的密码,它仍然显示验证,说它太短.
我有一个简单的rails应用程序,就像一个具有start_at字段和end_at字段的事件系统.我希望能够显示start_at当月的月份事件.我为此定义了一个范围,如下所示:
scope :this_months_event, lamda{where("start_at = ?" Time.zone.now.month)}
Run Code Online (Sandbox Code Playgroud)
每当我在控制台窗口中尝试这个时,我得到一个空数组.我观察到该start_at字段的月份未被提取,因此无法找到start_at月份,因此它返回空.
有没有人知道一个很好的方法来做这个或如何通过这个start_at月,以便当我打电话时Event.this_months_event,我得到本月的所有事件,如果可能的话,范围来获取过去的事件和另一个来获取当前事件.
有人能指出我在这个陈述中的错误吗?
2011-08-19T20:16:05+00:00 app[web.1]: ActiveRecord::StatementInvalid (PGError: ERROR: argument of AND must be type boolean, not type integer
2011-08-19T20:16:05+00:00 app[web.1]: : SELECT "app_permissions".* FROM "app_permissions" WHERE (user_id = 1,2 AND is_shared = 't' AND app_id = '1')):
2011-08-19T20:16:05+00:00 app[web.1]:
Run Code Online (Sandbox Code Playgroud)
码:
AppPermission.find(:all, :conditions => ["user_id = ? AND is_shared = ? AND app_id = ?", User.find_all_by_facebook_id(@friends_uids).collect { |itm| itm["id"] }, true, @appl_id])
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在使用paperclip和我的rails 3应用程序.我想附加一个随机字符串,没有什么可以在文件的末尾长或疯狂缓存CDN.有人知道一个真正简单的方法吗?
这是我目前的情况:
has_attached_file :photo,
:styles => { :thumb => "70x70>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:rails_env/public/users/:id/:style/:basename.:extension",
.....
Run Code Online (Sandbox Code Playgroud)
我想要一个像FILENAME_31313.png这样的文件名
每次保存照片时31313是随机的.
谢谢
在一个新项目上工作,继续在rails 3.0上使用ruby或开始使用3.1是否明智?
当我意识到我在当前项目中有多深入时,我才想到这个问题.而不是后来切换我想知道现在进行切换是否合适?或者是否为时尚早?
rubygems ruby-on-rails ruby-on-rails-plugins ruby-on-rails-3 ruby-on-rails-3.1
ruby-on-rails-3 ×10
activerecord ×2
devise ×2
date ×1
paperclip ×1
passwords ×1
ruby ×1
rubygems ×1
sql ×1
validation ×1