Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render template and change url string in browser?

I have 2 actions - Edit and Update. Form in Edit submits the values to Update action. When saving a model fails I render edit teplate, where user sees errors and fields are prepopulated with what he filled before. There's a huge but for me - in URL panel in user's browser there's /user/update, even when (and because) I rendered edit template. Can I somehow change that with passing some parameters to render method in update action? I don't want the user to see that there's any (update) action aside of edit. Is it possible?

like image 530
Kreeki Avatar asked Jan 16 '11 19:01

Kreeki


1 Answers

Here is the third way around this:

In your routes.rb

resources :users
match 'users/:id/edit' => 'users#update', :via => :put, :as => :put_user

In your view (edit.html.erb for example)

<%= form_for @user, :url => put_user_path do |f| %>
  ...
<%  end %>

In your controller (users_controller.rb for example)

def update
  @user = User.find(params[:id])
  if @user.update_attributes(params[:user])
    ...
  else
    render 'edit'
  end
end
like image 79
CL Chang Avatar answered Sep 27 '22 17:09

CL Chang