rails-api | Rails for API only applications | REST library
kandi X-RAY | rails-api Summary
kandi X-RAY | rails-api Summary
Traditionally, when people said that they used Rails as an "API", they meant providing a programmatically accessible API alongside their web application. For example, GitHub provides an API that you can use from your own custom clients. With the advent of client-side frameworks, more developers are using Rails to build a backend that is shared between their web application and other native applications. For example, Twitter uses its public API in its web application, which is built as a static site that consumes JSON resources. Instead of using Rails to generate dynamic HTML that will communicate with the server through forms and links, many developers are treating their web application as just another client, consuming a simple JSON API. This guide covers building a Rails application that serves JSON resources to an API client or client-side framework.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Build Rack middleware .
- Sets up the configuration .
- Builds the middleware stack .
- Check if the static file is available
rails-api Key Features
rails-api Examples and Code Snippets
Community Discussions
Trending Discussions on rails-api
QUESTION
I have cloned an existing project and trying to run it in my system. Since this is the first time I don't have any Gemfile.lock file in my directory. I tried running bundle install and the following errors occur:
...ANSWER
Answered 2021-Apr-10 at 18:06In your project directory, try installing rails gem install rails -v 4.1.6
and removing the version from the failing gems like (liquid_markdown
, gon
, etc..) then try running bundle update
then bundle clean --force
I think this might be an issue because all the version of these gems are locked inside your Gemfile
QUESTION
I am creating a Login form in JavaScript and am trying to send this form to my Ruby on Rails backend.
I have two problems:
When the form loads, it triggers the eventListener and sends the blank form's data to the backend. I have e.preventDefault() commented out in the second function below because I kept getting the error that it is not a function. Note: in the backend for Rails, I get the following message when I type in params. "Permitted: false" concerns me.
"sessions", "action"=>"create", "session"=>{}} permitted: false>
When I fill in the form with an email and password and click the submit button, the loginData (from
loginButton.addEventListener("submit", submitLogin(loginData)
submits a blank value for the email and 'password' for the password (which are the default values I set to test the values) even though these elements are filled in in the form with an actual email address and password.
Function loading login form (note: this loads just fine):
...ANSWER
Answered 2021-Feb-19 at 02:43Your form is submitting automatically because of the way you've set the event handler. The .addEventListener()
API requires a reference to a callback function as the second argument. But you passed a function call with arguments like this:
loginButton.addEventListener("submit", submitLogin(loginData));
You have two choices to fix this:
- Pass a reference.
- This will require
loginData
to be available to the handler in some other fashion.
- This will require
loginButton.addEventListener("submit", submitLogin);
- Enclose the call within a function expression:
loginButton.addEventListener("submit", ()=>submitLogin(loginData));
Option 2 is generally preferred when needing to pass parameters to the handler. But you'll see below, for you, option one is the way to go.
The leads to the next problem - submitLogin()
function itself.
QUESTION
I was following step by step this article to no avail. I also cloned directly from author's repo and not only did I had the same issue but I am also not the onlyone
Problemat runtime the container does not have access to the rails
command
docker-compose run backend /bin/bash
...ANSWER
Answered 2021-Feb-06 at 20:58Ruby applications generally use a tool called Bundler to manage their application's dependencies. If your application depends on the rails
gem, which provides a rails
executable, that will often be in vendor/bin/rails
or somewhere similar, but not on the standard $PATH
.
The quickest way to make this work is to use the bundle exec
wrapper, which knows how to set up paths accordingly.
QUESTION
I am setting up my Rails API server following this tutorial: building-awesome-rails-apis-part-1
Everything works well, except the part that mentions that it is not necessary to indicate the namespace in the route. Eg.
Now our URls look like:
http://api.example.com/v1/people
or justhttp://api.example.com/people
if you don’t use the version, it doesn’t interfere with your regular people routes, and it looks great.
When I call http://api.mydomain.com/v1/therapists/
it works, but when I try to omit the v1
namespace in the URL it's not working, do I need to do any extra configuration?
I'm using Rails 6.0.3.4
This is my specific routes.rb file:
...ANSWER
Answered 2020-Nov-05 at 19:30If you omit the v1
namespace in the URL, you must also remove it from your routes.rb file.
The quote from the tutorial stated "or just http://api.example.com/people if you don’t use the version", meaning if you don't include the v1
namespace in the routes.rb file.
QUESTION
I am trying to implement a follow/unfollow function in a react rails-api web application. Currently the follow and unfollow post/delete just fine when I click the follow/unfollow button.
However, whenever a user visits another users page it will follow/unfollow when the page loads without clicking the follow/unfollow button. I do not understand why this is happening since I have, for my post/delete, the useEffect second param set to go off when the state for my follow/unfollow changes.
Please help me figure out why this is happening and how to prevent this. Let me know if more information is needed.
...ANSWER
Answered 2020-Sep-07 at 20:44import React, {useState, useEffect} from 'react'
import {Link, useParams} from 'react-router-dom'
import decode from 'jwt-decode'
function NotUserPage() {
const {id} = useParams()
const [user, setUser] = useState({})
const loggedUser = decode(localStorage.getItem("token"))
const username = loggedUser.username
const userId = loggedUser.user_id
const [following, setFollowing] = useState(false)
const fetchUserData = () => {
fetch(`http://localhost:3000/users/${id}`)
.then(res => res.json())
.then(data => setUser(data))
}
useEffect(() => {
fetchUserData()
}, [])
const unFollow = () => {
fetch(`http://localhost:3000/users/${id}/unfollow`, {
method: "POST",
body: JSON.stringify({
follower_id: userId,
followee_id: id
}),
headers: {
"Content-type": "application/json",
"Authorization": `bearer ${localStorage.getItem("token")}`,
},
})
.then(res => res.json())
.then(data => console.log(data))
.then(() => setFollowing(false))
}
const handleFollow = () => {
fetch(`http://localhost:3000/users/${id}/follow`, {
method: "POST",
body: JSON.stringify({
follower_id: userId,
followee_id: id
}),
headers: {
"Content-type": "application/json",
"Authorization": `bearer ${localStorage.getItem("token")}`,
},
})
.then(res => res.json())
.then(data => console.log(data))
.then(() => setFollowing(true))
}
const fButton = () => following ? unFollow() : handleFollow();
return (
{user.username}
follow
)
}
export default NotUserPage
QUESTION
ANSWER
Answered 2020-Aug-05 at 15:21Silly error on my part.
I just needed to change curl -XGET
to curl -v -XGET
to display the headers, which included the tokens.
QUESTION
I am trying to build a React front end on top of a Rails API and I am currently working on the registration form on React. Below is my attempt at handling the form submission.
...ANSWER
Answered 2020-Jun-07 at 17:43use axios and qs stringifier:
QUESTION
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :user
t.timestamps
end
end
end
class CreateArticles < ActiveRecord::Migration[6.0]
def change
create_table :articles do |t|
t.string :content
t.timestamps
end
end
end
...ANSWER
Answered 2020-Apr-19 at 19:19SET GLOBAL default_storage_engine = 'InnoDB';
Running this in phpmyadmin solved it. Seems to be an issue with MySQL database.
QUESTION
I've built a Rails-Api with React SPA frontend following this guide (very helpful, would recommend). I'm having issues getting Heroku to set the environment variables in the .env file that React uses for config vars. The values of the file are just set as literally "ENV[...]" instead of evaluating and setting the value. On the Rails side the Heroku ENV vars are being set correctly because I can see them in the console.
...ANSWER
Answered 2020-Mar-23 at 00:48You can expand environment variables in .env
file. When React app is built, it has access to variables in your Heroku dyno, and react-scripts supports referencing them in your .env
file. See more in docs.
With this in mind, your .env
file would look like:
QUESTION
Issue: styles applied to class names generated by Material-UI / JSS are incorrectly changing when components are re-rendered.
Setup: I'm serving a React app (built with create-react-app
) that uses Material-UI jss styling and a Rails back end. I'm not sure how relevant the Rails part is since the same thing happens when I open the build/index.html
file directly on my local machine -- the Rails back end handles the root request to serve the static client files as presented here. In either case, the static build is created using npm run build
, which runs react-scripts build
(from create-react-app
).
Example of the issue: I have an element which is given
className: {classes.logo}
. When built, classes.logo
is "jss3"
, which takes on the following correct CSS:
ANSWER
Answered 2019-Jun-18 at 15:52The issue is related to using two different versions of a class name generator. Many ways to do this; in my case I was mixing an older version of material-ui/core/styles#withStyles
with a newer material-ui/styles#makeStyles
as I was refactoring class components to use hooks. By removing usage of the older core/styles#withStyles
, I fixed the issue.
What happens is the two style classname generators don't know about each other, and create class names with simple indexes (e.g. jss3
). At least, they do this for production builds, it seems there use more verbose component-name-based class names in dev builds, which explains why I was only seeing it when hosting statically.
Since the FileEntry
component was not rendered until login, the jss3
class name was not generated by the second class name generator until after the login action, at which point the jss3
class was given updated styling, and the browser applied it to the existing jss3
elements as it is meant to do.
Some workaround solutions involved forcing both to use the same Jss Provider, but not using independent invocations of class name generators in the first place was a more thorough and well-supported solution.
Similar issues are documented here:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install rails-api
On a UNIX-like operating system, using your system’s package manager is easiest. However, the packaged Ruby version may not be the newest one. There is also an installer for Windows. Managers help you to switch between multiple Ruby versions on your system. Installers can be used to install a specific or multiple Ruby versions. Please refer ruby-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page