javascript - Have to refresh page for ajax request, page_html method deprecated issue -
i'm trying generate , redirect ajaxs request forms , display messages they`re created. i've been able work, unfortunately have refresh page message show up.
the problem i'm using rails 4.1.7 , replace_html method deprecated, latest stable version beingness v3.0.9
# messages controller before_action :set_message, only: [:show, :edit, :update, :destroy] respond_to :html, :json def index @messages = message.all @the_message = message.new respond_to |format| format.json { render :update |page| page.replace_html 'messages', :partial => 'list', :locals => {:messages => @messages} end } format.html end end def create @the_message = message.new(message_params) respond_to |format| if @the_message.save format.json { redirect_to messages_path(:format => :json) } format.html { redirect_to messages_path } else format.html { render :new } format.json { render json: @the_message.errors, status: :unprocessable_entity } end end end
in form partial, i'm setting remote true rails convert html form 1 can send ajax requests. , i`m setting format alternative json server can send javascript automatically update message list.
<%= form_for(@the_message, :remote=>true, :format => json) |f| %> .. <% end %>
then created partial generates part of page message-list, , rendered in index file
#_list.html.erb <% messages.each |message| %> ... <% end %> #index.html.erb <%= render 'list', :messages => @messages %>
how can prepare this? help appreciated.
never mind. able solve problem.
i removed index method:
respond_to |format| format.json { render :update |page| page.replace_html 'messages', :partial => 'list', :locals => {:messages => @messages} end } format.html end
and created create.js.erb file, identical list.html.erb file, replacing:
<% messages.each |message| %> <%= message.stuff %> <%= message.morestuff %> etc.. <% end %>
with:
<%= @the_message.stuff %> <%= @the_message.morestuff %> etc..
javascript ruby-on-rails ajax
No comments:
Post a Comment