在Rails中更新嵌套的fields_for和collection_select(Updating nested fields_for and collection_select in Rails)
真的难倒在这里。 我正在尝试让我的表单更新
edit
表单上的类别。 问题是,除了类别之外,我提交的表格中的所有内容都会更新。 它最终会插入所选择的新类别,就像它通过create
方法而不是update
方法一样,所以当提交后再次显示编辑表单时,它会使类别的字段加倍。 每次提交后1,然后是2,然后是4,然后是8等。 请帮助任何人。 欣赏它。视图/ blog_posts / edit.html.erb
<div class="col-md-6 col-md-offset-3 blog-submit"> <%= form_for @blog_post do |b| %> <%= b.label :title %> <%= b.text_field :title %><br> <%= b.fields_for :categorizations do |cat| %> <%= cat.label :category_name, "Category 1" %> <%= cat.collection_select(:category_id, Category.all, :id, :category_name, {blank: "Select Category"}) %> <%= link_to "Add Categories", new_category_path %> <br> <% end %> <%= b.submit "Submit", class: "btn btn-primary" %> <% end %> </div>
Blog_post控制器:
class BlogPostsController < ApplicationController protect_from_forgery before_action :authenticate_admin!, only: [:new, :edit] def index @blog_posts = BlogPost.order(id: :desc) end def new @blog_post = BlogPost.new @blog_post.categorizations.build.build_category @blog_post.categories.build end def edit @blog_post = BlogPost.find(params[:id]) end def create @blog_post = BlogPost.new(blog_post_params) respond_to do |format| if @blog_post.save format.html { redirect_to @blog_post, notice: 'Your blog was submitted successfully' } format.json { render :show, status: :created, location: @blog_post } else format.html { render :new } format.json { render json: @blog_post.errors, status: :unprocessable_entity } end end puts @blog_post.errors.inspect end def update @blog_post = BlogPost.find(params[:id]) if @blog_post.update_attributes(blog_post_params) render 'show' else render 'edit' end end def show @blog_post = BlogPost.find(params[:id]) end private def blog_post_params params.require(:blog_post).permit(:title, :content, :posted_by, :comments, :blog_pic, {categorizations_attributes: [:category_id, :category_name]}) end end
楷模:
class BlogPost < ApplicationRecord has_many :categorizations has_many :categories, :through => :categorizations accepts_nested_attributes_for :categorizations has_many :comments mount_uploader :blog_pic, BlogPicUploader end class Categorization < ApplicationRecord belongs_to :blog_post belongs_to :category end class Category < ApplicationRecord has_many :categorizations has_many :blog_posts, :through => :categorizations end
Really stumped here. I'm trying to get my form to update the categories on the
edit
form. Problem is, everything in my form updates when submitted except the categories. It ends up inserting the new category chosen like it's going through thecreate
method instead of theupdate
method, so when the edit form is shown again after submission, it keeps doubling the fields of categories. 1, then 2, then 4, then 8, etc. after each submission. Please please help anyone. Appreciate it.views/blog_posts/edit.html.erb
<div class="col-md-6 col-md-offset-3 blog-submit"> <%= form_for @blog_post do |b| %> <%= b.label :title %> <%= b.text_field :title %><br> <%= b.fields_for :categorizations do |cat| %> <%= cat.label :category_name, "Category 1" %> <%= cat.collection_select(:category_id, Category.all, :id, :category_name, {blank: "Select Category"}) %> <%= link_to "Add Categories", new_category_path %> <br> <% end %> <%= b.submit "Submit", class: "btn btn-primary" %> <% end %> </div>
Blog_post controller:
class BlogPostsController < ApplicationController protect_from_forgery before_action :authenticate_admin!, only: [:new, :edit] def index @blog_posts = BlogPost.order(id: :desc) end def new @blog_post = BlogPost.new @blog_post.categorizations.build.build_category @blog_post.categories.build end def edit @blog_post = BlogPost.find(params[:id]) end def create @blog_post = BlogPost.new(blog_post_params) respond_to do |format| if @blog_post.save format.html { redirect_to @blog_post, notice: 'Your blog was submitted successfully' } format.json { render :show, status: :created, location: @blog_post } else format.html { render :new } format.json { render json: @blog_post.errors, status: :unprocessable_entity } end end puts @blog_post.errors.inspect end def update @blog_post = BlogPost.find(params[:id]) if @blog_post.update_attributes(blog_post_params) render 'show' else render 'edit' end end def show @blog_post = BlogPost.find(params[:id]) end private def blog_post_params params.require(:blog_post).permit(:title, :content, :posted_by, :comments, :blog_pic, {categorizations_attributes: [:category_id, :category_name]}) end end
models:
class BlogPost < ApplicationRecord has_many :categorizations has_many :categories, :through => :categorizations accepts_nested_attributes_for :categorizations has_many :comments mount_uploader :blog_pic, BlogPicUploader end class Categorization < ApplicationRecord belongs_to :blog_post belongs_to :category end class Category < ApplicationRecord has_many :categorizations has_many :blog_posts, :through => :categorizations end
原文:
最满意答案
我认为你并没有指向同一个密钥库。 你可以添加keystore作为keytool命令的参数吗? 我的意思是:
keytool -list -storepass changeit -keystore ../keystore
默认情况下,使用的密钥存储是.keystore,并且它不等于您尝试导入证书的密钥存储(../keystore)。
如果列出文件夹(/ web / home / wb10d1 / companyCA?)的内容,则会找到一个名为
.keystore
的文件,但如果列出父文件夹的内容,则会找到证书所在的keystore
别名tomcat确实存在。I think that you are not pointing to the same keystore. Can you add the keystore to open as a parameter to the keytool command? I mean:
keytool -list -storepass changeit -keystore ../keystore
By default, the key store used is .keystore and it is not equal as the keystore where you were trying to import the certificate (../keystore).
if you list the contents of your folder (/web/home/wb10d1/companyCA?), you will find a file called
.keystore
, but if you list the contents of the parent folder, you will find thekeystore
where the certificate with the alias tomcat really exists.
相关问答
更多-
Java Keytool:添加自签名证书而不提示用户是/否(Java Keytool : Add Self Signed Certificate without promting Yes/No from User)[2022-03-09]
是的,有一个选项-noprompt不会提示来自用户的输入Y / N。 通过在终端上运行命令keytool -importcert -help可以找到更多的细节。 Yes, there is an option -noprompt that doesn't prompt the input Y/N from user. More details can found by running command keytool -importcert -help on terminal. -
我不知道这是否可行,但是您可以实施您自己的TrustManager以允许此连接或此CA. 这里是基础知识。 I don't know if that is possible, but you could implement your own TrustManager to allow this connection or this CA. Here are the basics.
-
如果你需要做的只是创建一对自签名证书......我可能会提供帮助。 在Microsoft Windows机器上: 创建一个空目录并在其中保存以下脚本(GenTestCerts.ps1)。 编辑脚本并将Alias值(和其他变量)更改为您需要的任何值。 执行脚本。 将服务器(tomcat.server.net.p12)证书复制到服务器期望的位置。 将Trust Store(truststore.p12)复制到服务器期望的位置。 在您的Windows密钥存储区中安装admin(tomcat-admin.p12)证 ...
-
如果您没有以管理员模式运行命令提示符,则可能会发生这种情况。 如果使用windows7,可以运行,键入cmd,然后按Ctrl + Shift +进入。 这将以管理员模式打开命令提示符。 如果没有,您还可以开始 - >所有程序 - >配件 - >右键单击命令提示符,并说出以管理员身份运行。 This could happen if you are not running the command prompt in administrator mode. If you are using windows 7, ...
-
使用java keytool,密钥库证书的最小有效期可以是1天。 编辑:看起来有一个-startdate的选项,因为@ shyam0191已经回答了。 所以,你不能(更正:你实际上可以)生成一个有过去日期的证书。 我建议使用以下命令,该命令将生成一个有效期为1天的证书,第二天您将能够使用它进行测试: keytool -selfcert -alias Test -genkey -keystore myStore.keystore -keyalg RSA -validity 1 或使用@ shyam0191的 ...
-
这是java jdk的问题。 我已经使用java 1.8和jdk1.7.0_79进行了验证,它对我起作用了fyn。 更改您的java jdk版本并验证。 如需进一步说明,请参阅此链接。 https://bugs.eclipse.org/bugs/show_bug.cgi?id=398644 It is the problem with java jdk. I have verified using java 1.8 and jdk1.7.0_79 it is working fyn for me. Chan ...
-
没有办法跳过证书上的任何内容。 如果Issuer Alternative Name为空白,则生成该证书的人生成错误。 如果它是自签名证书,则颁发者备用名称和主题备用名称是相同的值。 如果它由任何CA签名,则CA的主题备用名称将放入最终用户的颁发者备用名称中。 您可能需要询问他们是否有正确的CA证书。 There is no way to skip anything that's on a certificate. If the Issuer Alternative Name is blank, whoeve ...
-
如何通过keytool命令删除已导入的证书/别名?(How to delete already import certificate/alias by keytool command?)[2022-06-02]
您可以使用KeyStore Explorer检查是否存在并轻松管理您的证书, KeyStore Explorer下载链接 只需使用资源管理器打开您的密钥库文件 ,执行您想要的操作并将其保存回来。 You can make use of KeyStore Explorer to check if exists and manage your certs easily, KeyStore Explorer Download Link Just open your keystore file with the e ... -
我认为你并没有指向同一个密钥库。 你可以添加keystore作为keytool命令的参数吗? 我的意思是: keytool -list -storepass changeit -keystore ../keystore 默认情况下,使用的密钥存储是.keystore,并且它不等于您尝试导入证书的密钥存储(../keystore)。 如果列出文件夹(/ web / home / wb10d1 / companyCA?)的内容,则会找到一个名为.keystore的文件,但如果列出父文件夹的内容,则会找到证书所在 ...
-
您可以使用-export选项执行此操作。 请参阅文档。 但是,自签名证书完全是浪费时间,特别是现在可以免费获得SSL证书。 得到它签名。 这也记录在案。 You can do this with the -export option. See the documentation. However self-signed certificates are a complete waste of time, especially now that SSL certificates can be obtained ...