Problems using named models and controllers in Rails

I am developing a Ruby on Rails application and I am trying to streamline my code a little better. Part of the app is the knowledge base, which is organized into categories and articles are pretty standard stuff.

This is how I set everything up (at least what I think is important):

routes.rb

namespace :knowledgebase do
  resources :categories
  resources :articles
end

      

Models / Knowledge Base /article.rb

class Knowledgebase::Article < ActiveRecord::Base
  self.table_name = 'knowledgebase_articles'
  belongs_to :category, class_name: 'Knowledgebase::Category', foreign_key: 'knowledgebase_category_id'
end

      

Models / Knowledge Base /category.rb

class Knowledgebase::Category < ActiveRecord::Base
  self.table_name = 'knowledgebase_categories'
  has_many :articles, class_name: 'Knowledgebase::Article'
end

      

Controllers / Knowledge base / articles _controller.rb

class Knowledgebase::CategoriesController < ApplicationController
  load_and_authorize_resource
  before_action :authenticate_user!
  before_action :set_category, only: [:show, :edit, :update, :destroy]

  def new
    @category = ::Knowledgebase::Category.new
  end

  def create
    @category = ::Knowledgebase::Category.new(category_params)

    if @category.save
      redirect_to knowledgebase_categories_url, notice: '...'
    else
      render :new
    end
  end

  # ...

  private
    def set_category
      @category = ::Knowledgebase::Category.find(params[:id])
    end

    def category_params
      params.require(:category).permit(:title)
    end
end

      

view / knowledge base / categories / _form.html.erb

<%= form_for @category do |form| %>
  <%= form.text_field :title
  <%= form.submit %>
<% end %>

      

My problem: when I try to submit a form (new or right), I get an error:

param is missing or the value is empty: category

      

When I set up the form like this: <%= form_for ([:knowledgebase, @category]) do |form| %>

I get the following error:

undefined method `knowledgebase_knowledgebase_categories_path' for #<#<Class:0x007fecd7a78908>:0x007fecd707ad20>

      

The same goes for articles. I am using Rails 4.2.3.

If anyone can help me or point me in the right direction, I would be very grateful - I've searched Google for three days now, I've tried everything I can think of.

Update 1

This is the generated parameter log:

{"utf8"=>"✓",
"authenticity_token"=>"fjOs...VA==",
"commit"=>"Category erstellen",
"knowledgebase_category"=>{"title"=>"Test Title"}}

      

+3


source to share


1 answer


When you look at params hash it has knowledgebase_category

not category

. So try changing category_params

to below

def category_params
  params.require(:knowledgebase_category).permit(:title)
end

      



Advice:

Always look at the generated parameters in the log . It really helps to find a solution to most errors.

+4


source







All Articles