Ajax introduction (1)

Ajax:

  • using javascript to make a request to server
  • fast when used correctly because partial page is refreshed
  • lost addressibility ( two actions may have the same URL )

JQuery:

  • a lib of javascript
  • support Ajax
  • more convienent than the rails’ lib Prototype and (***) for Ajax

JQuery syntax:

  • run on click: $(‘some_tag’).click(function(){})
  • run on submit: $(‘some_tag’).click( function() {} )
  • add stuff on a page: $(‘some_html_tag’).append();
  • with Ajax

$.ajax({

data: {

attr_name:  this.xxxselect.val()   // rails got the data by params[:attr_name] (which

// should equal to user’s selected value this.xxxselect.val() here)

}

success: function() {

}

error:  function() {

}

});

Good slides: http://www.slideshare.net/JamesEdwardGrayII/ajax-with-jquery-in-rails

Advertisements

controller test/passing parameters

get 'enrollments', {:id => '1'}   # comment: ( get/post(method) action, params[:id]=1 )
get :admin, :action, { :id => '1' }  # comments: ( get/post :role, :action, params[:id]=1)
response.should be_success

assigns for view

controller A

def action

@user = User.find(xxx)

end

A/action.html.erb

<%= “Welcome, #{@user.user_name}” %>

test the view (without controller)

let(:cur_user) { mock_model(User) }

assigns[:user] = cur_user

cur_user.stub(:user_name).and_return(“xxx”)