新建的时候是这样 :
点击保存之后:
每次点击更新会自动加上空格:
这是什么原因?
它提交的时候 [["text", "ruby\r\ndef hello\r\nputs \"hello world!\"\r\nend\r\n
"] 就是这样,不知道在哪里修改
假设那些 code 在 params 里的 key 是 text,就用
article_params[:text].gsub!(/\r\n?/, "\n")
如果解决了的话请在 stackoverflow 上选一下,谢谢。
#14 楼 @cosmosz params[:article].permit(:title, :text, :node_id, :article_img).gsub!(/\r\n?/, "\n")
报错了
undefined method `gsub!' for #ActionController::Parameters:0x007f9ab51dc0c0
#18 楼 @cosmosz @article = current_user.articles.build(article_params[:text].gsub!(/\r\n?/, "\n"))
def article_params params[:article].permit(:title, :text, :node_id, :article_img) end
"article"=>{"node_id"=>"1", "title"=>"hello", "text"=>"ruby\r\ndef hello\r\n puts \"hello world!\"\r\nend\r\n
"}, "commit"=>"Save"}
报错了: When assigning attributes, you must pass a hash as an argument.
current_user.articles.build(article_params[:article][:text].gsub!(/\r\n?/, "\n"))
或者
current_user.articles.build(article_params['article']['text'].gsub!(/\r\n?/, "\n"))
pm = article_params
pm[:article][:text].gsub!(/\r\n?/, "\n")
current_user.articles.build(pm)
或者
pm = article_params
pm[:article][:text] = pm[:article][:text].gsub(/\r\n?/, "\n")
current_user.articles.build(pm)
我都糊涂了 😄
log:
[["body", "world\r\n\r\nworld\r\n\r\nhello\r\nworld\r\nin\r\nhere\r\n\r\nruby\r\ndef hello\r\n puts \"hello world!\"\r\nend\r\n
"], ["updated_at", 2017-01-10 04:07:22 UTC], ["id", 2]]