Answer the question
In order to leave comments, you need to log in
How to solve the problem when using the gem FriendlyId (friendly_id) - incorrect saving of data from the form?
Decided to use the FriendlyId gem for lack of a better option to make Friendly URLs. I use it in two models (one is nested in another - inherited_resources). There was a problem.
Let's say we create two pages of the form:
localhost/catalog1/pages/about
localhost/catalog2/pages/
about each directory has pages with the same title. Page titles are created manually and can be the same.
When created, everything is saved correctly. If you try to edit this form, then saving changes to a completely different directory with the same page name.
# :show, :edit method
def show
@catalog = Catalog.friendly.find(params[:catalog_id])
@page = Page.friendly.find(params[:id])
end
def edit
@catalog = Catalog.friendly.find(params[:catalog_id])
@page = Page.friendly.find(params[:id])
end
def create
@catalog = Catalog.friendly.find(params[:catalog_id])
@page = @catalog.pages.build(page_params)
= form_for [@company, @page] do |f|
= f.label :name
= f.text_field :name, :id => 'name'
Answer the question
In order to leave comments, you need to log in
Something too much written. Here's how it works great.
routes.rb
Example::Application.routes.draw do
resources :catalogs do
resources :pages
end
end
models:
class Catalog < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: [:slugged, :finders]
has_many :pages, dependent: :destroy
end
class Page < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: [:slugged, :finders]
belongs_to :catalog
end
class PagesController < ApplicationController
inherit_resources
belongs_to :catalog
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question