0

I created a website: http://rails.project-midas.com/categories and when i want to add new category or update it I get an error.

The log says:

FATAL -- : 
ActionView::Template::Error (undefined method `error_messages' for #<ActionView::Helpers::FormBuilder:0x000000031b5fa0>):
    1: <%= f.error_messages %> 
    2: <p> 
    3:     <%= f.label :name %><br /> 
    4:   <%= f.text_field :name %> 
  app/views/categories/_form.html.erb:1:in `_app_views_categories__form_html_erb___3155759666617831256_25435480'
  app/views/categories/edit.html.erb:3:in `block in _app_views_categories_edit_html_erb__3036452290204433295_27619860'
  app/views/categories/edit.html.erb:2:in `_app_views_categories_edit_html_erb__3036452290204433295_27619860'

CategoriesController:

class CategoriesController < ApplicationController
  before_action :set_category, only: [:show, :edit, :update, :destroy]

  # GET /categories
  # GET /categories.json
  def index
    @all_categories = Category.find(:all, :order =>"name")
  end

  # GET /categories/1
  # GET /categories/1.json
  def show
  end

  # GET /categories/new
  def new
    @category = Category.new
    @all_categories = Category.find(:all, :order => "name")
  end

  # GET /categories/1/edit
  def edit
    @category = Category.find(params[:id])
    @all_categories = Category.find(:all, :order => "name")
  end

  # POST /categories
  # POST /categories.json
  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 action: 'show', status: :created, location: @category }
      else
        format.html { render action: 'new' }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /categories/1
  # PATCH/PUT /categories/1.json
  def update
    respond_to do |format|
      if @category.update(category_params)
        format.html { redirect_to @category, notice: 'Category was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /categories/1
  # DELETE /categories/1.json
  def destroy
    @category.destroy
    respond_to do |format|
      format.html { redirect_to categories_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_category
      @category = Category.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def category_params
      params.require(:category).permit(:parent_id, :name)
    end
end

Category edit view file:

<h1>Zmiana kategorii</h1>
<% form_for(@category) do |f| %>
<%= render :partial => 'form', :locals => { :f => f } %>
  <p>
    <%= f.submit "Modyfikuj" %>
  </p>
<% end %>
<%= link_to 'Pokaz', @category %> |
<%= link_to 'Wyswietl', categories_path %>

_form file:

<%= f.error_messages %> 
<p> 
    <%= f.label :name %><br /> 
  <%= f.text_field :name %> 
</p> 
<p> 
    <%= f.label :parent_id %><br /> 
  <%= f.collection_select :parent_id, @all_categories, :id, :long_name %> 
</p>

Delete doesn't work either, in any page. All project file are in link

henio180
  • 156
  • 1
  • 8

2 Answers2

1

The method error_messages of the FormBuilder class is deprecated. You can use a custom form builder with a similar method (using something like this: f.error_messages in Rails 3.0) or you can just create a helper that implements the same thing.

In either way, you have to read the errors from your model (using model.errors) and return a string with the values formatted as you want.

Community
  • 1
  • 1
  • Have you maybe some easy tutorial how to create helper or read the errors?? – henio180 Jan 23 '14 at 13:26
  • Yup. Look at this. I didn't tested it but i think it works. https://gist.github.com/juliogarciag/8590148 –  Jan 24 '14 at 01:05
  • but my file categories_helper.rb is: `module CategoriesHelper end` Can I paste all your code replacing this? – henio180 Jan 24 '14 at 16:31
  • All my project files are hiere [link](https://github.com/henio180/rails/tree/master/rail2) – henio180 Jan 24 '14 at 16:38
1

The errors exist on the @category model, not the form builder. I would do the following:

<h1>Zmiana kategorii</h1>
<%= 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 |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <%= render :partial => 'form', :locals => { :f => f } %>
  <p>
    <%= f.submit "Modyfikuj" %>
  </p>
<% end %>
<%= link_to 'Pokaz', @category %> |
<%= link_to 'Wyswietl', categories_path %>

The <% if @category.errors.any? %> code is what the Rails scaffold does by default...

Jason Noble
  • 3,756
  • 19
  • 21
  • I updated my answer above, you need <%= on the form_for line, not <%. – Jason Noble Jan 25 '14 at 00:55
  • All time the same problem. All kode files are hiere [link](https://github.com/henio180/rails/tree/master/rail2) without your kode beacue it doesn't work. Could you look at this? – henio180 Jan 25 '14 at 12:38
  • Opened pull request to show you it works... https://github.com/henio180/rails/pull/1 – Jason Noble Jan 27 '14 at 20:49
  • Thank you so much. It's workink now. And one more question: Could you tell me why delete doesn't work?? – henio180 Jan 27 '14 at 21:31