Tag: 的Ruby on 轨道

如何更新某些属性时更新时间戳?

我需要一个仅在更新某些字段时更新的时间戳列,例如,如果产品名称发生更改,则应使用Time.now更新attributes_updated_at字段。 我不希望在添加外键关联的情况下更新字段,或者如果product.status从’pending’更改为’active’ 。 有没有办法在模型中简单地定义它?

使用虚拟属性干掉此模型

在我的表单中,我有一个虚拟属性,允许我接受混合数字(例如38 1/2)并将它们转换为小数。 我也有一些validation(我不确定我正在处理这个问题),如果出现爆炸,会抛出错误。 class Client < ActiveRecord::Base attr_accessible :mixed_chest attr_writer :mixed_chest before_save :save_mixed_chest validate :check_mixed_chest def mixed_chest @mixed_chest || chest end def save_mixed_chest if @mixed_chest.present? self.chest = mixed_to_decimal(@mixed_chest) else self.chest = "" end end def check_mixed_chest if @mixed_chest.present? && mixed_to_decimal(@mixed_chest).nil? errors.add :mixed_chest, "Invalid format. Try 38.5 or 38 1/2" end rescue ArgumentError errors.add :mixed_chest, "Invalid […]

Rails:未初始化的常量User :: UserAlbum?

嗨我目前正在尝试构建一个附有许多用户的相册,我正在尝试修复它们之间的关系,以便它是多对多的(一个用户+多个专辑,一个专辑+多个用户)。 但是,在我触摸模型和迁移后,当我尝试加载show.html.erb时,我收到错误。 请帮忙!! 错误: uninitialized constant User::UserAlbum show.html.erb You currently have hey boo 专辑模型 class Album :user_albums has_many :photos end 用户模型 class User :create validates_format_of :name, :with => /[A-Za-z]+/, :on => :create validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[az]{2,})\Z/i, :on => :create validates_length_of :password, :minimum => 5, :on => :create has_many :user_albums has_many :albums, :through => :user_albums […]

Rails:ActiveRecord :: RecordNotFound – 找不到没有ID的用户

我目前正在开发一个上传图片的项目。 这些相册中有用户,相册和图片。 我已经达到了以用户身份创建专辑的程度(尚未完成会话,身份validation或登录,但已完成注册)。 我在表单提交Rails后收到错误 Couldn’t find User without an ID 我注意到我的new.html.erb的url没问题: http://localhost:3000/users/13/albums/new (没问题) 但在我提交之后,它给了我一个错误,页面是: http://localhost:3000/albums/58 (问题。url中没有user_id) 有谁知道为什么我的路线突然改变了,以及如何解决它? 错误发生在我的app / controllers / albums_controller.rb:14:在`show’中@user = User.find(params [:user_id])行。 new.html.erb { :id => “uploadform”, :multipart => true } do |f| %> albums_controller def show @user = User.find(params[:user_id]) @album = @user.albums.find(params[:id]) @photo = @album.photos.build(params[:photo]) respond_to do |format| if @user.save format.html […]

date_select的多参数属性错误

我在应用程序中添加了这一段编码,现在它不允许任何人注册。 我收到以下错误:1个错误分配多参数属性。 错误页面的标题是“UsersController #create中的ActiveRecord :: MultiparameterAssignmentErrors”。 我不知道如何解决,因为其他post不幸没有帮助。 有人有一些解决方案? new.html.erb: 1995, :end_year => 1930 %> users_controller: def create @user = User.new(params[:user]) if @user.save UserMailer.registration_confirmation(@user).deliver session[:user_id] = @user.id redirect_to root_url, notice: “Thank you for signing up!” else render “new” end end development.log: Started POST “/users” for 127.0.0.1 at 2013-03-22 10:27:31 -0400 Processing by UsersController#create as HTML Parameters: […]

在视图中获取输入并将其传递给控制器

以下代码甚至不渲染按钮: :create_new, :action=>:input do %> 控制器create_new具有以下方法: def input @my_input=params[:my_input] end routes.rb有:: resources :create_new do post :input, :on=>:collection

安装rails的问题

大家好我在安装rails时遇到问题:我已写在终端 gem安装导轨 Successfully installed rails-3.0.3 1 gem installed Installing ri documentation for rails-3.0.3… File not found: lib 接下来需要做什么以及如何使用gem安装rails。

在铁轨中路由#TOKEN而不是Post#ID

我在我的rails应用程序中走了很长一段路,并完成了95%。 我需要做的最后一件事是通过生成我发送给特定用户的令牌来限​​制某些用户的某些post。 我知道还有其他限制访问的方法(没有太详细)但是对于这个应用程序,它必须使用令牌。 如何让我的路由设置看起来像这个post#token_string而不是这个post#id post#token_string ? 更多信息:我的post控制器中的“私有”方法已经有restrict_access 。 我的post模型has_many link_tokens和我的link_tokens模型belongs_to post因为一个post会有很多link_tokens 任何灯棚都会受到赞赏。 提前谢谢(我迫不及待地想把这个应用程序包起来) 现在,我喜欢这个link_to post.name, post_url + “?token=” + @token.token结果http://localhost:3000/post/1?token=25b8af 使用find_by_token时出错信息( http://localhost:3000/post/25b8af ) def show **@post = LinkToken.find_by_token(params[:token]).post** @posts = Post.order(“created_at DESC”) end

Rails在一些标签助手中传递额外的参数

目前image_tag(“file.jpg”)产生正常的图像html标签,BUT src =“file.jpg **?7485793246 **”无论如何这些数字是什么以及如何禁用它们?

Rails身份validation问题

我对rails比较陌生,现在我正在开发一个简单的登录注销系统。 在我的应用程序中,当我登录时生成的URL是: 本地主机:3000 /用户/索引/ 7 当我退出时,我会回到根目录。 但是,如果复制此URL并将其粘贴到另一个浏览器窗口中,我会立即登录而不会被定向到登录表单。 如何纠正这个问题。 我尝试将用户ID存储在会话哈希中,然后在注销时我将会话中的用户ID设置为nil。 但这不起作用。 需要帮助。 编辑: 在我的家庭控制器中 class HomeController [“user_login=? AND user_password=?”,params[:user] [:username],params[:user][:password]); if user!=nil session[:user_id]=user.user_id; redirect_to({:controller=>’user’}) end end end 在用户控制器中我有一个注销方法: def logout session[:user_id]=nil; redirect_to({:controller=>’home’}); end 我的routes.rb文件如下所示: ActionController::Routing::Routes.draw do |map| map.root :controller => “home”,:action => “index” map.connect ‘:controller/:action/:id’ map.connect ‘:controller/:action/:id.:format’ end 编辑: 我已经解决了这个问题,我没有在用户控制器索引方法中检查会话哈希中的id值。 但是我有另一个问题如果我在rails 2.3.17中有一个应用程序并且我想将它转移到最新版本我将需要做多少更改