Arthur Truong

Joined

3,000 Experience
28 Lessons Completed
0 Questions Solved

Activity

Hey, I discovered a problem with Rails app. When I run rails db:seed, I generate 10 posts and store them in the database. I also have an API where I pull data as JSON, which gets parsed and data is basically stored in the DB as a post. I run Post.reindex on both of them, however, despite reindexing, the seed.rb generated posts don't show up in the correct order and are always shown first instead of chronological order with the API-derived posts. How do I fix this?

In my posts_controller.rb, I sort the posts by created_at:
@posts = Post.search(query, page: params[:page], per_page: 10, order: { created_at: :asc })

My Rake Task:

namespace :api do
  desc "get jobs from job aggregator api"
  task get_posts: :environment do

      url = *url here*
      req = Net::HTTP::Get.new(url.to_s)
      res = Net::HTTP.start(url.host, url.port) {|http|
        http.request(req)
      }
      response = JSON.parse(res.body)

      Post.where(api: true).delete_all

      response.each do |post| 
        Post.create! do |f|
          f.company = post["company"]
          f.company_logo = post["company_logo"]
          f.description = post["description"]
          f.how_to_apply = post["how_to_apply"]
          f.title = post["title"]
          f.url = post["url"]
          f.api_created_at = post["created_at"]
          f.api = true
          f.user_id = 1 #user is required
          f.published_at = Time.parse(post["created_at"])
        end
      end
      Post.reindex
      p "#{response.count} posts pulled"
  end

end

And my seeds.rb file:

if Rails.env.development? 
  require 'faker'

  @post = Post.create(
    :company => "Tesla", 
    :title => "Tesla Web Engineer Required Immediately!", 
    :url => "http://www.tesla.com", 
    :content => "<div>This is a demo post</div>", 
    :tag_list => "react, node, expressjs",
    :user_id => 1,
    :published_at => DateTime.now.utc,
    :paymentstatus => true
  )

  @post.save!

  20.times do 
    @post2 = Post.create(
      :company => Faker::Company.name, 
      :title => Faker::Job.title, 
      :url => Faker::Company.logo, 
      :content => Faker::Quotes::Shakespeare.hamlet_quote, 
      :tag_list => "#{Faker::Job.field}, #{Faker::Job.field}, node, expressjs",
      :user_id => 1,
      :published_at => DateTime.now.utc,
      :paymentstatus => true

    )

    @post2.save!
  end

  p "done!"
end

I'm new to rails and I just realised that activestorage doesn't support CDN's..

I'm trying to figure a way how to do multi-image uploads with Shrine using stimulus but I can't seem to find any guides how to do this?

Can someone point me to the right direction

Posted in How do I do multiple uploads in Active Storage?

Hi there,

I am following this tutorial to do multiple file uploads with DropboxJS but I don't know how to get multiple images belonging to a model to show in the 'show' page. Only one image shows when I upload multiple images, despite doing a .each in the@post.feature_image array.
Tutorial here:
https://web.archive.org/web/20191223022642/https://web-crunch.com/rails-drag-drop-active-storage-stimulus-dropzone/

Example Repo: https://github.com/justalever/drag_and_drop_active_storage

Changes I have made to get it working:
_form.html.erb

data-dropzone-max-files="10"

posts/show.html.erb

 <%= link_to @post do %>
    <% @post.feature_image.each do |image| %>
      <%= image_tag image %>
    <% end %>
  <% end %>

models/post.rb

class Post < ApplicationRecord
  belongs_to :user
  has_many_attached :feature_image
end

Posted in Doing multiple uploads with Active Storage

Hi there,

I am following this tutorial to do multiple file uploads with DropboxJS but I don't know how to get multiple images belonging to a model to show in the 'show' page. Only one image shows when I upload multiple images, despite doing a .each in the@post.feature_image array.
Tutorial here:
https://web.archive.org/web/20191223022642/https://web-crunch.com/rails-drag-drop-active-storage-stimulus-dropzone/

Example Repo: https://github.com/justalever/drag_and_drop_active_storage

Changes I have made to get it working:
_form.html.erb

data-dropzone-max-files="10"

posts/show.html.erb

 <%= link_to @post do %>
    <% @post.feature_image.each do |image| %>
      <%= image_tag image %>
    <% end %>
  <% end %>

models/post.rb

class Post < ApplicationRecord
  belongs_to :user
  has_many_attached :feature_image
end
logo Created with Sketch.

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

© 2022 GoRails, LLC. All rights reserved.