简单的表单和表单对象 - 一种用于新建和编辑的表单
simple form and form object - one form for new and edit
我试图找到以下问题的答案,但我失败了。
我正在使用表单对象创建 simple_form,但我不知道我应该在 simple_form 中指定什么 URL 以便为新操作和更新操作重用相同的表单.
这是我的代码:
文章控制器:
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
要在 new/edit 操作中呈现的表单:
= 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!'
文章形式:
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
当然我已经为文章指定了路线:
resources :articles
当我用 "article_path" 指定 url 时,我无法使用 "new" 操作(未指定 ID),当我输入 "articles" 时,我无法修补(没有发现行动)。我已经阅读了有关生成 url 和基于记录是新记录还是较早创建的方法的信息,我也一直在考虑渲染局部图……但我不太确定这些步骤是否正确。
您可以检查您的对象是否为新记录,因此您可以为新的或编辑的表单指定不同的 url,如下所示:
= simple_form_for @article_form,
url: (@article_form.new_record? ? your_path_for_new_record : your_path_for_edit)
do |f|
希望这对您有所帮助。祝你好运!
如果您使用 REST 网址(articles/#{id}
进行更新,articles/
进行创建):
= simple_form_for @article_form do |f|
如果 @article_form.class.name == "Article"
完全没问题。默认行为如下所示:
= simple_form_for obj, url: url_for(obj) do |f|
我试图找到以下问题的答案,但我失败了。
我正在使用表单对象创建 simple_form,但我不知道我应该在 simple_form 中指定什么 URL 以便为新操作和更新操作重用相同的表单.
这是我的代码: 文章控制器:
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
要在 new/edit 操作中呈现的表单:
= 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!'
文章形式:
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
当然我已经为文章指定了路线:
resources :articles
当我用 "article_path" 指定 url 时,我无法使用 "new" 操作(未指定 ID),当我输入 "articles" 时,我无法修补(没有发现行动)。我已经阅读了有关生成 url 和基于记录是新记录还是较早创建的方法的信息,我也一直在考虑渲染局部图……但我不太确定这些步骤是否正确。
您可以检查您的对象是否为新记录,因此您可以为新的或编辑的表单指定不同的 url,如下所示:
= simple_form_for @article_form,
url: (@article_form.new_record? ? your_path_for_new_record : your_path_for_edit)
do |f|
希望这对您有所帮助。祝你好运!
如果您使用 REST 网址(articles/#{id}
进行更新,articles/
进行创建):
= simple_form_for @article_form do |f|
如果 @article_form.class.name == "Article"
完全没问题。默认行为如下所示:
= simple_form_for obj, url: url_for(obj) do |f|