Rails 4 form - no implicit conversion of Model into String -
after adding helper method , edit form, i'm getting error
typeerror in posts#create showing /users/jim/project/test/app/views/posts/_form.html.erb line #2 raised: no implicit conversion of question 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 error won't appear if validations of post
model met. i'm guessing (not sure) there's wrong if else statement
in create
action of posts controller
don't know how 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) |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 # random method return 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 problem @sissy comment.
i should pass @question = cookies[:question]
instead of @question = question.where(id: params[:post][:question_id]).first
in create
action of posts controller
thanks help.
Comments
Post a Comment