0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

Oops, yes, missed that one. But it still isn't working. The new error is: ActionView::Template::Error (undefined local variable or method "oneliner"for #<#<Class:0x007fe4fb0d3648>:0x007fe4f7a2a2b8>): 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_70310691947700" app/controllers/general_connection_controller.rb:7:in "like_oneliner"
it because oneliner is local variable you should change it to instance variable.. e.g @oneliner and change locals: {oneliner: oneliner} to locals: {oneliner: @oneliner}.. and that work for you :)
I've changed the .js.erb locals into locals: {oneliner: @oneliner}, but I'm still getting the same error.
have you change local variable in controller too??
change in app/controllers/general_connection_controller.rb
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.