Understanding LeetCode 234. Palindrome Linked List

This was probably the second or third challenge that I attempted as I started exploring the platform by the featured list “Challenges for New Users”.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Image Upload with Rails 5.2 Active Storage

To demonstrate how to use Active Storage, we will build a simple app where a user has a profile image associated with it. I’m going to assume that you have little or no knowledge of Rails and keep it as simple as possible.

Let’s start off with a basic rails setup.

Open the terminal and type the command below,

this will create a new rails app, then we need to go to the recently created folder,

now we can create a user model with name and email columns,

let’s run the migration.

So far we just created a user’s table with name and email columns, we now have to generate a user’s controller and its corresponding views.

In the index, we want to show all the users, in the show we want to see a particular user’s profile, the new will have a form for creating new users and the create method will use the params gotten from the submitted form to add a new user to the database.

In index.html.erb, we just show the name of the user, a link to the user’s profile, and a link to create a new user.

In show.html.erb, we have the name and email of the user and a link to all users.

The new.html.erb contains a form for creating new users

We are almost done with the basic setup, one last thing we have to do is to set up the routes to our index, show, and new pages. This is done on the routes.rb file which is located in the config folder.

We set the root route to the index page.

That’s all for the basic setup. When we start the server we should have our app running. In the terminal type the code below to start the server.

Go ahead and create a new user, you should now see a show link next to the user’s name on the home page. When you click the link, you should see the user’s name and email.

Pheew! Now we are done with the basic setup, let’s get into business and add a profile picture to each user.

To install Active Storage, run the following command:

and then migrate to generate the tables.

You should now have two new tables in your db/schema.rb file.

In this example, we are only attaching one picture to the user. Add the following code to your user model.

The form in new.html.erb now looks like this:

The next and final change is to show the uploaded image on each user’s show page. So go to “app/views/users/show.html.erb” and update the code with the one below:

Now you should be able to upload an image when creating a new user. When you go to the user’s show page you should see the uploaded image. Try uploading a file which is not an image.

That’s it for this article, we could improve this app in so many ways, for example, resize images before adding them to the view, updating a user’s profile picture, etc. I’d leave that to you to play around with.

I won’t be covering that in this article, but I’ll leave a link below to an article that did a great job explaining it.

Add a comment

Related posts:

Vitamin C And Its Role In Fighting inflammation And Oxidative Stress

Vitamin C is a well-known essential nutrient vital for maintaining good health. This essential vitamin is important for maintaining a robust immune system and plays a crucial role in fighting…

Etapa de la conquista

Las etapas de la conquista de América por los europeos fueron: el descubrimiento, la conquista, la colonización y la evangelización. Desde la llegada de Cristobal Colon al nuevo mundo el 12 de…

American G.I.s Face Winter Terror

Follow the gaze of the two American soldiers. The men suspect German soldiers are off to the right, well out of the frame of the photograph. The G.I.s move warily. The third man in the rear is moving…