标签: update-attributes

有没有办法防止rails中的序列化属性更新,即使没有更改?

这可能是所有新用户迟早会发现有关Rails的事情之一.我刚刚意识到rails正在使用serialize关键字更新所有字段,而不检查内部是否真的发生了任何变化.在某种程度上,这是通用框架的明智之举.

但有没有办法覆盖这种行为?如果我可以跟踪序列化字段中的值是否已更改,是否有办法阻止它在更新语句中被推送?我尝试使用"update_attributes"并将哈希值限制在感兴趣的字段中,但rails仍然更新所有序列化字段.

建议?

serialization ruby-on-rails update-attributes ruby-on-rails-3

10
推荐指数
1
解决办法
2536
查看次数

Rails - update_attributes遇到验证

所以我有一个用户模型,有登录,电子邮件地址,密码,密码确认,姓名,头像(图片)等.前5个有验证,基本上说明所有5个都需要存在才能创建一个新模式.

但是,这会给我带来更新问题.

我有一个编辑页面,用户只能编辑他们的名字和头像.我目前不打算让他们更改他们的登录信息,我希望从其他页面更改电子邮件和密码.

因此编辑表单如下所示:

<% form_for @user, :html => { :multipart => true } do |u| %>
 <p>
  <label>Name:</label>
  <%= u.text_field :name %>
 </p>
 <p>
  <label>Avatar:</label>
  <%= display_user_avatar %>
  <%= u.file_field :avatar%>
 </p>
 <p>
  <%= submit_tag %>
 </p>
<% end %>
Run Code Online (Sandbox Code Playgroud)

如果我尝试做了@user.update_attributes(params[:user]),然后因为只有2个PARAMS是nameavatar,更新失败,因为东西像密码,确认密码,电子邮件等需要验证的条目,它们根本就没有在该形式存在.

我可以通过这样做来解决这个问题@user.update_attribute(:name, params[:user][:name]),但后来我担心避免验证是否是Good Thing™.尤其是关于像密码更新,在这里我需要验证新密码.

还有另外一种方法吗?

如果我这样做简单地使用update_attribute为:name:avatar,我将如何去这样做呢?

这会有用吗?

params[:user].each do |attribute|
  @user.update_attribute(attribute, params[:user][attribute])
end
Run Code Online (Sandbox Code Playgroud)

这是一种可以接受的方式吗?


- 编辑跟进 -
好吧,我按照你的建议做了尝试

  def update
    @user = User.find_by_login(params[:id]) …
Run Code Online (Sandbox Code Playgroud)

validation ruby-on-rails update-attributes

7
推荐指数
1
解决办法
7290
查看次数

Rails - 如何在不使用accepts_nested_attributes_for的情况下管理嵌套属性?

我的问题是我遇到了accepts_nested_attributes_for的限制,所以我需要弄清楚如何自己复制该功能以获得更大的灵活性.(请参阅下文,了解究竟是什么让我失望.)所以我的问题是:如果我想模仿并增加accepts_nested_attributes_for,我的表单,控制器和模型应该是什么样的?真正的诀窍是我需要能够使用现有的关联/属性更新现有的AND新模型.

我正在构建一个使用嵌套表单的应用程序.我最初使用这个RailsCast作为蓝图(利用accepts_nested_attributes_for):Railscast 196:嵌套模型表单.

我的应用程序是带有作业(任务)的清单,我让用户更新清单(名称,描述)并在单个表单中添加/删除关联的作业.这很好用,但是当我将其合并到我的应用程序的另一个方面时,我遇到了问题:历史记录通过版本控制.

我的应用程序的一个重要部分是我需要记录我的模型和关联的历史信息.我最终推出了自己的版本(是我在描述我的决策过程/注意事项的问题),其中很大一部分是我需要创建旧版本的新版本的工作流程,对新版本进行更新,归档旧版本.这对于用户是不可见的,用户将该体验视为仅通过UI更新模型.

代码 - 模型

#checklist.rb
class Checklist < ActiveRecord::Base
  has_many :jobs, :through => :checklists_jobs
  accepts_nested_attributes_for :jobs, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end

#job.rb
class Job < ActiveRecord::Base
  has_many :checklists, :through => :checklists_jobs
end
Run Code Online (Sandbox Code Playgroud)

代码 - 当前表单(注意:@jobs在检查清单控制器编辑操作中被定义为此清单的未归档作业;因此是@checklist)

<%= simple_form_for @checklist, :html => { :class => 'form-inline' } do |f| %>
  <fieldset>
    <legend><%= controller.action_name.capitalize %> Checklist</legend><br>

    <%= f.input :name, :input_html => { :rows => 1 }, :placeholder => …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-forms nested-attributes model-associations update-attributes

6
推荐指数
2
解决办法
7777
查看次数

Rails 4中的未定义方法update_attributes

我现在正在使用Rails 4现在我想更新我的用户记录我正在尝试使用此命令

@user=User.update_attributes(:name=> params[:name], :user=> params[:username], :pass=> params[:password])
OR
@user=User.update_attributes(name: params[:name], user: params[:username], pass: params[:password])
Run Code Online (Sandbox Code Playgroud)

但总是得到错误

undefined method `update_attributes' 
Run Code Online (Sandbox Code Playgroud)

所以我如何更新我的用户我也想问它是否会更新我的数据库中的所有用户?

我想我必须添加一些条件, where id=@user.id但我不知道我怎么能在铁路上做到这一点!

update-attributes

6
推荐指数
2
解决办法
1万
查看次数

Carrierwave文件名在update_attributes上不断变化

我有模型公司和公司已经安装了carrierwave uploader Logo.

class Company < ActiveRecord::Base
  mount_uploader :logo, LogoUploader
Run Code Online (Sandbox Code Playgroud)

图片上传工作,但我有update_attributes的问题.当用户想要仅更新公司的描述或标题,而不是上传新图像时 - DB中的文件名值每次都在更改.这是一个简单的例子:

1.9.3-p545 :004 > a = Company.last
1.9.3-p545 :005 > a.update_attributes(:title => "test title 2")
 (0.4ms)  BEGIN
  Company Exists (0.9ms)  SELECT 1 AS one FROM `companies` WHERE (`companies`.`title` = BINARY 'test title 2' AND `companies`.`id` != 37) LIMIT 1
  Company Load (0.7ms)  SELECT `companies`.* FROM `companies` WHERE `companies`.`id` = 37 LIMIT 1
   (0.7ms)  UPDATE `companies` SET `title` = 'test title 2', `logo` = '1396206630_1f288be4.jpg', `updated_at` = '2014-03-30 19:10:30' …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails update-attributes ruby-on-rails-3 carrierwave

6
推荐指数
1
解决办法
1280
查看次数

如果失败,`update_attribute`会返回什么?

我有以下一段代码

@user = User.find(params[:id])
if (@user.activation_status == "active") 
  #some code here 
  @user.update_attribute('activation_status' ,'inactive')  # Line 44
  #send mail to user that his account is Acivated
else

end
Run Code Online (Sandbox Code Playgroud)

有没有机会Line 44失败?因为数据库内存已满或网络出现故障.在那种情况下会发生什么?如果这会产生问题,那么避免它的更好方法是什么?update_attribute失败后会返回什么?

ruby ruby-on-rails update-attributes update-attribute

5
推荐指数
2
解决办法
5731
查看次数

Rails update_attribute

我有以下问题.我有一个名为user的模型,它有一个名为activated的列.我试图更新该方法激活方法?但它给我错误:验证失败:密码不能为空,密码太短(最少6个字符)这对我没有意义,因为我不接触密码字段!我只想更新激活的列.我把这些代码放在这里我认为它相关,但如果你认为你需要更多只是问:) :)非常感谢你提前!

模型:

attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation, :activated
has_many :sucu_votes

email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

validates :name,  :presence => true,
                                    :length => { :maximum => 50 }

validates :email, :presence => true,
                                    :format => {:with => email_regex},
                                    :uniqueness => { :case_sensitive => false }

validates :password, :presence => true,
                                         :length => { :within => 6..15 },
                                         :confirmation => true

before_save :encrypt_password

def activated?
    self.update_attributes!(:activated => true)
    return self.activated
end
Run Code Online (Sandbox Code Playgroud)

控制器从哪个方法激活?叫做

def activate
if request.get?
        user=User.find_by_id(params[:id])
        if user.activated?
            flash[:notice]="Your account …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails update-attributes ruby-on-rails-3

5
推荐指数
1
解决办法
3167
查看次数

如何更新/重命名载波上传的文件?

我无法弄清楚如何在 rails 3.2.6 中更新/重命名使用 Carrierwave-mongoid 上传/管理的文件。我想重命名数据库和文件系统中的文件。

像这样的东西也许...

def rename( id , new_name )
  f = UploadedFile.find(id)

  if f.update_attributes({ f.file.original_filename: new_name })  # this is WRONG, what is right???
    new_path = File.join( File.dirname( f.file.current_path ) , new_name ))
    FileUtils.mv( f.file.current_path , new_path )
  end

  return f
end
Run Code Online (Sandbox Code Playgroud)

让我补充一下这是在它已经上传之后。

rename ruby-on-rails file update-attributes carrierwave

5
推荐指数
2
解决办法
7282
查看次数

Rspec any_instance should_receive应该收到以下消息,但没有

我在与其中any_instance.should_receive抛出一个错误RSpec的支架试验的问题:故障/错误:无法找到回溯整整一个实例匹配行应该已收到以下消息(S),但没有:update_attributes方法

Rspec代码,使用FactoryGirl和strong_parameters:

describe "PUT update" do
   describe "with valid params" do
     it "updates the requested acquisition" do
       puts "starting updates the requested acquisition"
       acquisition = create(:acquisition)
       # Assuming there are no other acquisitions in the database, this
       # specifies that the Acquisition created on the previous line
       # receives the :update_attributes message with whatever params are
       # submitted in the request.
       Acquisition.any_instance.should_receive(:update_attributes).with(:acquisition =>    {:person_id => '10'})
      puts "before the put "
       put :update, :id => acquisition.id, :acquisition => { :person_id …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails update-attributes

5
推荐指数
1
解决办法
4111
查看次数

validation_context和update_attributes

如何使用update_attributes指定validation_context?

我可以使用2个操作(没有update_attributes)来做到这一点:

my_model.attributes = { :name => '123', :description => '345' }
my_model.save(:context => :some_context)
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails update-attributes

4
推荐指数
1
解决办法
2425
查看次数