vendredi 31 juillet 2015

Rails 4 form and listing on same page, correct format responses

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.

Aucun commentaire:

Enregistrer un commentaire