English 中文(简体)
我如何在铁路废墟上使用替代更新方法更新我的方法?
原标题:How can I update my method using an alternative update method in ruby on rails?

Is it possible to update from an action/method other than the update action/method? For example in my users controller I already have an update method for other parts of my users account.

我需要改变我的用户密码。 有可能这样做:

def another_method_to_update
  user = User.authenticate(current_user.email, params[:current_password])
  if user.update_attributes(params[:user])
    login user
    format.js   { render :js => "window.location =  #{settings_account_path} " } 
    flash[:success] = "Password updated" 
  else
    format.js   { render :form_errors }

  end
end

Then have my change password form know to use that method to perform the update?

It has 3 fields: current password new password confirm new password

我利用Ajax显示形式错误。

幼儿园

最佳回答

是的;<代码>update行动只是一个缺省,使基于区域无害环境技术的接口容易轻松。 您将保证在<代码>config/routes.rb上有一个POST线路,该线路指的是users#another_method_to_update,推定在用户主计长(和铁路3)中做了所有这些工作,但回答你的问题的基本答案是,在你具备模型的任何地点都可以进行模型操作(包括更新领域)。

可以称之为示范方法与正在采用何种控制方法之间没有搭配。

问题回答

为什么你们想要利用另一条路线来做到这一点? 与公约一致,使用违约路线。 如果我正确理解,你的网页含有更新密码的表格。

我们可以在更新方法中为此撰写整个法典,但这更清洁,更不言而喻:

def update
   change_password and return if change_password?
   # old code of your update method
   # ...
end

private

def change_password?
   !params[:current_password].nil?
end

def change_password
  user = User.authenticate(current_user.email, params[:current_password])
  respond_to do |format|
    if user.update_attributes(params[:user])
     login user
     flash[:success] = "Password updated" 
     format.js   { render :js => "window.location =  #{settings_account_path} " } 
    else
     format.js   { render :form_errors }
    end
  end
end 

对于那些将研究你的法典的人来说,这非常容易理解,因为你仍然呼吁更新方法,更新你的模式,然后采取习惯行动。

我也确定了你的习俗法。

in config/routes.rb:

puts "users/other_update_method"




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签