実現したいこと
まず最初に、私はとても初心者なので意味の分からないことを言っていたらごめんなさい。
実現したいことは、React Nativeで作成中のアプリで、TextInputに入力した値を
Rails APIを通してDBに保存したいということです。
現状
現在の状況として、
ReactNativeで新規アプリを作成して、文字入力はできるようになっています。
そして、RailsをAPIモードで作成し、Postモデルを作成しました。
そこで
"http://localhost:3000/posts/new"
このURLにアクセスすることによってTextInputの内容をPostモデルに保存しようとしています。
以下にソースコードをのせます
React Native
TextInputに入力された値をPostする画面です
React.js
1//CreateNewEventCardScreen.js 2 3import React, { Component } from 'react'; 4import { 5 View, 6 Text, 7 StyleSheet, 8 TextInput, 9 TouchableOpacity, 10 Alert, 11} from 'react-native'; 12 13export default class CreateNewEventCardScreen extends Component { 14 state = { 15 inputValue: "いまどうしてる?" 16 }; 17 18 _handleTextChange = inputValue => { 19 this.setState({inputValue}); 20 } 21 22 /* ========================================================== 23 ========================================================== 24 POSTしようとしているメソッドです 25 ========================================================== 26 ========================================================== */ 27 postValueToAPI() { 28 fetch('http://localhost:3000/posts/new', { 29 method: 'POST', 30 headers: { 31 Accept: 'application/json', 32 'Content-Type': 'application/json', 33 }, 34 body: JSON.stringify({ 35 content: this.state.inputValue, 36 }), 37 }); 38 } 39 40 render() { 41 console.log(this.state); 42 return ( 43 <View style={styles.container}> 44 <TextInput 45 value={this.state.inputValue} 46 onChangeText={this._handleTextChange} 47 style={styles.txtInput} 48 multiline={true} 49 /> 50 <TouchableOpacity onPress={this.postValueToAPI.bind(this)}> 51 <View style={styles.postBtn}> 52 <Text style={{color:"white"}}>送信</Text> 53 </View> 54 </TouchableOpacity> 55 </View> 56 ); 57 } 58} 59 60const styles = StyleSheet.create({ 61 container: { 62 flex: 1, 63 alignItems: "center", 64 //justifyContent: "center", 65 }, 66 txtInput: { 67 width:"80%", 68 height:200, 69 borderWidth:0.5, 70 borderColor:"gray", 71 padding:8, 72 marginTop:50, 73 }, 74 postBtn: { 75 width:120, 76 height:35, 77 borderRadius:10, 78 backgroundColor:"blue", 79 marginTop:20, 80 alignItems: "center", 81 justifyContent: "center", 82 } 83})
Ruby on Rails
//routes.rb Rails.application.routes.draw do resources :users # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html # post get "posts" => "posts#index" post "posts/new" => "posts#new" end
//posts_controller.rb class PostsController < ApplicationController # GET /posts def index @posts = Post.all.reverse_order render json: @posts end # POST /posts/new def new @post = Post.new(post_params) if @post.save render json: @post, status: :created, location: @post else render json: @post.errors, status: :unprocessable_entity end end private # Only allow a trusted parameter "white list" through. def post_params params.fetch(:post, {}) end end
React Native側のPostするためのpostValueToAPIメソッド
は完全にコピペでどういう意味なのか分かっていません。
またPostsControllerのnewアクションも
もともとあったUsersControllerのcreateアクションのコピペになってます。
教えていただきたいこと
以上から、
・ReactNativeのPostのメソッドの書き方
・newアクション内でどのようにしてTextInputの情報をうけとるのか
を教えていただきたいです。
よろしくお願いいたしいます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。