質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 3.2.0

Ruby on Railsは、Rubyにより構築されたオープンソースのWebアプリケーションフレームワークである。 version 3.2.0は2012年1月2日にリリースされた。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

0回答

2047閲覧

SwiftでrailsのwebAPIを使ったアプリの開発でPOSTできない

kabutomusi8

総合スコア7

Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 3.2.0

Ruby on Railsは、Rubyにより構築されたオープンソースのWebアプリケーションフレームワークである。 version 3.2.0は2012年1月2日にリリースされた。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2016/04/04 17:20

###発生している問題・エラーメッセージ
フロントサイドをswift,バックエンドをrailsを使い、APIサーバーを使う簡易的なアプリを作ろうとしているのですが、swift側からPOSTをリクエストすると
param is missing or the value is empty: product
のエラーが出ます。

なにが悪いのでしょう?
初心者なので、丁寧に教えていただけると非常に助かります><

###ソースコード

Ruby

1 2class ProductsController < ApplicationController 3 before_action :set_product, only: [:show, :edit, :update, :destroy] 4 skip_before_filter :verify_authenticity_token, only: :create 5 6 # GET /products 7 # GET /products.json 8 def index 9 @products = Product.all 10 respond_to do |format| 11 format.html # => 通常のURLの場合、index.html.erb が返される 12 format.json { render json: @products } # URLが.jsonの場合、@products.to_json が返される 13 end 14 end 15 16 # GET /products/1 17 # GET /products/1.json 18 def show 19 end 20 21 # GET /products/new 22 def new 23 @product = Product.new 24 end 25 26 # GET /products/1/edit 27 def edit 28 end 29 30 # POST /products 31 # POST /products.json 32 def create 33 @product = Product.new(product_params) 34 35 respond_to do |format| 36 if @product.save 37 format.html { redirect_to @product, notice: 'Product was successfully created.' } 38 format.json { render :json, @product } 39 else 40 format.html { render :new } 41 format.json { render json: @product.errors, status: :unprocessable_entity } 42 end 43 end 44 end 45 46 # PATCH/PUT /products/1 47 # PATCH/PUT /products/1.json 48 def update 49 respond_to do |format| 50 if @product.update(product_params) 51 format.html { redirect_to @product, notice: 'Product was successfully updated.' } 52 format.json { render :show, status: :ok, location: @product } 53 else 54 format.html { render :edit } 55 format.json { render json: @product.errors, status: :unprocessable_entity } 56 end 57 end 58 end 59 60 # DELETE /products/1 61 # DELETE /products/1.json 62 def destroy 63 @product.destroy 64 respond_to do |format| 65 format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' } 66 format.json { head :no_content } 67 end 68 end 69 70 private 71 # Use callbacks to share common setup or constraints between actions. 72 def set_product 73 @product = Product.find(params[:id]) 74 end 75 76 # Never trust parameters from the scary internet, only allow the white list through. 77 def product_params 78 params.require(:product).permit(:name, :price, :category_id) 79 end 80end 81

Swift

1mport UIKit 2import Alamofire 3 4class ViewController: UIViewController { 5 6 @IBOutlet weak var priceText: UITextField! 7 @IBOutlet weak var nameText: UITextField! 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 // Do any additional setup after loading the view, typically from a nib. 11 12 } 13 14 override func didReceiveMemoryWarning() { 15 super.didReceiveMemoryWarning() 16 // Dispose of any resources that can be recreated. 17 } 18 19 @IBAction func saveBtnTap(sender: AnyObject) { 20 21 let memo = Memo() 22 memo.name = nameText.text 23 memo.price = Int(priceText.text!) 24 memo.category_id = 1 25 26 StockMemos.postMemo(memo) 27 } 28 29} 30 31

Swift

1import UIKit 2import Alamofire 3 4class StockMemos: NSObject { 5 6 class func postMemo(memo: Memo) { 7 8 var params: [String: AnyObject] = [ 9 "name": memo.name!, 10 "price":memo.price!, 11 "category_id":memo.category_id! 12 ] 13 14 // HTTP通信 15 Alamofire.request(.POST, "http://localhost:3000/products.json", parameters: params, encoding: .URL).responseJSON(completionHandler: { response in 16 if response.result.isSuccess{ 17 print("成功しました") 18 } 19 20 }) 21 22 } 23 24 } 25

Swift

1import UIKit 2 3class Memo: NSObject { 4 var name:String? 5 var price:Int? 6 var category_id:Int? 7 } 8

###補足情報(言語/FW/ツール等のバージョンなど)
Swift 2.1
Xcode 7.1.1
ruby 2.1.3
rails 4.2.5.1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問