Rails Blog 保存文章,内容会自动加上空格的问题

F_ · 2017年01月10日 · 最后由 F_ 回复于 2017年01月10日 · 1315 次阅读

新建的时候是这样 :

点击保存之后:

每次点击更新会自动加上空格:

这是什么原因?

添加的有 tab?

2 楼 已删除
3 楼 已删除

#1 楼 @sec 每次 点击 update 会自动加上 tab,不知道这什么问题

#1 楼 @sec 点击保存,也会出现 空格。

看 log 里面这个 update/保存提交的是什么啊

#6 楼 @cosmosz oh, [["text", "ruby\r\ndef hello\r\nputs \"hello world!\"\r\nend\r\n"]

8 楼 已删除
9 楼 已删除

在 stack overflow 被我发现了,哈哈。在那里回答了。

#10 楼 @cosmosz 嘿,哥们儿

没找到地方啊,

@article = current_user.articles.build(article_params).gsub!(/\r\n?/, "\n")

貌似,替换不了

它提交的时候 [["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 上选一下,谢谢。

15 楼 已删除

#14 楼 @cosmosz params[:article].permit(:title, :text, :node_id, :article_img).gsub!(/\r\n?/, "\n")

😀 报错了

undefined method `gsub!' for #ActionController::Parameters:0x007f9ab51dc0c0

。。 current_user.articles.build(article_params[:text].gsub!(/\r\n?/, "\n"))

是把 request 里的 params 修改了,不是修改 permitted_params

#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.

20 楼 已删除

current_user.articles.build(article_params[:article][:text].gsub!(/\r\n?/, "\n")) 或者 current_user.articles.build(article_params['article']['text'].gsub!(/\r\n?/, "\n"))

#21 楼 @cosmosz undefined method `[]' for nil:NilClass

保存,不了了 😅

#16 楼 @F_ 2 个 都显示的是 undefined method `[]' for nil:NilClass

是模版输出的问题,用的什么模版?

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)

我都糊涂了 😄

#26 楼 @cosmosz oh, thanks,重新做一遍,用 erb,嘿 stack overflow 怎么选? 😓

#27 楼 @F_ 点下 accept answer 就可以了。谢谢

#29 楼 @F_ ok 了。谢谢,希望帮助到了你。

32 楼 已删除

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]]

#30 楼 @cosmosz 是 那个 haml 模版的问题。

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]]

F_ 关闭了讨论。 01月10日 21:36
需要 登录 后方可回复, 如果你还没有账号请 注册新账号