React Router 4 triggering full page reload - ruby-on-rails

React Router 4 is running on top of Rails and React, but following a <Link /> element triggers a GET request in my server and a full page reload in my development environment.
Here are the current versions I'm running:
"#rails/webpacker": "^3.0.2",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router-dom": "^4.2.2",
"clean-webpack-plugin": "^0.1.17",
"webpack-dev-server": "^2.9.5",
"webpack-merge": "^4.1.1"
I'm using the foreman gem, which runs the rails server and webpack-dev-server on localhost:5000.
What I've tried:
Adding historyApiFallback to my webpack-dev-server CLI
Using either <Link /> and <NavLink />
Adding a trailing / to my links; <Link path='/sign-in/' component={Register} />
Rendering just <App /> at the highest level component, and adding <BrowserRouter /> and all routing/switching to <App />
I've tried using exact path, strict path, and just path
I'm curious if it's something with my webpack config... I can post that as well if necessary. It just seems like this is all set up right but something else funky is going on.
What is working:
- I can switch between components, even though my rails routes in routes.rb has a catch-all: get '*path', to: 'pages#index'
Here is my highest level component, index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import Main from '../react/src/components/Main';
document.addEventListener('DOMContentLoaded', () => {
ReactDOM.render(
<Router>
<Main />
</Router>,
document.getElementById('app')
);
});
Here is main.js:
import React from 'react';
import { Route, Switch, } from 'react-router-dom';
import Dashboard from './Dashboard';
import FormContainer from './FormContainer';
const Main = props => {
return (
<main>
<Switch>
<Route path="/sign-in/" component={FormContainer} />
<Route path="/" component={Dashboard} />
</Switch>
</main>
);
};
export default Main;
An example of a component that is rendered, Dashboard.js:
import React from 'react';
import { Link } from 'react-router-dom';
const Dashboard = props => {
return (
<div>
<h2>Dashboard</h2>
<Link to="/sign-in/">Sign In</Link>
</div>
);
};
export default Dashboard;
The empty controller responsible for my root page:
class PagesController < ApplicationController
end
Also my routes.rb:
Rails.application.routes.draw do
root "pages#index"
get '*path', to: 'pages#index'
end
I've gone through multiple tutorials and I'm fairly certain that this is structured correctly... I think I'm mainly looking for any hints as to what may be interfering with React Router, outside of the library, i.e. webpack, running on localhost, etc.

I was able to fix this issue by renaming a tag of <main> that my application.html.erb's <%= yield %> tag was wrapped in... I don't quite understand why wrapping <%= yield %> in a tag specifically called <main> causes a page reload/disables client-side rendering, but in my case, it did!

Related

Rails 7 accessing pinned libraries

importmap.rb includes
pin "javascript-autocomplete", to: "https://ga.jspm.io/npm:javascript-autocomplete#1.0.5/auto-complete.js"
And, following the rails guides, application.js was amended with
import "javascript-autocomplete"
although uncertainty about the syntax remains. Also attempted, as the script defines var autoComplete was
import autoComplete from "javascript-autocomplete"
in either instance, while the header shows:
<script type="importmap" data-turbo-track="reload">{
"imports": {
"application": "/assets/application-333b944449a4c540424142c36f01f97feebd58f2f41d10565ecfde32cb315110.js",
"#hotwired/turbo-rails": "/assets/turbo.min-e5023178542f05fc063cd1dc5865457259cc01f3fba76a28454060d33de6f429.js",
"#hotwired/stimulus": "/assets/stimulus.min-900648768bd96f3faeba359cf33c1bd01ca424ca4d2d05f36a5d8345112ae93c.js",
"#hotwired/stimulus-loading": "/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js",
"javascript-autocomplete": "https://ga.jspm.io/npm:javascript-autocomplete#1.0.5/auto-complete.js",
[...]
}
the body has
<%= javascript_tag do %>
var demo1 = new autoComplete({
[...]
});
<% end %>
which fails to run: Uncaught ReferenceError: autoComplete is not defined
so the script is not being accessed.
So where is this lacking?

rails 6.1.3 using react Uncaught Error: Target container is not a DOM element

My app is rendering a blank page The console has the error "Uncaught Error: Target container is not a DOM element." from index.js line 7
This is my index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import '../stylesheets/index.css';
import App from '../components/App';
import * as serviceWorker from './serviceWorker';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('body')
);
serviceWorker.unregister();
index.html.erb:
<%= javascript_pack_tag 'index.js', 'data-turbolinks-track': 'reload' %>
<div id="root"></div>
Shyam's comment answered my question I needed to put my javascript_pack_tag below the div tag in my index.html file

react with Rails 5, getting CSRF error for post with axios

I'm trying to use react_on_rails to build my first example with react and rails. I'm trying to save some data to the rails backend, using axios for the ajax.
here's my code:
import store from "../store/helloWorld";
import axios from "axios";
export const SAVE_NAME = "SAVE_NAME";
export function saveNameAction(name) {
return {
type: SAVE_NAME,
name
};
}
export function saveName(name) {
axios
.post("/hello_world", saveNameAction(name))
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
}
and the component:
import PropTypes from "prop-types";
import React from "react";
import * as actions from "../actions/helloWorld";
export default class HelloWorld extends React.Component {
static propTypes = {
name: PropTypes.string.isRequired // this is passed from the Rails view
};
/**
* #param props - Comes from your rails view.
*/
constructor(props) {
super(props);
this.state = { name: this.props.name };
}
updateName(name) {
this.setState({ name: name });
}
handleSubmit(event) {
event.preventDefault();
actions.saveName(this.state.name);
}
render() {
return (
<div>
<h3>
Hellopp, {this.state.name}!
</h3>
<hr />
<form>
<label htmlFor="name">Say hello to:</label>
<input
id="name"
type="text"
value={this.state.name}
onChange={e => this.updateName(e.target.value)}
/>
<input
type="submit"
value="Submit"
onClick={event => this.handleSubmit(event)}
/>
</form>
</div>
);
}
}
The problem is that when I click the submit, my backend reports
Started POST "/hello_world" for ::1 at 2017-07-07 15:30:44 +0200
Processing by HelloWorldController#create as HTML
Parameters: {"type"=>"SAVE_NAME", "name"=>"Stranger", "hello_world"=>{"type"=>"SAVE_NAME", "name"=>"Stranger"}}
Can't verify CSRF token authenticity.
Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms)
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
For one, I don't understand why the parameters seem to be passed twice, but that's not even generating a warning, so don't care for now.
The problem is that I don't see a way to obtain the CSRF tokens in my react code to use in the post requests
should I just disable CSRF? or is there a better way?
ActionController::InvalidAuthenticityToken
(ActionController::InvalidAuthenticityToken):
Rails handles CSRF attacks by appending authenticity_token to every Non-GET requests(POST,PUT/PATCH and DELETE). The error means you are not sending authencity_token in the request params, You should append an unique authenticity_token to the params, something like "authuenticity_token" => "BsfdgtZ1hshxgthjj" which should resolve the issue.
react_on_rails deals with this issue by providing two helpers. From the react_on_rails documentation:
Rails has built-in protection for Cross-Site Request Forgery (CSRF), see Rails Documentation. To nicely utilize this feature in JavaScript requests, React on Rails provides two helpers that can be used as following for POST, PUT or DELETE requests:
import ReactOnRails from 'react-on-rails';
// reads from DOM csrf token generated by Rails in <%= csrf_meta_tags %>
csrfToken = ReactOnRails.authenticityToken();
// compose Rails specific request header as following { X-CSRF-Token: csrfToken, X-Requested-With: XMLHttpRequest }
header = ReactOnRails.authenticityHeaders(otherHeader);
I found that react_on_rails has a helper system to handle CSRF tokens,
it basically uses:
<%= csrf_meta_tags %>
to add the csrf_token to the headers in the page as a meta
and then you can use:
import ReactOnRails from "react-on-rails";
export function saveNameAction(name) {
console.log("creating action " + name);
return {
authenticity_token: ReactOnRails.authenticityToken(),
type: SAVE_NAME,
name
};
}
to fetch it and use it.

Using the Grails Stripe plugin

I'm using Stripe grails plugin in my application and I'm getting the below error:
Class:groovy.lang.MissingPropertyExceptionMessage:No such property: Stripe for class: com.myApp.app.SubscriptionRequestController
Here is my action:
def charge(String stripeToken, Double amount) {
Stripe.apiKey = grailsApplication.config.grails.plugins.stripe.secretKey
def amountInCents = (amount * 100) as Integer
def chargeParams = [
'amount': amountInCents,
'currency': 'usd',
'card': stripeToken,
'description': 'customer#sample.org'
]
def status
try {
Charge.create(chargeParams)
status = 'Your purchase was successful.'
} catch(CardException) {
status = 'There was an error processing your credit card.'
}
redirect(action: "confirmation", params: [msg: status])
return
}
i'm also getting the below error since i installed the plugin when i remove it i don't see it, it occurs while trying to access or refresh any view :
java.lang.RuntimeException: It looks like you are missing some calls to the r:layoutResources tag. After rendering your page the following have not been rendered: [head]
Try this:
in your main layout file e.g.: main.gsp, have the two additional both in body and head. Also, loads the stripe-v2 manually. No need to add it in web-app/js as the plugin itself already has it.
<html>
<head>
<g:javascript src="stripe-v2.js" />
<r:layoutResources/>
</head>
<body>
<g:layoutBody/>
<r:layoutResources/>
</body>
</html>
in config.groovy add:
grails.resources.modules = {
stripe {
dependsOn 'jquery'
resource url:'/js/stripe-v2.js', disposition: 'head', exclude:'minify'
}
}
Sample gsp (taken from the docs) but i added payment-errors as the source use it:
<stripe:script formName="payment-form"/>
<g:form controller="checkout" action="charge" method="POST" name="payment-form">
<div class="payment-errors"></div>
<div class="form-row">
<label>Amount (USD)</label>
<input type="text" size="20" autocomplete="off" id="amount" name="amount"/>
</div>
<stripe:creditCardInputs cssClass="form-row"/>
<button type="submit">Submit Payment</button>
</g:form>
I think the plugin itself did not support the current implementation of Grails Asset Pipeline.I think it is not compatible for grails 2.4.3 and above.
As it stands, you probably aren't importing the com.stripe.Stripe class which is why you're getting that particular message.
You don't need to attempt to manually assign the secret key to the Stripe class. Just define grails.plugins.stripe.secretKey in your Config.groovy and the plugin will handle the rest, as you can see in the plugin source.

Angular ui-router templates are not loading, Rails backend

I'm following along with the Angular/Rails tutorial at Thinkster and I've run into an issue which seems to be most likely be Angular-related. Everything works just fine until I get to the Angular Routing section. Simply put, the inline templates within the <script> tags do not load in the <ui-view></ui-view> element. I originally thought this may be due to having opened the page locally as a file rather than having it loaded from a server, but the same problem persists even after integrating Rails (using an older version of Sprockets, as pointed out in this similar but unrelated issue).
When I load the index page in either the browser as a file or as a URL when running the Rails server, I've inspected the HTML and, sure enough, the only thing it shows in the code are the divs and an empty <ui-view> element, indicating something just isn't adding up correctly. I've tried various things, including:
Using the newest version of ui-router (0.2.15 at this writing) rather than the version in the tutorial
Using <div ui-view></div> instead of <ui-view></ui-view>
Changing the value of 'url' in the home state to 'index.html', including using the full path to the file (file:///...)
Putting the contents of the inline <script> templates into their own files (without the <script> tags, of course) and specifying the 'templateUrl' field using both relative and full paths
Trying both Chrome and Firefox just to be extra certain
None of these things have worked, even when accessing http://localhost:3000/#/home when the Rails server is running after having integrated Angular into the asset pipeline in the Integrating the Front-end with the Asset Pipeline section of the tutorial. Indeed, the route loads but does not show anything more than a completely blank page with a lonesome and empty <ui-view> element when inspecting the HTML using Chrome's dev tools.
Given that the issue seems to occur even before the Rails portion, it does seem like something to do with Angular itself, but I've got no clue what's going on, especially since I've followed along to the letter.
I'm using Bower to manage the Angular dependencies and the HTML does show that the Angular javascript files in both the app/assets/javascripts directory and in the vendor/assets/bower_components directory are being loaded properly in the <head> section, so everything seems to be okay on the asset pipeline integration.
Versios I'm using:
Rails: 4.2.3
Ruby: 2.2.1p85
Angular: 1.4.3
ui-router: 0.2.15
The code I've got for the major moving parts is below:
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Test App</title>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
</head>
<body ng-app="testApp">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<ui-view></ui-view>
</div>
</div>
</body>
</html>
app/assets/javascripts/app.js
angular.module('testApp', ['ui.router', 'templates']).config(['$stateProvider', '$urlRouteProvider', function($stateProvider, $urlRouteProvider) {
$stateProvider
.state('home', {
'url': '/home',
'templateUrl': 'home/_home.html',
'controller': 'MainCtrl'
})
.state('posts', {
'url': '/posts/{id}',
'templateUrl': 'posts/_posts.html',
'controller': 'PostsCtrl'
});
$urlRouteProvider.otherwise('home');
}]);
app/assets/javascripts/application.js
//= require angular
//= require angular-rails-templates
//= require angular-ui-router
//= require_tree .
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
respond_to :json
def angular
render 'layouts/application'
end
end
config/routes.rb
Rails.application.routes.draw do
root to: 'application#angular'
end
app/assets/javascripts/home/mainCtrl.js
angular.module('testApp').controller('MainCtrl', ['$scope', 'posts', function($scope, posts) {
$scope.posts = posts.posts;
$scope.addPost = function() {
if (!$scope.title || $scope.title === "")
return;
$scope.posts.push({
'title': $scope.title,
'link': $scope.link,
'upvotes': 0,
'comments': [
{'author': 'Some Person', 'body': 'This is a comment.', 'upvotes': 0},
{'author': 'Another Person', 'body': 'This is also a comment.', 'upvotes': 0}
]
});
$scope.title = "";
$scope.link = "";
};
$scope.incrementUpvotes = function(post) {
post.upvotes++;
};
}]);
app/assets/javascripts/posts/postsCtrl.js
angular.module('testApp').controller('PostsCtrl', ['$scope', '$stateParams', 'posts', function($scope, $stateParams, posts) {
$scope.post = posts.posts[$stateParams.id];
$scope.addComment = function() {
if($scope.body === '')
return;
$scope.post.comments.push({
'body': $scope.body,
'author': 'user',
'upvotes': 0
});
$scope.body = '';
};
}]);
app/assets/javascripts/posts/posts.js
angular.module('testApp').factory('posts', ['$http', function($http) {
var o = {
'posts': []
};
o.getAll = function() {
return $http.get('/posts.json').success(function(data) {
angular.copy(data, o.posts);
});
};
return o;
}]);
If any other code is required to help uncover the problem, please let me know and I'll supply anything requested.
it seems that the angular-ui-router is incompatible with the new Rails sprockets. To fix this, add this earlier version of sprockets to your gemfile:
gem 'sprockets', '2.12.3'
And then run bundle update sprockets.
This was answered a few times in other similar questions, like the one below:
Angular Rails Templates just not working
$urlRouteProvider in my code should've been $urlRouterProvider. Be sure to double-check everything, folks, and make good use of the console!

Resources