简体   繁体   中英

Ruby On Rails don't delete and update elements

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

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.

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? %> <% if @category.errors.any? %> code is what the Rails scaffold does by default...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM