# Rails 3 - AJAX Response

**URL:** <https://www.ruby-forum.com/t/rails-3-ajax-response/190574>\
**Category:** Rails\
**Created:** [June 23, 2010, 5:33am UTC](https://www.ruby-forum.com/t/rails-3-ajax-response/190574 "2010-06-23T05:33:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric2323223](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eric2323223](https://www.ruby-forum.com/u/eric2323223)\
**Post date:** [June 23, 2010, 5:33am UTC](https://www.ruby-forum.com/t/rails-3-ajax-response/190574/1 "2010-06-23T05:33:25Z")

</div>

Hi,

I need help with some AJAX in rails 3.

So basically, I have a select option in my index page for my User  
model. This select option is a form that modifies the user’s type. The  
" :remote =\> true " works and the type is changed when hit the submit  
button. But, nothing happens in the browser. The user is updated.

I’m actually not sure where to put either my JS / RJS file ( I’ve  
tried both, but neither do anything. )

So here’s my code:

# The form:

# \<%= form\_for(@user, :remote =\> true) do |f| %\> \<%= f.select(“type\_id” , Type.all.collect{ |t| [t.name,t.id] } ) %\> #TYPE is another model. User belongs\_to Type \<%= f.submit “Save” %\> \<% end %\>

# The model:

class UsersController \< ApplicationController  
skip\_before\_filter :authorize, :only =\> [:new, :create]

# GET /users

# GET /users.xml

def index  
@users = User.all

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @users }
end

```

end

# GET /users/1

# GET /users/1.xml

def show  
@user = User.find(params[:id])

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @user }
end

```

end

# GET /users/new

# GET /users/new.xml

def new  
@user = User.new

```
respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @user }
end

```

end

# GET /users/1/edit

def edit  
@user = User.find(params[:id])  
if params[:change\_type]  
change\_type  
end  
end

# POST /users

# POST /users.xml

def create  
@user = User.new(params[:user])

```
respond_to do |format|
  if @user.save
    format.html { redirect_to(@user, :notice => 'User was

```

successfully created.’) }  
format.xml { render :xml =\> @user, :status  
=\> :created, :location =\> @user }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @user.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

# PUT /users/1

# PUT /users/1.xml

def update

```
@user = User.find(params[:id])

unless params[:user][:type_id]

    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to(@user, :notice => 'User was

```

successfully updated.’) }  
format.js  
format.xml { head :ok }  
else  
format.html { render :action =\> “edit” }  
format.js  
format.xml { render :xml =\> @user.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
else  
respond\_to do |format|  
if @user.update\_attribute(“type”,  
Type.find\_by\_id( params[:user][:type\_id] ) )  
format.js{ head:ok}  
else  
format.js {head:ok}  
end  
end

```
end

```

end

def change\_type  
unless params[:change\_type\_value]  
if @user.category == 5  
@user.category = 0  
else  
@user.category = 5  
end

```
  respond_to do |format|
    if @user.save
      format.html { redirect_to( users_path, :notice =>

```

"Sucessfully changed " + @user.name.titleize + “'s category” ) }  
format.js  
else  
format.html { redirect\_to( users\_path, :notice =\> "Failed to  
change " + @user.name.titleize + “'s category” ) }  
end  
end  
end  
end

# DELETE /users/1

# DELETE /users/1.xml

def destroy  
@user = User.find(params[:id])  
@user.destroy

```
respond_to do |format|
  format.html { redirect_to(users_url) }
  format.xml { head :ok }
end

```

end

## end

# The Response:

Started POST “/users/1” for 127.0.0.1 at Tue Jun 22 23:31:07 -0400  
2010  
Processing by UsersController#update as JS  
Parameters: {“commit”=\>“Save”, “authenticity\_token”=\>“OT1oYw/  
cSiLrbT1w9PNu8A9u6v5eZy/bFxjbhEo3lbY=”, “id”=\>“1”,  
“user”=\>{“type\_id”=\>“1”}}  
User Load (0.9ms) SELECT “users”.\* FROM “users” WHERE (“users”.“id”  
= 1) LIMIT 1  
CACHE (0.0ms) SELECT “users”.\* FROM “users” WHERE (“users”.“id” =

1. LIMIT 1  
Type Load (0.8ms) SELECT “types”.\* FROM “types” WHERE (“types”.“id”  
= 1) LIMIT 1  
Completed 200 OK in 347ms  
====
