Usa*_*Man 4 ruby-on-rails activeadmin
我有一种情况,我需要在 activeadmin 中覆盖 create。我自动填充字段,如果数据已经存在,它应该更新,否则创建。这是我的创建方法:
def create
id = params[:company].dig(:id)
if id.present?
@company = Company.find(id)
if @company.update(permitted_params[:company])
redirect_to resource_url
flash[:notice] = 'Company created successfully'
else
#add errors to semantic errors
end
else
new_permitted_params = permitted_params[:company].except(:id)
@company = Company.new(new_permitted_params)
@company.save
if @company.errors.any?
#add this to semantic errors so that activeadmin handles and displays the errors
end
end
end
Run Code Online (Sandbox Code Playgroud)
我想显示违反验证的错误,以便用户知道他/她是否输入了无效条目。
我找到了这个,但它看起来更像是一种解决方法而不是解决方案。请帮我解决这个问题。
提前致谢。
我在命名new.html.arb并添加insert_tag renderer_for(:new)到其中的视图中创建了一个新的 HTML 文件。在那之后我所做的就是
if @company.errors.any?
render 'new'
end
Run Code Online (Sandbox Code Playgroud)
我是通过查看 activeadmin 的默认行为发现的。我希望这可以帮助其他想要做类似事情的人。这是我得到的结果,也是我所要求的。
| 归档时间: |
|
| 查看次数: |
2428 次 |
| 最近记录: |