开发者

How can I have multiple forms to update the same model?

I am new to Rails.

I have a User model. I would like a web page that allows users to change their :name and :email, and another web page that allows them to change their password.

Right now, I have a form to edit :name and :email at

/users/1/edit

The form on the page is

<%= form_for(@user) do |f| %>

My routes.rb has

resources :users

This works. Users can edit their :name and :email just fine. How do I now set up another web page with another form that allows them to change their passwor开发者_StackOverflowd?

Thank you.


You can set up any actions you like in your controller. The default CRUD operations are there to cover the basics, but there is no inherent limitation to what you can do.

#controller:
def change_password
  render :action => "change_password"
end 

#routes:
map.resource :users, :member => {:change_password => :get}

#view:
<%= form_for(@user) do |f| %>

The above would create the route: /users/1/change_password

In the view you simply have the change password fields => the form basically stays the same, submitting to your existing update action.


Per the comment from Sanjay regarding Toby's answer (and to save anyone the few minutes that I took to figure it out) in Rails 3 you would define the routes with:

#routes:
resources :users do
 member do
  get 'change_password'
 end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜