不渲染Rails AJAX

时间:2017-05-10 08:23:54

标签: ruby-on-rails ajax partial

我想在添加新帖子后在我的“static_pages / index.html.erb”视图中呈现部分“shared / _feed.html.erb”,但它只会消除“#posts”! 我不知道,在哪里找到我的错误! 结果如下:

Started POST "/posts" for 127.0.0.1 at 2017-05-10 10:50:23 +0300 Processing by PostsController#create as JS   Parameters: {"utf8"=>"✓", "authenticity_token"=>"uTjOINWDjrTfzvkNowDH9vKRTGrHenps88DZdnZmifM=", "post"=>{"title"=>"fasdfsdf", "body"=>"sdfasfa"}, "commit"=>"Post"}   User Load (1.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`active` = 1 AND `users`.`id` = 1 LIMIT 1    (0.7ms)  BEGIN   SQL (382.8ms)  INSERT INTO `posts` (`body`, `created_at`, `published_at`, `title`, `updated_at`, `user_id`) VALUES ('sdfasfa', '2017-05-10 07:50:23', NULL, 'fasdfsdf', '2017-05-10 07:50:23', 1)   User Load (1.5ms)  SELECT `users`.* FROM `users` WHERE `users`.`active` = 1 AND `users`.`id` = 1 LIMIT 1   SQL (32.0ms)  UPDATE `users` SET `posts_count` = COALESCE(`posts_count`, 0) + 1 WHERE `users`.`active` = 1 AND `users`.`id` = 1    (51.1ms)  COMMIT   Rendered posts/_post_added.html.erb (0.2ms)    (2.2ms)  SELECT COUNT(*) FROM `posts` WHERE `posts`.`user_id` = 1   Rendered shared/_feed.html.erb (0.0ms)   Rendered posts/create.js.erb (11.4ms) Completed 200 OK in 503.6ms (Views: 16.8ms | ActiveRecord: 471.7ms)

"static_pages/index.html.erb"

视图/帖/ create.js.erb:

    $("#add_post").replaceWith("<%= j render(:partial =>
    'posts/post_added') %>"); $(".posts_count").html("<%=
    pluralize(current_user.posts.count, 'post') %>");
    $("#posts").replaceWith("<%= j render(:partial => 'shared/feed',
    collection: @feed_items) %>")

路线:

Courses::Application.routes.draw do   resources :users do
    collection { post :import}   end

  resources :users do
    resources :posts   end

  root to: "static_pages#index"   get "/home" => "static_pages#index"

  resources :sessions, only: [:new, :create, :destroy]

  match 'index', to: 'static_pages#index', via: [:get, :post]


  match '/signup', to: 'users#new'   match '/signin', to: 'sessions#new'   match '/signout', :to => 'sessions#destroy'

  resources :posts   resources :books do
    post 'search', on: :collection   end end

视图/ static_pages / index.erb.html:

<% if signed_in? %>
  <div class="row">
    .
    .
    <aside class="col-md-4">
      <section>
        <%= render 'shared/user_info' %>
      </section>


      <section class="add_post" id="add_post">
        <h3>Post Add</h3>
        <%= render 'shared/post_form' %>
      </section>
    </aside>
    <div class="col-md-8">
      <h3>Post Feed</h3>
      <div id="posts">
        <%= render 'shared/feed' %>
      </div>
    </div>
  </div>

<% else %>

  <div class="center hero-unit">
    <h1>Welcome to the Sample App</h1>
    <h2>
      This is the home page for the
      <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
      sample application.
    </h2>
    <%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %>
    <%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>
  </div>

<% end %>

posts_controller.rb

class PostsController < ApplicationController
  before_filter :authorize, only: [:edit, :destroy, :update]
  before_filter :correct_user,   only: [:edit, :update, :destroy]


  def new
    @post = Post.new
  end

  def create
    @post = current_user.posts.build(params[:post])
    respond_to do |format|
      if @post.save
        flash[:success] = "Post created!"
        format.html { redirect_to @post }
        format.js 
        format.json { render json: @post, status: :created, location: @post }
      else
        flash.now[:error] = "your message"
        @feed_items = []
        render 'static_pages/home'
      end
    end
  end

  def index
    @posts = Post.order(:created_at).paginate(page: params[:page])
    @post = current_user.posts.build if signed_in?
  end

  def post_params
    params.require(:post).permit(:title,:body)
  end

  def show
    if signed_in?
      @post  = current_user.posts.build
      @feed_items = current_user.feed.paginate(page: params[:page])
    end
  end


  def edit
    @post = Post.find(params[:id])
  end

  def destroy
    @post.destroy
    # redirect_to root_path, :notice => 'The Post has been deleted!'
    respond_to do |format|
      format.html { redirect_to posts_url }
      format.js { render :layout => false }
      format.json { head :no_content }
    end
  end

  def update
    @post = Post.find(params[:id])
    if @post.update_attributes(params[:post])
      redirect_to posts_path, :notice => 'The Post is Updated'
    else
      render "edit"
    end
  end
  private

    def correct_user
      @post = current_user.posts.find_by_id(params[:id])
      redirect_to root_url, :notice => 'Can\'t' if @post.nil? 
    end
end

static_pages_controller.rb

class StaticPagesController&lt; ApplicationController中

  def index
    if signed_in?
      @post  = current_user.posts.build
      @feed_items = current_user.feed.paginate(page: params[:page])
    end
  end

  def destroy
  end

  def create
  end


  def show
  end
end

视图/共享/ _feed.html.erb

<% if @feed_items.any? %>
  <ol class="posts">
    <%= render partial: 'shared/feed_item', collection: @feed_items %>
  </ol>
  <%= will_paginate @feed_items %>
<% end %>

视图/共享/ _feed_item.html.erb

<li id="<%= feed_item.id %>">
  <%= link_to gravatar_for(feed_item.user), feed_item.user %>
  <span class="user">
    <%= link_to feed_item.user.username, feed_item.user %>
  </span><br>
  <span class="content"><u><%= feed_item.title %></u></span><br>
  <span class="content"><%= feed_item.body %></span><br>
  <span class="timestamp"><small>
    Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
  </small></span>
  <% if current_user?(feed_item.user) %>
    <%= link_to "delete", feed_item, method: :delete, data: { confirm: "You sure?" }, title: feed_item.title, remote: true, :class => 'delete_post' %>
  <% end %>
  <hr>
</li>

这是我的git https://github.com/mkiselyow/hw5_hw6/commit/eec43a2c6faabde4fc75c4146ca41ced534ce999

0 个答案:

没有答案