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” =
- LIMIT 1
Type Load (0.8ms) SELECT “types”.* FROM “types” WHERE (“types”.“id”
= 1) LIMIT 1
Completed 200 OK in 347ms
====