我试图找到以下问题的答案,但我失败了。
我正在使用Form Object创建simple_form,并且我不知道我应该在simple_form中指定哪个URL,以便为新的和更新操作重用相同的表单。
这是我的代码: 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
要在新/编辑操作中呈现的表单:
= 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
当然我已经为文章指定了路线:
resources :articles
当我用&#34; article_path&#34;指定url时我无法使用&#34; new&#34;动作(没有指定id),当我把文章&#34;文章&#34;我无法补丁(没有找到aciton)。我已经阅读了基于记录是新的还是之前创建的关于生成url和方法的内容,我一直在考虑渲染部分内容......但我不确定这些是否是正确的步骤。
答案 0 :(得分:1)
您可以检查您的对象是否是新记录,因此您可以为新表单或编辑表单指定不同的URL,如下所示:
= simple_form_for @article_form,
url: (@article_form.new_record? ? your_path_for_new_record : your_path_for_edit)
do |f|
希望这会有所帮助。祝你好运!
答案 1 :(得分:0)
如果您使用REST网址(TID:%{SPACE}\[%{INT:tenant_id}\]%{SPACE}\[]%{SPACE}\[%{TIMESTAMP_ISO8601:hit_timestamp}\]%{SPACE}%{LOGLEVEL:level}%{SPACE}{%{JAVACLASS:java_class}}%{SPACE}-%{SPACE}WSO2Status%{SPACE}=%{SPACE}%{WORD:Status},%{SPACE}APIE2ETime%{SPACE}=%{SPACE}%{GREEDYDATA:context},%{SPACE}X-External-CorrelationId%{SPACE}=%{SPACE}%{WORD:CorrelationId},%{SPACE}IN%{SPACE}=%{SPACE}%{TIMESTAMP_ISO8601:in_timestamp},%{SPACE}OUT%{SPACE}=%{SPACE}%{TIMESTAMP_ISO8601:out_timestamp},%{SPACE}HTTP_SC%{SPACE}=%{SPACE}%{INT:http_sc},%{SPACE}Channel%{SPACE}=%{SPACE}%{WORD:channel},%{SPACE}Http_Method%{SPACE}=%{SPACE}%{WORD:http_method},%{SPACE}RemoteAddress%{SPACE}=%{SPACE}%{IP:remoteaddress}%{SPACE}{%{JAVACLASS:java_class2}}
用于更新,articles/#{id}
用于创建):
articles/
如果 = simple_form_for @article_form do |f|
很好。默认行为如下:
@article_form.class.name == "Article"