'I am trying to create a post in instagram clone in ruby and rails but it throws an error "no implicit conversion of Integer into string"
This is the Post Model
class Post < ApplicationRecord
mount_uploader :image, ImageUploader
scope :active, -> { where active: true }
end
This is the Post Controlller
class PostsController < ApplicationController
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to dashboard_path, flash: { success: "Post Created Successfuly" }
else
redirect_to new_post_path, flash: { danger: "Post was not saved Successfuly" }
end
end
def show
end
private
def post_params
params.require(:post).permit(:image, :image_cache)
end
end
This the Database Table for create post
class CreatePosts < ActiveRecord::Migration[7.0]
def change
create_table :posts do |t|
t.string :image
t.boolean :active
t.references :account
t.timestamps
end
end
end
This the Request created when save post
Parameters:
{"authenticity_token"=>"[FILTERED]",
"post"=>
{"image"=>
#<ActionDispatch::Http::UploadedFile:0x00007fc4a96c25a8
@content_type="image/png",
@headers="Content-Disposition: form-data; name=\"post[image]\"; filename=\"image(2).png\"\r\n" + "Content-Type: image/png\r\n",
@original_filename="image(2).png",
@tempfile=#<File:/tmp/RackMultipart20220421-27384-m946e8.png>>,
"image_cache"=>""},
This is the Form to create post on front end
<%= form_for @post, html: { multipart: true } do |f|%>
<%= f.file_field :image, class: "form-control" %>
<%= f.hidden_field :image_cache %>
<%= f.submit "Save Post", class: "btn btn-success"%>
<% end %>
TypeError in PostsController#create no implicit conversion of Integer into String
def create
@post = Post.new(post_params) // This line have error
if @post.save
redirect_to dashboard_path, flash: { success: "Post Created Successfuly" }
Backtrace
["/home/hexcoder/Desktop/instagram-clone/app/controllers/posts_controller.rb:8:in `backtrace'",
"/home/hexcoder/Desktop/instagram-clone/app/controllers/posts_controller.rb:8:in `create'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/web-console-4.2.0/lib/web_console/evaluator.rb:24:in `eval'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/web-console-4.2.0/lib/web_console/evaluator.rb:24:in `eval'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/web-console-4.2.0/lib/web_console/session.rb:57:in `eval'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/web-console-4.2.0/lib/web_console/middleware.rb:104:in `block in update_repl_session'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/web-console-4.2.0/lib/web_console/middleware.rb:69:in `block in json_response_with_session'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/web-console-4.2.0/lib/web_console/middleware.rb:61:in `json_response'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/web-console-4.2.0/lib/web_console/middleware.rb:69:in `json_response_with_session'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/web-console-4.2.0/lib/web_console/middleware.rb:102:in `update_repl_session'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/web-console-4.2.0/lib/web_console/middleware.rb:22:in `block in call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/web-console-4.2.0/lib/web_console/middleware.rb:17:in `catch'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/web-console-4.2.0/lib/web_console/middleware.rb:17:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/actionpack-7.0.2.3/lib/action_dispatch/middleware/show_exceptions.rb:26:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/railties-7.0.2.3/lib/rails/rack/logger.rb:36:in `call_app'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/railties-7.0.2.3/lib/rails/rack/logger.rb:25:in `block in call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/activesupport-7.0.2.3/lib/active_support/tagged_logging.rb:99:in `block in tagged'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/activesupport-7.0.2.3/lib/active_support/tagged_logging.rb:37:in `tagged'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/activesupport-7.0.2.3/lib/active_support/tagged_logging.rb:99:in `tagged'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/railties-7.0.2.3/lib/rails/rack/logger.rb:25:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/sprockets-rails-3.4.2/lib/sprockets/rails/quiet_assets.rb:13:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/actionpack-7.0.2.3/lib/action_dispatch/middleware/remote_ip.rb:93:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/actionpack-7.0.2.3/lib/action_dispatch/middleware/request_id.rb:26:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/method_override.rb:24:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/runtime.rb:22:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/activesupport-7.0.2.3/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/actionpack-7.0.2.3/lib/action_dispatch/middleware/server_timing.rb:20:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/actionpack-7.0.2.3/lib/action_dispatch/middleware/executor.rb:14:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/actionpack-7.0.2.3/lib/action_dispatch/middleware/static.rb:23:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/sendfile.rb:110:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/actionpack-7.0.2.3/lib/action_dispatch/middleware/host_authorization.rb:137:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/railties-7.0.2.3/lib/rails/engine.rb:530:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/puma-5.6.4/lib/puma/configuration.rb:252:in `call'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/puma-5.6.4/lib/puma/request.rb:77:in `block in handle_request'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/puma-5.6.4/lib/puma/thread_pool.rb:340:in `with_force_shutdown'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/puma-5.6.4/lib/puma/request.rb:76:in `handle_request'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/puma-5.6.4/lib/puma/server.rb:441:in `process_client'",
"/home/hexcoder/.rvm/gems/ruby-3.0.0/gems/puma-5.6.4/lib/puma/thread_pool.rb:147:in `block in spawn_thread'"]
It will be very helpful if someone could answer this question and tell me where i am doing something wrong.
Solution 1:[1]
Your Post is supposed to be linked to an Account
create_table :posts do |t|
#...
t.references :account
# ..
end
I would say there is an error during the save because account_id is nil or empty. It's the biggest mistake I see here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | brcebn |
