DEV Community

Patryk Orlik
Patryk Orlik

Posted on

React-Rails app - How to validate uniqueness?

Hi there,
This is my first post. Please let m know if I'm doing something wrong. I am Ruby on Rails deveoloper which start to useing React form Model View and Ract-Native.

I am building app to mangaing warehouse. It is already done and in production (created with Rails, Turbolinks and jQery). Now I would like to change this app to React single page app with react-native mobile version.

I knew basics about, React, Redux, React-Native now and I started changing process.

I have simple react-rails app created. I created a form in react and db in rails and I would like to crate record only if his name is unique.

I connect with rails through axios and when I try to create record with not unique name, axios send back to react action validation error.

export const startAddingLocation = ( name ) => {

return(dispatch) => {

const location = { name }

return '/api/locations', { location })
.then(response => {
if(['status'] === 'SUCCESS'){
const id =;

const created_at =;

dispatch(addLocation({id, ...location, created_at}));

}).catch(error => {
dispatch(nameChangedError(name, error+"Cannot save in DB"))

I need to find a way to not redirect from from when have a validation error. What is the best route to do this? I tried to redirect when action nameChangedError fill up error in redux but this is asynch action and when when I am checking error in component it still empty. Sould I redirect somehow from action?

onButtonPress() {

const { name, error } = this.props;

if(name && name.length < 30){


this.props.nameChangedError(name, "Incorrect value")


Top comments (1)

andy profile image
Andy Zhao (he/him)

Hey Patryk, congrats on your first post! I'd recommend using codeblocks to format your post. You can use them by wrapping your code portions in triple backticks, like so:

codeblock example

You can also put in the language you want to add syntax highlighting for, like I did with the first set of triple backticks (`javascript `).