The goal is to create, edit and destroy model category on index page. I am able to save the model but I can not refresh the listing. I have read many similar examples, its confusing.
This is what I have:
At views/categories/index.html.erb
<!-- Full expand -->
<div class="row mt-20">
<div class="well-basic">
<div class="col-xs-8">
<table class="table">
<thead>
<tr>
<th>Content</th>
<th>Actions</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @categories.each do |category| %>
<tr>
<td><%= category.content %></td>
<td> Edit |
<%= link_to '', category, method: :delete, data: { confirm: 'Are you sure?' }, class: 'glyphicon glyphicon-remove' %> </td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div class="col-xs-4">
<%= render '/categories/form', :locals => {:category => @category} %>
</div>
</div>
</div>
<!--/ Full expand -->
This is the form at app/categories/_form.html.erb
<%= simple_form_for(@category, remote: true ) do |form| %>
<%= form.error_notification %>
<div class="form-inputs">
<%= form.input :content %>
</div>
<div class="form-actions">
<%= form.button :submit, class: 'btn btn-success custom-reload' %>
</div>
<% end %>
Seems like trouble comes here: at app/controllers/categories_controller.rb
class CategoriesController < ApplicationController
def index
@user = current_user
@categories = @user.categories
@category = Category.new
end
def create
@user = current_user
@category = @user.categories.build(category_params)
respond_to do |format|
if @category.save
format.html { redirect_to @categories, notice: 'Category has been sucesifully created' }
format.js { }
format.json { render json: @category, status: :created, location: @category }
else
format.js { render @categories, notice: 'Category could not been saved.' }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
def destroy
@category = Category.find(params[:id])
@category.destroy
redirect_to :back
end
private
def category_params
params.require(:category).permit(:content)
end
end
This is app/views/categories/create.html.erb. First line works, second does not.
$("<%= escape_javascript(render @category") %>).appendTo("#categories")
$('.custom-reload').on('click', function() {
window.location.reaload();
});
Please, help, does someone knows how to refresh the page. Even, better, does someone have a full example of this kind of common usage of forms inside listings? (I am still not even trying to edit, but delete works already). Thanks.
答案 0 :(得分:0)
我找到了一种方法,可以在同一页面上显示列表和模型的形式。这是非常基础的,也许更多的例子可以很好地超越RoR约定。感谢。
该模型是分类。
# views/categories/_category.html.erb
<%= category.content %>
<%= link_to '', category, method: :delete, data: { confirm: 'Are you sure?' }, class: 'glyphicon glyphicon-remove' %> </td>
# views/categories/index.html.erb
<div class="col-xs-8">
<%= render @categories %>
</div>
<div class="col-xs-4">
<%= render '/categories/form', :locals => { :category => @category } %>
</div>
# views/categories/_from.html.erb
<%= simple_form_for(@category, remote: true ) do |form| %>
<%= form.error_notification %>
<div class="form-inputs">
<%= form.input :content %>
</div>
<div class="form-actions">
<%= form.button :submit, class: 'btn btn-success btn-action' %>
</div>
<% end %>
# controllers/categories_controller.rb
def create
@user = current_user
@category = @user.categories.new(category_params)
if @category.save
respond_to do |format|
format.html { render @categories, :notice => 'Category has been successfully created' }
format.js { } // No need to write here because rails search for create.js.erb by default
end
else
respond_to do |format|
format.html { render @categories, :alert => 'Category could not been saved.' }
format.js { render "fail_create.js.erb" }
end
end
end
def destroy
@category = Category.find(params[:id])
@category.destroy
redirect_to :back
end
# views/categories/create.js.erb
$("<%= escape_javascript(render @category) %>").appendTo("#categories");
$('.btn-action').on('ajax:success', window.location.reload(true));
# views/categories/fail_create.js.erb
alert("<%= @category.errors.full_messages.to_sentence.html_safe %>");
我仍然会在同一页面上搜索编辑方式。如果有人知道,请发一个例子。