laman
laman

Reputation: 562

Rails 4 form - no implicit conversion of Model into String

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

Upvotes: 2

Views: 1859

Answers (2)

laman
laman

Reputation: 562

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.

Upvotes: 2

Richard Peck
Richard Peck

Reputation: 76774

Your helper seems suspicious to me -- you're passing a Ruby object and trying to parse it as JSON

I think the @sissy comment is spot on - I think it's an issue with your passing the object at different stages. And considering you're getting the error when you receive errors on Post.save, sissy will likely be correct

Further, I'd imagine the fundamental problem would be this:

@question ->

     !ruby/object:Question
      attributes:
         -- title: x
         -- created_at: y
         -- updated_at: z

If you're parsing JSON, surely this would try to translate !ruby/object:Question into an array element, meaning it needs to be a String in order for it to work


Fix

I would fix it by changing the helper to this:

#app/helpers/questions_helper.rb
def show_random(post)
   returned_post = Post.random_question(post)
   returned_post.title
end

#app/models/post.rb
Class Post < ActiveRecord::Base
    def self.random_question(post)
        joins(:question).where(#your conditions here)
    end
end

You could then use:

<%= show_random(@post) %>

Upvotes: 0

Related Questions