G
G
gejarufu2014-04-16 21:08:12
Ruby on Rails
gejarufu, 2014-04-16 21:08:12

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

# :create method
def create
  @catalog = Catalog.friendly.find(params[:catalog_id])
  @page = @catalog.pages.build(page_params)

#_form.html.slim
= form_for [@company, @page] do |f|
  = f.label :name
  = f.text_field :name, :id => 'name'

Maybe it's all about the show and edit methods.
Has anyone experienced this? How can the issue be resolved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Fesenko, 2014-04-16
@gejarufu

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 question

Ask a Question

731 491 924 answers to any question