ラベル rjs の投稿を表示しています。 すべての投稿を表示
ラベル rjs の投稿を表示しています。 すべての投稿を表示

2010/07/01

collection_select の値を rjs に渡す

備忘録
<%= f.collection_select(:something , Company.find(:all), :id, :name, 
  {},
  {:onchange => remote_function(:url => {:action => "change_vendor"}, :with => "'id='+this.value")}
 )%> 

:with で含めた値がパラメータとして渡されます。

2010/02/25

rjs でflash を書き換える

flash[:notice] = t('request.confirm_success')
page.replace_html :notice, flash[:notice]
flash.discard

2010/01/09

Ajax を使ってコメントを挿入

Ajax をつかって、記事(article)にコメント(comment)を挿入するほうほう。よく使うわりにはいつもはまってしまうので備忘録。(カフェトークでは、レッスンにメモを追加するところにつかってます)

article.rb
class Article < ActiveRecord::Base
  belongs_to :user
  has_many :comments
  after_update :save_comments
  
  def new_comment_attributes=(comment_attributes)
    comment_attributes.each do |attributes|
      comments.build(attributes) if attributes[:content].length > 0
    end
  end
  
  # ここでは使ってない
  def existing_comment_attributes=(comment_attributes)
    comments.reject(&:new_record?).each do |comment|
      attributes = comment_attributes[comment.id.to_s]
      if attributes
        comment.attributes = attributes
      else
        comments.delete(comment)
      end
    end
  end
  
  def save_comments
    comments.each do |comment|
      comment.save(false)
    end
  end  
  
  # 特定のユーザのみに表示するためのメソッド
  def user_comments
    self.comments.find(:all, :conditions => {:display_user => true})
  end
end

comment.rb
class Comment < ActiveRecord::Base
  belongs_to :article 
end
views/articles/show.html.erb
<div id="comments">
 <% for comment in @article.pro_comments %>
  <%= render :partial => 'comment_item', :locals => {:comment_item => comment} %>
 <% end %>
</div>
<%= link_to_function "コメント追加" do |page|
      page.insert_html :bottom, :add_comment, :partial => 'comment' , :object => comment.new
<% end%>
<div id="add_comment"></div>

views/pro/articles/_comment_item.html.erb
<div class="comment" id="comment_<%= comment_item.id %>">
 <%= comment_item.content%>
</div>

views/articles/_comment.html.erb
<div class="add_comment" >
<% @comment = Comment.new %>
<% form_remote_for ([:comment, @article]), 
  :url => add_comment_article_path(@article), :html => { :method => 'put' } do |f| %>
   <%= f.text_area :content %>
  <%= f.submit "追加"  %>
<% end %>
</div>

add_comment.rjs
if params[:comment]
  @comment= comment.new(params[:comment])
  if @comment.save
    comment_item_id = "comment_" + @comment.id.to_s
    page.insert_html :bottom, "comments", :partial => "comment_item", :object => @comment
    page[comment_item_id].visual_effect :highlight, :duration => 0.5, :startcolor => "#66CCFF"
    page.replace_html "add_comment", ""
  else
    @comment.errors.each{|attr,msg| page.alert("#{attr} - #{msg}")}
  end      
end

routes.rb
map.resources :articles, :member => {:add_comment => :put}

RJS 参考色々

最近 rjs を使うことが多くなってきたので、備忘録

特定のアイテムの下にインサートする場合

page.insert_html :bottom, "item_" + params[:id], :partial => "item", :object => item
この場合、partial と object名は一致している必要がある。

ハイライト

page[:item].visual_effect :highlight, :duration => 0.5, :startcolor => "#66CCFF"
特定のアイテムをハイライトしたい場合(例 div id="item_{アイテムid}")
item_id = "item_" + item.id.to_s
page[item_id].visual_effect :highlight, :duration => 0.5

その他色々

page.alert("hello")

# 表示トグル
page['mytest'].toggle

page.alert(params.inspect)

target_field = "prefix_" + params[:target]
page[target_field].value = temp

page.visual_effect :toggle_appear, params[:somename], :duration => 0.5
page[:item_form].remove
page.redirect_to root_path
page.replace_html 'someid', :partial => 'somepartial', :object => @someobject
page.insert_html :bottom, 'someid', :partial => 'somepartial', :locals => {:this => this, :that => that}
page << "new Ajax.Request('"+ url_for(:action => :some_ajax_action, :params =>{:myparam => myparam} )+"')"
page.visual_effect :shake 'score'
page.visual_effect :fade  'dodo'
page.delay(1.5) do 
  page.visual_effect :fade 'sunset'
end