ruby-on-railsroutessimple-formsimple-form-for

simple form and form object - one form for new and edit


I was trying to find the answer for the following question, but I failed.

I am creating simple_form with Form Object, and I don't know what URL should I specify in simple_form in order to reuse the same form for new and update actions.

Here is my code: ArticlesController:

class ArticlesController < ApplicationController

...

  def new
    @article = Article.new
    @article_form = ArticleForm.new(@article)
  end

  def create
    @article = Article.new
    @article_form = ArticleForm.new(@article)
    if @article_form.save(article_params)
      flash[:notice] = 'You have added a new article.'
      redirect_to @article_form.article
    else
      flash[:danger] = 'Failed to add new article.'
      render :new
    end
  end

  def edit
    @article = Article.find(params[:id])
    @article_form = ArticleForm.new(@article)
    # binding.pry
  end

  def update
    @article = Article.find(params[:id])
    @article_form = ArticleForm.new(@article)
    if @article_form.save(article_params)
      flash[:success] = 'Article updated'
      TagServices::OrphanTagDestroyer.call
      redirect_to @article_form.article
    else
      flash[:error] = 'Failed to update the article'
      render :edit
    end
  end
end

Form to be rendered in new/edit actions:

= simple_form_for @article_form, url: article_path do |f|
  = f.input :title, label: "Article title:"
  = f.input :body, label: "Body of the article:", as: :text, input_html: { :style => 'height: 200px' }
  = f.input :tags_string, label: "Tags:", input_html: { value: f.object.all_tags }
  = f.button :submit, 'Send!'

ArticleForm:

class ArticleForm
  include ActiveModel::Model
  delegate :title, :body, :author_id, :tags, :id, :persisted?, to: :article
  attr_accessor :article
  validates_presence_of :title, :body, :tags
  validate :validate_prohibited_words

  def initialize(article)
    @article = article
  end

  def save(article_params)
    @article.update_attributes(article_params.slice('title', 'body', 'author_id'))
    @article.tags = tag_list(article_params[:tags_string])

    if valid?
      @article.save!
      true
    else
      false
    end
  end

  def tag_list(tags_string)
    tags_string.scan(/\w+/)
               .map(&:downcase)
               .uniq
               .map { |name| Tag.find_or_create_by(name: name) }
  end

  def all_tags
    return tags.collect(&:name).join(', ') if tags.present?
    ''
  end
end

Of course I have specified routes for article:

  resources :articles

When I specify url with "article_path" I can't use "new" action (no id specified), when I pute "articles" I can't Patch (no aciton found). I have read about generating url and method based on whether record is new or was created earlier, I've been thinking about rendering partials as well.. but I am not quite sure if these are the correct steps to take.


Solution

  • You can check if your object is a new record, so you can specify different url's for a new or edit form, like this:

    = simple_form_for @article_form, 
    url: (@article_form.new_record? ? your_path_for_new_record : your_path_for_edit) 
    do |f|
    

    Hope this helps. Good luck!