Reputation: 1855
When i create a new category_item_key in the db the slug isn't being added to the slug column.
here is my coding
migration file
class AddSlugToCategoryItemKeys < ActiveRecord::Migration
def change
add_column :category_item_keys, :slug, :string
add_index :category_item_keys, :slug, unique: true
end
end
category_item_key controller
def new
@guide = Guide.friendly.find(params[:guide_id])
@category = Category.friendly.find(params[:category_id])
@key = Category.friendly.find(params[:category_id]).category_item_keys.new
end
def create
@guide = Guide.friendly.find(params[:guide_id])
@category = Category.friendly.find(params[:category_id])
@key = Category.friendly.find(params[:category_id]).category_item_keys.new(key_params)
if @key.save
flash[:info] = "Key added succesfully!"
redirect_to @guide
else
render 'new'
end
end
private
def key_params
params.require(:category_item_key).permit(:name, :slug)
end
new.html.erb
<%= form_for([@category, @key], url: category_item_keys_create_path) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name, "Key name" %>
<%= f.text_field :name %>
<%= f.submit "Next" %>
<% end %>
When creating a new guide or category friendly id works just fine, with the slug being added. But for category_item_key something is going wrong.
Maybe i'm missing something. But i cant find the problem.
Upvotes: 0
Views: 331
Reputation: 380
Just for your notice
You can rewrite to_params method in your model to generate your uniq slug
to_params
#for instance
your_column + your_column
end
Upvotes: 0
Reputation: 1855
Forgot to add extend FriendlyId
friendly_id :name, use: :slugged
into my model. once it was added my problem was solved
Upvotes: 0
Reputation: 76774
def new
@guide = Guide.find params[:guide_id]
@category = Category.find params[:category_id]
@key = @category.category_item_keys.new
end
def create
@guide = Guide.find params[:guide_id]
@category = Category.find params[:category_id]
@key = @category.category_item_keys.new key_params
if @key.save
redirect_to @guide, notice: "Key added succesfully!"
else
render 'new'
end
end
private
def key_params
params.require(:category_item_key).permit(:name)
end
--
<%= form_for [@guide, @category, @key] do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name, "Key name" %>
<%= f.text_field :name %>
<%= f.submit "Next" %>
<% end %>
If you've ran your migration (have the columns in the db), the above should work. If it doesn't, you need to post the sent params, and the response you're receiving from the server.
--
As an aside, you'll also want to look at how many levels you're nesting your routes...
Resources should never be nested more than 1 level deep.
Because you've got guides
and then categories
before you get to keys
, you'll probably want to remove the category
or guide
param:
# config/routes.rb
resources :categories do
resources :keys
end
I think the issue you have is likely that you're only passing [@category, @key]
to your form_for
. Instead, you'll need to include the @guide
as well.
Upvotes: 1