Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/controllers/test_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
class TestController < ApplicationController
def index
end

def ajax
render json: {
title: 'Bun Bun Hello YouTube!',
}
end
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
root 'test#index'
resources :test

get 'ajax' => 'test#ajax'

# React のルーティングを使いたいので記載
get '*path', to: 'test#index'

Expand Down
16 changes: 16 additions & 0 deletions frontend/src/javascripts/actions/Article.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,19 @@ export function updateArticleInfo(updateText) {
}
}

export function fetchArticleInfo() {
return function (dispatch) {
return fetch('/ajax', {
credentials: 'include',
method: 'GET',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
})
.then(response => response.json())
.then(json => {
dispatch(updateArticleInfo(json.title));
});
}
}

8 changes: 8 additions & 0 deletions frontend/src/javascripts/components/article/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class Article extends Component {
<input type="text" ref="update_text" name="update_box" />
<button onClick={(e) => this.updateArticleInfo(e)}>Update Info</button>
</div>
<div>
<button onClick={(e) => this.fetchArticleInfo(e)}>Update Info With Ajax</button>
</div>
{this.props.children}
</div>
);
Expand All @@ -25,6 +28,11 @@ class Article extends Component {
const updateText = this.refs.update_text.value;
dispatch(ArticleActions.updateArticleInfo(updateText));
}

fetchArticleInfo(e) {
const { dispatch } = this.props;
dispatch(ArticleActions.fetchArticleInfo());
}
}

function mapStateToProps(state) {
Expand Down