Rails 4 形式 - 没有将模型隐式转换为字符串 [英] Rails 4 form - no implicit conversion of Model into String
问题描述
添加辅助方法并编辑表单后,出现此错误
帖子中的类型错误#create显示/Users/jim/project/test/app/views/posts/_form.html.erb 其中第 2 行提出:没有将问题隐式转换为字符串提取的源代码(围绕第 2 行):12 <%= show_random(@question)[:title] %>3app/helpers/questions_helper.rb:4:in`show_random'app/views/posts/_form.html.erb:2:在`_app_views_posts__form_html_erb___4147312638633006209_2202637820'app/views/posts/new.html.erb:3:在`_app_views_posts_new_html_erb___3518261482060642021_2203102520'app/controllers/posts_controller.rb:33:in `create'要求参数:{utf8"=>✓",authenticity_token"=>MK1wiBKc8MqXsKPtvbgJWaBNAaZ7kHm7RDVC8ZYRMNc=","post"=>{"question_id"=>"1","内容"=>""},提交"=>创建帖子"}
但如果满足Post
模型的验证,则不会出现此错误.所以我猜测(不确定)在我的 Posts 控制器
的 create
操作中我的 if else 语句
有问题,但我没有不知道怎么修.
questions_helper.rb
模块 QuestionsHelperdef show_random(随机)JSON.parse(random).with_indifferent_access结尾结尾
_form.html.erb
<%= show_random(@question)[:title] %><%= form_for(@post) 做 |f|%><%= render 'shared/error_messages', object: f.object %><div><%= f.hidden_field :question_id, :value =>show_random(@question)[:id] %><br>
<div><%= f.label :content%><br><%= f.text_area :content %>
<div><%= f.submit %>
<%结束%>
posts_controller.erb
def new@post = Post.new@question = cookies[:question] ||= Question.random # random 方法使用 to_json 返回一个随机问题序列化记录结尾定义创建@post = current_user.posts.build(post_params)如果@post.saveflash[:success] = "帖子创建成功!"别的@question = Question.where(id: params[:post][:question_id]).first呈现新"结尾
感谢@sissy 的评论,我发现了问题.
我应该通过 @question = cookies[:question]
而不是 @question = Question.where(id: params[:post][:question_id]).first
在我的 posts 控制器
create
操作中感谢大家的帮助.
After adding a helper method and edit my form, I'm getting this error
TypeError in Posts#create
Showing /Users/jim/project/test/app/views/posts/_form.html.erb where line #2 raised:
no implicit conversion of Question into String
Extracted source (around line #2):
1
2 <%= show_random(@question)[:title] %>
3
app/helpers/questions_helper.rb:4:in `show_random'
app/views/posts/_form.html.erb:2:in `_app_views_posts__form_html_erb___4147312638633006209_2202637820'
app/views/posts/new.html.erb:3:in `_app_views_posts_new_html_erb___3518261482060642021_2203102520'
app/controllers/posts_controller.rb:33:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"MK1wiBKc8MqXsKPtvbgJWaBNAaZ7kHm7RDVC8ZYRMNc=",
"post"=>{"question_id"=>"1",
"content"=>""},
"commit"=>"Create Post"}
But this error won't appear if the validations of Post
model are met. So I'm guessing (not sure) there's something wrong with my if else statement
in my create
action of my Posts controller
but I don't know how to fix it.
questions_helper.rb
module QuestionsHelper
def show_random(random)
JSON.parse(random).with_indifferent_access
end
end
_form.html.erb
<%= show_random(@question)[:title] %>
<%= form_for(@post) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div>
<%= f.hidden_field :question_id, :value => show_random(@question)[:id] %><br>
</div>
<div>
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div>
<%= f.submit %>
</div>
<% end %>
posts_controller.erb
def new
@post = Post.new
@question = cookies[:question] ||= Question.random # the random method return a random question serialized record using to_json
end
def create
@post = current_user.posts.build(post_params)
if @post.save
flash[:success] = "Post created successfully!"
else
@question = Question.where(id: params[:post][:question_id]).first
render 'new'
end
I have found the problem thanks to @sissy comment.
I should pass @question = cookies[:question]
instead of @question = Question.where(id: params[:post][:question_id]).first
in my create
action of my posts controller
Thanks everyone for your help.
这篇关于Rails 4 形式 - 没有将模型隐式转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!