Sebastian Plasschaert Sebastian Plasschaert - 1 month ago 7
Ajax Question

Replacing partial with AJAX in Rails 4: no errors and no updated data

For the following I'm not receiving any errors and my list-partial is being loaded again after executing the like_oneliner method. But the changed data (

oneliner.users.count
and the
if/else
for
!joined(oneliner)
are not refreshing) isn't showing.

I'm trying to change a button class with an upvote and downvote and try to use the build-in AJAX functionality for it.

models/campaign.rb

belongs_to :user
has_many :oneliners
has_many :general_connections


models/oneliner.rb

has_many :general_connections
has_many :users, through: :general_connections
belongs_to :campaign


models/general_connection.rb

belongs_to :oneliner
belongs_to :user
belongs_to :campaign


models/user.rb

has_many :general_connections
has_many :oneliners, through: :general_connections
has_many :campaigns


controllers/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


views/campaigns/show.html.erb

<div class="row">
<ul class="collection">
<% @oneliners.each do |oneliner| %>
<%= render partial: 'oneliners/list', locals: { oneliner: oneliner } %>
<% end %>
</ul>
</div>


views/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>


views/general_connection/like_oneliner.js.erb

$('#<%= dom_id(@oneliner) %>').replaceWith(<%= j render partial: 'oneliners/list', locals: {oneliner: @oneliner} %>");


The like and unlike methods do works, and I see a small change in the Timeago string (It changes for example from
2 months
in
2 months ago
), but that's it. The
oneliner.users.count
and the
if/else
for
!joined(oneliner)
only show the new data when I refresh the page.

Can anyone help me out?

Answer

$('#<%= dom_id(@oneliner) %>') this gives a button element which you are replacing. You should be replacing the whole partial by specifying it's id/class.

Comments