开发者

Rails: basic Ajax question! - I'm looking for example code about simple adding and editing for list

I've created simple rails page using 'ruby script/generate scaffold subject name:string desc:string' command.

Now, I can add, edit and delete subject in RESTful way.

What I' like to do is changing above behavior as Ajax way. Everything can be done in index page. For example...this is the page throw the browser~

screen 1

Listing subjects
name  desc
test1 test1 show edit destroy
test2 test2 show edit destroy
[___] [___] create

If you click edit link, the page will transform like bellow

screen 2

Listing subjects
name    desc
[test1] [test1] update             <-------- this !!! text_field !
test2   test2   show edit destroy

And you can change name and desc. After change the values, clicking update link will update it and browser will display similar page with screen 1

Also, you can add new subject with just filling empty text field bottom of the list and clicking create link. It will be applied immediately.

I tried to find this 开发者_如何学运维out but railscasts and even some other webpage did not help. :(

Do anybody know where the sample code for this is or please write simple one !!! I really appreciate it if you do so...........


wrap each row that you want to dynamically update in a form_remote_tag. When the update button is clicked, the form fields are sent via ajax to the server.

<% form_remote_tag :action => :update do %>
  <%= text_field_tag :name, @name %>
  <%= text_field_tag :desc, @desc %>
  <%= submit_tag "Update" %>
<% end %>

Then on the server side do something like this:

def update
  #do normal update stuff

  #send RJS back to the user that we did something.
  render :update do |page|
    page.alert "item updated!"
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜