weixin_33739523 2017-05-10 08:23 采纳率: 0%
浏览 33

不渲染Rails AJAX

I want to render partial "shared/_feed.html.erb" in my "static_pages/index.html.erb" view after adding a new post but it just dissappers "#posts"! I don't know, where to find my mistake! Here is the result:

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"

views/posts/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) %>")

routes:

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

views/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 < 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

views/shared/_feed.html.erb

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

views/shared/_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>

and here is my git https://github.com/mkiselyow/hw5_hw6/commit/eec43a2c6faabde4fc75c4146ca41ced534ce999

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记