Activity
Posted in Scheduling Posts Discussion
To fix the Turbolinks issue you could put your js into a wrapper like this:
$(document).on("turbolinks:load", function() {
# your javascript goes here...
});
What do you guys think?
Do you want to post more details, like what error you're getting or where you're hosting?
Consider using websockets, it would allow you to feed the refreshed data immediately, not wait for a minute potentially, which is kinda long when you're talking about real-time.
Read more here if your app is Rails: https://guides.rubyonrails.org/action_cable_overview.html
Awesome!
Posted in Need a hand with associations
Thanks for explaining.
Posted in Need a hand with associations
This looks correct, but I have to express that having these names with very similar meanings is confusing: member, account, user. I'm struggling to imagine a use case for this setup. :)
Uppy and Dropzone and popular js libraries that provide rich user experience for file uploads. ActiveStorage supports the integration of such libraries, but I couldn't find an example online on how to do it in practice. There are no active gems to implement it easily either.
Can you share your experiences if you had success building such an integration?
It would be awesome if Chris could create a video on this subject. I think it would be a hit video!
I assume you're talking about a Rails site. Is that correct? If not, use a SAS service like Shopify, or Ecwid.
How is an e-commerce site special in terms of hosting? What is your special requirement for hosting that other Rails sites do not need?
If you want easy hosting for a smaller site go for Heroku, if you have many users and you care about costs, and need a bit more control (for example install unique software) go for AWS Elastic Beanstalk.
If you need an easy way to create a filtered table of data try this gem:
https://github.com/leikind/wice_grid
How slow is slow? Seconds to load a single image? That's not normal. If it's under a second for many images, that's normal. It's generally slow to load images from AWS S3.
The only thing you can do to increase speed is to implement a CDN, but it complicates your ActiveStorage setup somewhat. Read this: https://medium.com/@tranduchanh.ms/optimize-rails-app-performance-with-rails-amazon-cloudfront-e3b305f1e86c
Same! I learned a lot.
The teaching method by Chris is excellent.
He explains the background to what he's doing, including his personal opinion on how to achieve a task best, then he walks you through the actual steps. It's slow enough to be able to understand, and fast enough to be able to sit through things.
I love that he leaves the errors in the videos, which helped me understand how to find bugs. And also, that he edits the videos so we don't have to sit through the boring parts with no useful information.
1) In the Journal model you're only recording the association between users and posts where the user marked it as read. You're not recording unread posts. Therefore when you query the journal model for all records match the current user, you would only be pulling the read posts.
current_user.posts does the following, but in one step in an efficient database query:
- Collects all journal entries where user_id matches the current user's id
- Takes the post_id from the collected journal entries
- Pulls the post objects based on the post_ids and puts it into an ActiveRecord object
2) OK, the many-to-many will work for you just fine. Here is some pseudocode to create the condition for the display:
# Posts controller
@recently_commented_posts = Post... # You got this covered I assume
@recently_commented_posts.each do |post|
journal_entries_for_given_post = Journal.where( user_id: current_user.id, post_id: post.id )
# if there are no journal entries (count == 0), it means the post hasn't been marked as read
if journal_entries_for_given_post.count == 0
<p><%= link_to post.title, post %></p>
end
end
3) Nothing special, just the usual resources :journals
is sufficient in routes.
In your journals_controller.rb you do need to create a method for new at least to create the new journal entries. So it would be something like this:
class JournalsController < ApplicationController
before_action :set_journal
before_action :authenticate_user!
def create
@journal = Journal.new(journal_params)
respond_to do |format|
if @journal.save
@post = Post.find(@journal.post_id)
format.html { redirect_to posts_path(@post), notice: 'Successfully marked as read.' }
end
end
end
private
def set_journal
@journal = Journal.find(params[:id])
end
def journal_params
params.require(:journal).permit(:post_id, :user_id)
end
end
In your posts_controller to the show method (or to the index method depending on where you show the button) you need to add the following to initiate the form:
@journal = Journal.new
In summary:
- You would be initiating the form from your posts controller show or index methods,
- Display the form on your posts show or index view,
- And when you submit the form the params will be submitted to the journal controller to create the record.
Let me know if this makes sense! :)
I wish to help but I can't figure out what you want to do. Use short and simple sentences to describe:
- Your current setup
- Your desired user flow
- What you're having a problem with
- Show code if you have any
Posted in Medium.com style URLs for username
I haven't tested it, but in your routes try:
get "/:username" => "user#show"
In your user controller:
def show
@user = User.where(username: params[:username])
end
See my answer to the same here: https://gorails.com/forum/how-to-mark-a-post-as-read-so-that-it-won-t-show-up-again-for-a-user#forum_post_14150
You can do two types of user interactions:
- The one described in the above link is a button 'Mark as Read' similar to what we have here on Gorails 'Mark as Completed'.
- The other one may be an automatic one where the page is marked as read automatically as soon as the user loads the page. In this case the difference will be in the controller. As soon as the user hits the show method, you create a journal entry that records the view.
Hope this makes sense after reading the link above.
There are multiple ways to set this up, but the most up-to-date 'Rails way' is called the many-to-many association.
Create a table that you can call 'journal', and it will contain only two columns:
- post_id
- user_id
When you create a record in this table it records the fact that a certain post was read by a certain user.
And, of course the created_at, and updated_at will be added to the journal table by default. You can also add other columns here if you wanted to record something related to the event. For example you may want to record the IP of the user, or the browser they used.
You will need this in your models:
# user model
has_many :journals # kinda clunky but it means journal entries
has_many :posts, :through => :journals
# posts model
has_many :journals
has_many :users, :through => :journals
# journal model
belonds_to: user
belongs_to :post
When you display a post for a certain user, you will check if there is a record with that post_id and user_id in the journal table. So in controller you will have something like this:
def show
@post = Post.find(params[:id])
@read = Journal.where(post_id: @post.id, user_id: current_user.id).last
end
In your views you will have to check if the @read has any records. If nil you will display a button to create a new journal entry. If not nil, it means the journal entry has been created in the past and thus you can tell the user they already read it:
<% if @read == nil %>
<% form_for @journal do |f| %>
<% f.hidden_field :post_id, value: post.id %>
<% f.hidden_field :user_id, value: current_user.id %>
<% f.submit 'Mark as Read' %>
<% end %>
<% else %>
You read this entry <%= time_ago_in_words(@read.created_at) %> ago
<% end %>
You will have to create the new and create methods for the journal of course for the form to work.
Now, you can create a page where you list all the posts read by a user by doing a query like this:
@posts_read_by_user = current_user.posts
Or, you can list all the readers of a post:
@readers_of_post = @post.users
Posted in New Ruby/Rails site with documentation
This works pretty well. Thanks!
Posted in Machine learning in Ruby?
I know Python is more popular for ML, but I prefer Ruby, and I was wondering if anyone has experience using Ruby for ML projects.
Great tutorial and subject!
I understand how you can create a variant to resize the original uploaded image, but how about other formatting, more specifically making the avatar a circle not a square. I realize that you can use CSS to achieve this, but such CSS doesn't always work. For example Outlook doesn't recognizes rounded corners CSS.
I built an app that generates email signatures and thus I want the image itself to have a circular formatting by applying a mask or something similar. How would I apply such a transformation to the image with active storage? What are the parameters for the variant?