jquery+ajax+crud,用Rails3跟jQuery创建一个100% ajax CRUD应用

/views/posts/_posts.html.erb

I created this partial so i can update the list of posts with javascript too:

TitleContent
true %> true, :confirm => 'Are you sure?', :method => :delete %>

We need to change our controller too. Since we gonna make a 100% ajax CRUD we don't need that redirects anymore:

class PostsController < ApplicationController

before_filter :load

def load

@posts = Post.all

@post = Post.new

end

def index

end

def create

@post = Post.new(params[:post])

if @post.save

flash[:notice] = "Successfully created post."

@posts = Post.all

end

end

def edit

@post = Post.find(params[:id])

end

def update

@post = Post.find(params[:id])

if @post.update_attributes(params[:post])

flash[:notice] = "Successfully updated post."

@posts = Post.all

end

end

def destroy

@post = Post.find(params[:id])

@post.destroy

flash[:notice] = "Successfully destroyed post."

@posts = Post.all

end

end

Almost there. If you try to use the form now, nothing happens right? If you take a look at the console(server) you will find some errors telling you Rails can't find the view js to render. So let's create this files. Basically we gonna create a view to render javascript, with this javascript we gonna inject or modify our html. We are using jquery to do the modification/injection part, so if you never work with that take a look at the official site to learn a lot of cool stuffs: http://www.jquery.com We need files to create, edit, update and destroy actions, so let's create them:

/views/posts/create.js.erb

Here we verify if the @post object contains errors and changes the behavior according to that:

/*Hide the flash notice div*/

$("#flash_notice").hide(300);

*/Update the html of the div post_errors with the new one*/

$("#post_errors").html("");

*/Show the div post_errors*/

$("#post_errors").show(300);

*/Hide the div post_errors*/

$("#post_errors").hide(300);

*/Update the html of the div flash_notice with the new one*/

$("#flash_notice").html("");

*/Show the flash_notice div*/

$("#flash_notice").show(300);

*/Clear the entire form*/

$(":input:not(input[type=submit])").val("");

*/Replace the html of the div post_lists with the updated new one*/

$("#posts_list").html(" "posts") ) %>");

/views/posts/edit.js.erb

In this action we just need to update the form with the select post.

$("#post_form").html(" "form"))%>");

/views/posts/update.js.erb

$("#flash_notice").hide(300);

$("#post_errors").html("");

$("#post_errors").show(300);

$("#post_errors").hide(300);

$("#flash_notice").html("");

$("#flash_notice").show(300);

$(":input:not(input[type=submit])").val("");

$("#posts_list").html(" "posts") ) %>");

/views/posts/destroy.js.erb

$("#post_errors").hide(300);

$("#flash_notice").html("");

$("#flash_notice").show(300);

$("#posts_list").html(" "posts") ) %>");

And that's it, go to your "/posts" page and you should see our 100% ajax CRUD.

Ps1: That uses html5 to make our forms and links remotes (:remote => true), so it's not gonna work in any version of INTERNET EXPLORER yet.

Ps2: You can also use the redirect/render in your actions pointing to the index and then create only one javascript view (index.js.erb) treating that as you want. I prefer to keep things separated in case I decide to change the visual behavior of some view.

Ps3: Someone should make a generator for this ajax forms.

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值