I was writing an app that had a post model. The post model belongs to a “type.” In order to create my form for my post model, I needed to create a dynamic select field with my options coming from my type model.

Here’s how I did that.

First, in my controller, I got my types and put them in an instance variable to expose it to my view.

def new
@post = Post.new
@types = Type.all
end

Then, in my view, I had the form:

<%= form_with(model: @post, local: true) do |f| %>  <div class="form-group">…

So you’ve been writing Ruby for a while. Maybe you’re familiar with control flow, data types, and the overall way to work with Ruby.

Before you start writing large programs or before working with others, it’s important that your code is consistent and sticks to a single style.

In this article, I’m going to give you some guidance on how to write Ruby that looks nice and that will be similar to how others write Ruby.

I got these guidelines while reading Eloquent Ruby by Russ Olsen. …


Welcome to part 3 of my React on Ruby on Rails (React on Rails?, RX3?, RoRoR?). Today, we’re going to add CRUD functions to our app. We already have it set up on the backend, now we just need to connect our frontend. This should be relatively easy.

Here is the code for our API in “app/javascript/src/api/api.js”

import axios from 'axios'const ROOT_PATH = '/api/v1'
const POSTS_PATH = `${ROOT_PATH}/posts`
export const getPosts = () => {
return axios.get(POSTS_PATH)
}
export const getPost = (postId) => {
return axios.get(`${POSTS_PATH}/${postId}`)
}
export const createPost = (postParams) => {
return axios.post(POSTS_PATH, postParams)
}


Hey, Josh here again. Lately, I’ve been showing you how to build projects with Ruby on Rails and React.js. Today, I’m going to show you something a little bit different — how to easily make your website responsive.

If you’ve written any CSS for responsive websites, then you know how much of a pain it can be to write media queries. This responsive technique involves only changing your font-size for each media query.

It’s all about using the ‘rem’ unit instead of using pixels. You might be used to writing something like this:

.my-class {
margin-top: 16px;
}

Instead of…


Forms are the backbone of the Internet. Without them, it would be pretty difficult to make pass information between web pages. Luckily, working with forms in React.js is pretty easy.

Let’s build a basic form today using a functional component and using the useState hook.

First, let’s make a simple form in React.

import React from 'react'const Form = () => {
return (
<form>
<h1> Our Form </h1>
<label htmlFor="title">Title</label>
<input type="text" name="title" id="title" />
<label htmlFor="body">Body</label>
<textarea name="body" id="body"></textarea>
<input type="submit" value="Submit" />
</form>
)
}
export default Form

Is this an ugly form? Yes. Does it…


Previously, we set up our Ruby on Rails app to use React.

Now, we need to do a few more things to make sure our app is really functional. We still have to

  1. Set up our model in rails
  2. Have our frontend connect to our backend
  3. Integrate Redux so React works better.

Let’s get started.

Setting up our Post model and controller in rails

This is going to be a pretty common Rails code. First, create the model in “app/models/Post.rb”.

class Post < ApplicationRecordend

Next, we’re going to set up our serializer. This basically turns our model into JSON that we can send to our frontend. Create “app/serializers/post_serializer.rb”…


Working with strings is one of the most common things you’ll do when working with Ruby or Ruby on Rails. Luckily, Ruby has all sorts of great built-in methods that help us work with strings.

I put together this guide so you know what methods are available to you when working with strings in Ruby.

I’d like to shoutout to this article which inspired me to write this guide.

String Methods in Ruby

How to get a string’s length in Ruby.

This is one of the most common things you’ll do when working with strings. …


If you’re like most developers, you probably have a lot of work to do. Because of this, smart developers like to take advantage of tools that let them write less code.

And Bootstrap is a tool that does just that. This CSS framework lets us quickly whip up pages that look pretty decent.

In this short article, I’m going to show you how to add Bootstrap 4 to your Rails project. This is a little different from adding earlier versions of Bootstrap to other versions of Ruby on Rails.

I’m going to assume you have already started a Rails project.


Ruby on Rails is such a wonderful framework to work with. However, using Embedded Ruby (.erb) and ajax to build apps with dynamic frontends can be painful.

And that’s where front-end frameworks like React, Angular, and Ember come in. Since React is the hot thing right now, we’re going to use it.

But how do you get React set up in a Ruby on Rails application? That’s what I’m covering in this article

The first thing you need to do is create your Ruby on Rails application and tell it you’re going to use React. …


Pretty much any application that you want to make will include some portions that need to be restricted.

Maybe you have certain pages you want to block, or maybe you just want to make sure users can log in to your site and post items.

Whatever it is, making sure that your website or application has the ability to log in and log out is vital.

The good news is, authentication in Ruby on Rails 6 is incredibly easy to implement. You’ll have this up and running in no time.

Getting started with authentication with Rails 6

The first thing you need to do is make sure…

Josh Lee

Marketing and language expert

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store