我尝试使用upvote和downvote更改按钮类,并尝试使用内置的AJAX功能。我认为一切都在那里,但在运行它时,它仍然缺少一个特定的视图。
模型/ oneliner.rb
has_many :general_connections
has_many :users, through: :general_connections
模型/ general_connection.rb
belongs_to :oneliner
belongs_to :user
模型/ user.rb
has_many :general_connections
has_many :oneliners, through: :general_connections
控制器/ 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
视图/ 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>
视图/ 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} %>");
类似和不同的方法确实有效,但我收到以下错误:
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'
任何人都可以帮助我吗?
更新:根据controller
.js.erb
和user4382423
部分
答案 0 :(得分:1)
类似和不同的方法确实有效,但我收到以下错误:
> 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]}.
您的模板必须位于 general_connections 文件夹中,而不是 general_connections