简体   繁体   中英

jquery ajax rendering of a new post in ruby on rails

I'm trying to render the creation of a post with jquery ajax but I can't seem to get it to work correctly. It seems to render the post with very strange (I think nonexistent) styling and the flash message appears only after the page is refreshed. The post also get formatted correctly after the page is refreshed.

Here's the javascript:

$("#micropost_form").before('<div id="flash_notice"><%= escape_javascript(flash.delete(:notice))%></div>');
$("#user_info").html("<%= pluralize(current_user.microposts.count, "micropost") %>");
$("#feed_items").prepend(" <%= escape_javascript(render(:partial => @micropost)) %>")
$("#micropost_form")[0].reset();

The body of my application layout:

<body>
<div class="container">
  <%= render 'layouts/header' %>
  <section class="round">
<div id="flash_notice"><%= render 'shared/flash_messages'%></div>
    <%= yield %>
  </section>
  <%= render 'layouts/footer' %>
  <%= debug(params) if Rails.env.development? %>
</div>
</body>

Here's my home page (rendered in the yield of the application layout):

<table class="front" summary="For signed-in users">
<tr>
  <td class="main">
    <h1 class="micropost">What's happening?</h1>
    <%= render 'shared/micropost_form' %>
    <%= render 'shared/feed' %>
  </td>
  <td class="sidebar round">
    <%= render 'shared/user_info' %>
    <%= render 'shared/stats' %>
  </td>
</tr>
</table>

Here's my feed partial:

<% unless @feed_items.empty? %>
 <table id="feed_items" class="microposts" summary="User microposts">
  <%= render :partial => 'shared/feed_item', :collection => @feed_items %>
 </table>
 <%= will_paginate @feed_items %>
<% end %>

My flash messages partial:

<% flash.each do |key, value| %>
  <div class="<%= key %>"><%= value %></div>
<% end %>

Let me know if there's any other code I should post.

So what am I doing wrong? If you want to look at all the code and run it locally to get a better understanding of what's happening, you can find it here: https://github.com/meltzerj/sample_app

When you're using ajax, you need to use flash.now for flash messages. See docs here . Rails is looking for a page refresh/redirect to push and pop from the flash hash stack, using now will set it immediately, then you can access it as above. You can use flash.now in replace of your regular flash usage in the controller. Something like this is very typical:

  if @object.save
    flash.now[:notice] = "Object successfully created"
  else
   ...
  end

So that will fix your flash issue.

As for the weird content/styling, it's a bit hard to comment without actually seeing the markup. The only thing that looks odd to me is the render :partial => @micropost

I'm not sure if @micropost is just a string, but you'd normally do something like render :partial => 'path/to/partial', :object => @micropost

Again it's almost impossible to say without seeing all the other markup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM