我得到這個菜單上我的網站獲取與不同的按鈕不同的HTML與AJAX
%button.dropdown-button
.current-user{onclick:'showMenu()'}
%img.current-user-image{src:current_user.picture_url}
= current_user
%i.fa.fa-bars{'aria-hidden':true}
.dropdown-content
.menu-option{onclick:'showFilters()'}
Filter Transactions
%i.fa.fa-paper-plane{'aria-hidden':true}
.transaction-filters
.filter-option
My Transactions
%i.fa.fa-square-o
%a{href:'#'}
.menu-option
Balance History
%i.fa.fa-history{'aria-hidden':true}
%a{href:destroy_user_session_path}
.menu-option
Sign Out
%i.fa.fa-sign-out{'aria-hidden':true}
而且我得到了這個時間表與交易
.timeline-container
- @transactions.each do |transaction|
.transaction-container
.transaction-header-container
.transaction-kudos-container
= "+#{transaction.amount}"
%span.currency
₭
.transaction-avatar-container
%div
= image_tag transaction.receiver_image, class:'avatar'
.transaction-body-container
.transaction-content
%span
= "#{transaction.sender.name}:"
%span.highlighted
= "+#{transaction.amount} ₭"
%span
to
%span.highlighted
= transaction.receiver_name_feed
%span
for
%span.highlighted
= transaction.activity_name_feed
%div
-#%button#like-button
-# Like
%span.post-time-stamp
= "#{distance_of_time_in_words(DateTime.now, transaction.created_at)} ago"
= paginate @transactions
它們都呈現在我的index.html.haml
所以當我點擊div .filter-option.sent
我想更改代碼從
- @transactions.each do |transaction|
到
- @all_transactions.each do |transaction|
篩選出當前用戶的交易無需重新加載頁面。
這些變量在我的控制器
@transactions = Transaction.order('created_at desc').page(params[:page]).per(20)
@all_transactions = Transaction.all_for_user(current_user)
定義,在我的模型的方法all_for_user
def self.all_for_user(user)
Transaction.where(sender: user).or(Transaction.where(receiver: user)).order('created_at desc').page.per(20)
end
我嘗試了很多事情AJAX但似乎沒有任何討好我。有人可以幫助我嗎?
另外我不建議使用(.menu-option {onclick:'showFilters()'})onclick html選項。我個人認爲它的凌亂和難以組織你的html和Javascript代碼..但是對於他們自己。 –