jeudi 24 novembre 2016

Undefined method `embroderies' for nil:NilClass for submit create form

I set up submit form to add new data, but I got this error undefined methodembroderies' for nil:NilClass.` I was following the rails guide for that but I have no idea how can I correct my code.

Here is my controller.

class EmbroderiesController < ApplicationController
  def index
  end

  def show
    @embroderies = Embrodery.find(params[:id])
  end

  def new
    @embrodery = Embrodery.new
  end

  def create
    @region = Region.find(params[:region_id])
    @embrodery = @region.embroderies.create(comment_params)

    if @embrodery.save
      redirect_to @embrodery
    else
      render 'new'
    end
  end

  private
  def embrodery_params
    params.require(:embrodery).permit(:name, :image)
  end
end

The view

<div class = "container">
  <div class = "row">
    <div class = "col-xs-12 add-wrap-div">
      <p>Add new model</p>
      <%= form_for ([@region, @region.embroderies.build]) do |f| %>
        <p>
          <%= f.label :name %><br>
          <%= f.text_field :name %>
        </p>

        <p>
          <%= f.label :image %><br>
          <%= f.file_field :image %>
        </p>
        <br>
        <br>
        <p>
          <%= f.submit %>
        </p>
      <% end %>
    </div>
  </div>
</div>

and the routes

               Prefix Verb   URI Pattern                                        Controller#Action
                 root GET    /                                                  home#index
           home_index GET    /home/index(.:format)                              home#index
           home_about GET    /home/about(.:format)                              home#about
   region_embroderies GET    /regions/:region_id/embroderies(.:format)          embroderies#index
                      POST   /regions/:region_id/embroderies(.:format)          embroderies#create
 new_region_embrodery GET    /regions/:region_id/embroderies/new(.:format)      embroderies#new
edit_region_embrodery GET    /regions/:region_id/embroderies/:id/edit(.:format) embroderies#edit
     region_embrodery GET    /regions/:region_id/embroderies/:id(.:format)      embroderies#show
                      PATCH  /regions/:region_id/embroderies/:id(.:format)      embroderies#update
                      PUT    /regions/:region_id/embroderies/:id(.:format)      embroderies#update
                      DELETE /regions/:region_id/embroderies/:id(.:format)      embroderies#destroy
              regions GET    /regions(.:format)                                 regions#index
                      POST   /regions(.:format)                                 regions#create
           new_region GET    /regions/new(.:format)                             regions#new
          edit_region GET    /regions/:id/edit(.:format)                        regions#edit
               region GET    /regions/:id(.:format)                             regions#show
                      PATCH  /regions/:id(.:format)                             regions#update
                      PUT    /regions/:id(.:format)                             regions#update
                      DELETE /regions/:id(.:format)                             regions#destroy

schema

ActiveRecord::Schema.define(version: 20161115165517) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "embroderies", force: :cascade do |t|
    t.string   "name"
    t.string   "image"
    t.integer  "region_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "embroderies", ["region_id"], name: "index_embroderies_on_region_id", using: :btree

  create_table "regions", force: :cascade do |t|
    t.string   "name"
    t.string   "image"
    t.text     "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  add_foreign_key "embroderies", "regions"
end

Aucun commentaire:

Enregistrer un commentaire