如果您在这里查看项目:http://www.leapfm.com/,您可以向下滚动并在首页上单击“更多”,它可以正常工作。但是,如果您尝试在“新歌曲”选项卡中执行此操作,则会保持“页面正在加载...”。我不太清楚为什么会这样。
new_songs.html.erb
<div id="new_songs">
<%= render 'new_songs'%>
</div></div>
_new_songs.html.erb(部分)
<div id="layout-1">
<!--div class="left-side"> -->
<%#= will_paginate @songs %>
<h6>New songs</h6>
<hr>
<ol><% @songs.each do |song| %>
<span class="title">
<li><%= link_to song.title, song %><span class="subtext"> (<%= song.url %>)<br></li></span>
<%#=link_to '▼'.html_safe, vote_against_song_path(song), :remote => true, :method => :put %>
posted <%= time_ago_in_words(song.created_at) + " ago" %>
<small><span class="comments"></small> | <%= pluralize(song.comments.size, 'comment') %></span></small><br /></span>
<%#= link_to 'Show', song, class: "button small secondary" %>
<%= link_to('Edit', edit_song_path(song), class: "button small secondary") if can? :update, song %>
<%= link_to('Destroy', song, method: :delete, data: {confirm: 'Are you sure?'}, class: "button small secondary") if can? :destroy, song %>
<% end %>
</ol>
</div>
<div class="pagination-centered">
<ul class="pagination">
<%#= will_paginate @songs %>
<!-- or custom pagination -->
<% if @songs.previous_page %>
<%= link_to "Back", params.merge(page: @songs.previous_page) %>
<% end %>
<% if @songs.next_page %>
<%= link_to "More", params.merge(page: @songs.next_page) %>
<% end %>
</ul></div>
pagination.js
$(function() {
$(".pagination a").on("click", function() {
$(".pagination").html("Page is loading...");
$.getScript(this.href);
return false;
});
});
song_controller.rb
class SongsController < ApplicationController
before_filter :authenticate_user!, only: [:create ,:edit, :update, :destroy, :vote_for_song]
before_action :set_song, only: [:show, :edit, :update, :destroy, :vote_for_song]
def extract_video
@song = Song.find(params[:id])
@song.YouTubeAddy.extract_video_id
end
def vote_for
@song = Song.find(params[:id])
current_user.vote_for(@song)
@song.plusminus = @song.votes_for
@song.save
respond_to do |format|
format.js { render 'update_votes' }
end
end
def vote_against
@song = Song.find(params[:id])
current_user.vote_against(@song)
respond_to do |format|
format.js { render 'update_votes' }
end
end
def new_songs
@songs = Song.order("id DESC").paginate(:page => params[:page], :per_page => 15)
end
# GET /Songs
# GET /Songs.json
def index
if params[:genre]
@songs = Song.tagged_with(params[:genre]).paginate(:page => params[:page], :per_page => 15)
else
@songs = Song.order('plusminus').paginate(:page => params[:page], :per_page => 15)
end
end
# GET /Songs/1
# GET /Songs/1.json
def show
@comment = Comment.new(song: @song)
@video_tag = YouTubeAddy.extract_video_id(@song.url)
end
# GET /Songs/new
def new
@song = Song.new
end
# GET /Songs/1/edit
def edit
end
# POST /Songs
# POST /Songs.json
def create
@song = Song.new(song_params)
respond_to do |format|
if @song.save
format.html { redirect_to @song, notice: 'Song was successfully created.' }
format.json { render action: 'show', status: :created, location: @song }
else
format.html { render action: 'new' }
format.json { render json: @song.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /Songs/1
# PATCH/PUT /Songs/1.json
def update
respond_to do |format|
if @song.update(song_params)
format.html { redirect_to @song, notice: 'Song was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @song.errors, status: :unprocessable_entity }
end
end
end
# Song /Songs/1
# Song /Songs/1.json
def destroy
@song.destroy
respond_to do |format|
format.html { redirect_to songs_url }
format.json { head :no_content }
end
end
private
def set_song
@song = Song.find(params[:id])
end
def song_params
params.require(:song).permit(:title, :artist, :url, :track, :user_id, :tag_list)
end
end
答案 0 :(得分:1)
您将2个操作的js代码放在同一个文件中。
您在index
中执行new_songs
行动和songs_controller.rb
行动,并将您的js置于index.js.erb
:
$("#songs").html("<%= escape_javascript(render("songs")) %>");
$("#new_songs").html("<%= escape_javascript(render("new_songs")) %>");
在名为new_songs.js.erb
的文件中移动第二行,您就完成了。