javascript - RubyJS how to reload page after submit button? - Stack Overflow

I'm writing an web app using ruby on rails.In the controller:def create@category = Category.new(

I'm writing an web app using ruby on rails. In the controller:

  def create
@category = Category.new(category_params)

respond_to do |format|
  if @category.save
    format.html { redirect_to @category, notice: 'Category was successfully created.' }
    format.json { render :show, status: :created, location: @category }
    format.js { render js: 'window.top.location.href = "/categories";' }
  else
    format.html { render :new }
    format.json { render json: @category.errors, status: :unprocessable_entity }
  end

end

In the new.html.erb:

<h1>New category</h1>
<%= render 'form' %>
<%= link_to 'Back', categories_path %>

In _form.html.erb:

<%= form_for(@category) do |f| %>
<% if @category.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@category.errors.count, "error") %> prohibited this category from being saved:</h2>
  <ul>
  <% @category.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>
  </ul>
</div>
<% end %>
<div class="field">
<%= f.label :content %><br>
<%= f.text_field :content, placeholder: "content" %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description, placeholder: "description" %>
</div>
<div class="actions">
<a href="#" onclick="window.top.location.reload(true)"><%= f.submit  %>
</a>
</div>
<% end %>

I want the submit button to function and the newly created category is saved before the page is refreshed. In the code, the page is refreshed before saving. Please help. thanks

I'm writing an web app using ruby on rails. In the controller:

  def create
@category = Category.new(category_params)

respond_to do |format|
  if @category.save
    format.html { redirect_to @category, notice: 'Category was successfully created.' }
    format.json { render :show, status: :created, location: @category }
    format.js { render js: 'window.top.location.href = "/categories";' }
  else
    format.html { render :new }
    format.json { render json: @category.errors, status: :unprocessable_entity }
  end

end

In the new.html.erb:

<h1>New category</h1>
<%= render 'form' %>
<%= link_to 'Back', categories_path %>

In _form.html.erb:

<%= form_for(@category) do |f| %>
<% if @category.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@category.errors.count, "error") %> prohibited this category from being saved:</h2>
  <ul>
  <% @category.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>
  </ul>
</div>
<% end %>
<div class="field">
<%= f.label :content %><br>
<%= f.text_field :content, placeholder: "content" %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description, placeholder: "description" %>
</div>
<div class="actions">
<a href="#" onclick="window.top.location.reload(true)"><%= f.submit  %>
</a>
</div>
<% end %>

I want the submit button to function and the newly created category is saved before the page is refreshed. In the code, the page is refreshed before saving. Please help. thanks

Share Improve this question asked May 9, 2016 at 8:16 BaodoanBaodoan 631 silver badge4 bronze badges 1
  • 1 remove onclick="window.top.location.reload(true)" – madalinivascu Commented May 9, 2016 at 8:24
Add a ment  | 

2 Answers 2

Reset to default 4

I prefer using unobtrusive javascript. As such, my answer as follows:

_form.html.erb

<%= form_for(@category, remote: true) do |f| %>
  ...
  <div class="actions">
    <%= f.submit %>
  </a>
</div>
<% end %>

categories_controller.rb

def create
  @category = Category.new(category_params)

  respond_to do |format|
    if @category.save
      format.html { redirect_to @category, notice: 'Category was successfully created.' }
      format.json { render :show, status: :created, location: @category }
      format.js { render js: 'window.top.location.reload(true);' }
    else
      format.html { render :new }
      format.json { render json: @category.errors, status: :unprocessable_entity }
    end
  end
end

Note that I updated format.js to render a JS script that will reload the page, as you wanted. You should know however that this script will work on ANY page wherever a JS POST request to /categories is called.

Hello there In this case you should submit the form through js i.e.

<%= form_for(@category, remote: true) do |f| %>
<%= end%>

Please let me know if is there any issue.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742332818a4424057.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信