开发者

Modal window to add related records in ruby on rails

I have the following models

 class Post < ActiveRecord::Base
    ...
    has_many :tags
 end

 class Tag 开发者_开发技巧< ActiveRecord::Base
    belongs_to :post
 end

The Post controller has the typical CRUD actions plus the index. The post new view is (using simple_form and slim:

 = simple_form_for(@post) do |f|
    = f.input :title, :error => false
    = f.input :body, :as => :text, :error => false
    = f.association :tags, :include_blank => true
    div
       =link_to "Add a new tag", new_tag_path
    = custom_button "Save"
    | or 
    = link_to "Cancel", posts_path

The problem is in the "Add a new tag" link. What I would like to do is to provide a modal window with a tag creation form. Once the user has filled the form and saved the new tag, I would display back the post creation form and have the f.association :tags list updated with the newly created tag.

How should I implement this feature? It's not clear to me how to refresh only the :tag list once a new tag has been created, without loosing any other information the user may have inserted in the form (eg, post title) before creating the new tag.

Thanks.


may be you can go with this:

  • display tag creation popup
  • send AJAX request to create a tag (separate TagsController)
  • display some progress indicator
  • add option to 'tags' select and mark it as selected in AJAX success callback
  • so everything happens without page reload

... or (if having modal window is not a requirement) use something like this: http://railscasts.com/episodes/258-token-fields :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜