對於我沒有收到任何錯誤,我的名單,部分正在執行like_oneliner方法之後再加載以下。但改變的數據(oneliner.users.count
和if/else
爲!joined(oneliner)
未刷新)未示出。Rails中4 AJAX更換部分:沒有錯誤和沒有更新的數據
我試圖改變一個按鈕類的給予好評和downvote,並嘗試使用內置的AJAX功能吧。
型號/ campaign.rb
belongs_to :user
has_many :oneliners
has_many :general_connections
型號/ oneliner.rb
has_many :general_connections
has_many :users, through: :general_connections
belongs_to :campaign
型號/ general_connection.rb
belongs_to :oneliner
belongs_to :user
belongs_to :campaign
個型號/ user.rb
has_many :general_connections
has_many :oneliners, through: :general_connections
has_many :campaigns
控制器/ general_connection.rb
def like_oneliner
@oneliner = Oneliner.find(params[:oneliner_id])
current_user.general_connections.create(oneliner: @oneliner)
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
def unlike_oneliner
@general_connection = GeneralConnection.where("oneliner_id = ? AND user_id = ?", params[:oneliner_id], current_user.id).first
@oneliner = @general_connection.oneliner
@general_connection.destroy
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
的意見/運動/ show.html.erb
<div class="row">
<ul class="collection">
<% @oneliners.each do |oneliner| %>
<%= render partial: 'oneliners/list', locals: { oneliner: oneliner } %>
<% end %>
</ul>
</div>
的意見/ oneliners/_list.html.erb
<li class="collection-item avatar">
<i class="circle black"><%= oneliner.users.count %></i>
<span class="title"><%= oneliner.title %></span>
<p><%= timeago_tag oneliner.created_at, :nojs => true, :limit => 100.days.ago %>/<%=t 'list.employee' %><%= oneliner.user.name %>
</p>
<% if !joined(oneliner) %>
<%= form_tag(onelinerlike_path, remote: true) do %>
<%= hidden_field_tag 'oneliner_id', oneliner.id %>
<%= button_tag 'thumb_up', id: "#{ dom_id(oneliner) }", class: "secondary-content material-icons grey-text", style: "background-color:white;border:none;" %>
<% end %>
<% else %>
<%= form_tag(onelinerunlike_path, remote: true) do %>
<%= hidden_field_tag 'oneliner_id', oneliner.id %>
<%= button_tag 'thumb_up', id: "#{ dom_id(oneliner) }", class: "secondary-content material-icons orange-text", style: "background-color:white;border:none;" %>
<% end %>
<% end %>
</li>
的意見/ general_connection/like_oneliner.js.erb
$('#<%= dom_id(@oneliner) %>').replaceWith(<%= j render partial: 'oneliners/list', locals: {oneliner: @oneliner} %>");
等,以及不同的方法做工作,我看到TIMEAGO串一個小的變化(從改變例如2 months
in 2 months ago
),但就是這樣。該oneliner.users.count
和if/else
爲!joined(oneliner)
只顯示新的數據時,我刷新頁面。
任何人都可以幫我嗎?
'$( '#<%= dom_id(@oneliner)%>')'這給出了要更換按鈕元件。你應該通過指定它的id/class來替換整個部分。 –
是的,這是它的一個重要組成部分,它導致了我試圖給出「呈現部分:」和ID而不是部分本身的結論。它解決了,謝謝!你可以把上面的答案,以便我可以接受嗎? –
很高興:) –