Sebastian Plasschaert
Sebastian Plasschaert

Reputation: 304

Replacing partial with AJAX in Rails 4 can't find my view

I'm trying to change a button class with an upvote and downvote and try to use the build-in AJAX functionality for it. I think that everything is there, but when running it it's still missing a specific view.

models/oneliner.rb

has_many :general_connections
has_many :users, through: :general_connections

models/general_connection.rb

belongs_to :oneliner
belongs_to :user

models/user.rb

has_many :general_connections
has_many :oneliners, through: :general_connections

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/oneliners/index.html.erb

<h5><%=t 'index.title' %></h5>
  <div class="row">
    <ul class="collection">
      <% @oneliners.each do |oneliner| %>   
        <%= render partial: '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, but I'm getting the following error:

ActionView::Template::Error (undefined local variable or method `oneliner' for #<#<Class:0x007fe4f15056a8>:0x007fe4f5408eb8>):
1: $('#<%= dom_id(oneliner) %>').replaceWith(<%= j render partial: 'oneliners/list', locals: {oneliner: oneliner} %>");
app/views/general_connection/like_oneliner.js.erb:1:in `_app_views_general_connection_like_oneliner_js_erb___1501586799937634897_70310671877500'
app/controllers/general_connection_controller.rb:7:in `like_oneliner'

Can anyone help me out?

Update: changed the controller and .js.erb sections according to some tips from user4382423

Upvotes: 0

Views: 123

Answers (1)

Ankita Agrawal
Ankita Agrawal

Reputation: 422

The like and unlike methods do works, but I'm getting the following error:

> ActionView::MissingTemplate (Missing template
> general_connection/like_oneliner, application/like_oneliner with
> {:locale=>[:en], :formats=>[:js, :html], :variants=>[],
> :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :arb, :jbuilder]}.

Your template must be in general_connection folder not in general_connections

Upvotes: 1

Related Questions