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

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

新規登録して質問してみよう
ただいま回答率
85.50%
POST

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

1回答

2513閲覧

Ruby on rails: form_forで受け取ったパラメータを指定のコントローラーに渡したい

RyuSA

総合スコア131

POST

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

0クリップ

投稿2016/06/30 10:18

編集2016/06/30 10:28

###前提・実現したいこと

お世話になります。 今、Ruby on railsとTwitterAPIを用いて、特定の文字列を検索してWebページに表示させるという Webアプリケーションを作成しています。

###発生している問題・エラーメッセージ

index.html.erb内のフォームから受け取ったパラメータを、Timelineコントローラーに送って hogeingアクションを起こしたいのですが、formで入力したパラメータを送れません。 お願いします。

エラーメッセージ

Showing /home/ubuntu/workspace/myapp/app/views/timelines/index.html.erb where line #4 raised: First argument in form cannot contain nil or be empty 第4列目は以下のソースに書いてあります

###該当のソースコード
Ruby on railsで開発

[index.html.erb] ... <div> <%= form_for @twitter do |f|%> #この上のformの行がエラーメッセージにおける line #4 です <%= f.label :word, "Test" %> <%= f.text_field :word %> <%= f.submit '取得する' %> <% end %> </div> ...
[routes.rb] Rails.application.routes.draw do resources :timelines root 'timelines#index' #試してみてダメだったもの # post '/timelines' => 'timeline#hogeing' post '/' => 'timelines#hogeing' end
[timelines_controller.rb] class TimelinesController < ApplicationController def index @twitter = Timeline.new end def hogeing # エラー確認用 if @twitter && params[:word] # インスタンス作成 @twitter = Timeline.new # 検索ワード取得 @twitter.word = params[:word] word = @twitter.word # 検索、最近のツイート50件を取得 @twitter.tweet = @twitter.client.search(word, lang: "ja", result_type: 'recent', count: 50).map do |tweet| { icon: tweet.user.profile_image_url, name: tweet.user.name, text: tweet.text, } end end render action: "index" end end

###試したこと

・routes.rbでpost元とpost先の変更(ソース中に書いてある通り) ・[index.html.erb]内の <%= form_for @twitter, :url => root_path do |f|%> を <%= form_for @twitter do |f|%> に書き換える(createコマンドが必要と言われました。)

###補足情報(言語/FW/ツール等のバージョンなど)
cloud9上で開発中。rake routesコマンドでは以下が表示されています。

> Username:~/workspace/myapp $ rake routes > Prefix Verb URI Pattern Controller#Action > timelines GET /timelines(.:format) timelines#index > POST /timelines(.:format) timelines#create > new_timeline GET /timelines/new(.:format) timelines#new > edit_timeline GET /timelines/:id/edit(.:format) timelines#edit > timeline GET /timelines/:id(.:format) timelines#show > PATCH /timelines/:id(.:format) timelines#update > PUT /timelines/:id(.:format) timelines#update > DELETE /timelines/:id(.:format) timelines#destroy > root GET / timelines#index > POST /timelines(.:format) timeline#hogeing

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

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

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

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

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

guest

回答1

0

ベストアンサー

routesの記述で、パスの重複が原因で、後から指定した方がうまく解決できていないのではないかと思います。

パスにこだわりがあって、どうしてもtimeline#hogeingを/timelinesで解決したいのであれば、リソース側のcreateのパスを削除する必要があるでしょう。

Ruby

1[routes.rb] 2Rails.application.routes.draw do 3 resources :timelines, except[:create] 4 root 'timelines#index' 5 post '/' => 'timelines#hogeing' 6end

createアクションは別に定義したいし、hogeingアクションのパスにもこだわりがないのであれば、以下の様にするのが一番自然でしょう。

Ruby

1[routes.rb] 2Rails.application.routes.draw do 3 resources :timelines do 4 post :hogeing 5 end 6 root 'timelines#index' 7end 8 9[index.html.erb] 10... 11<div> 12 <%= form_for @twitter, url: hogeing_timeline_path, method: :post do |f|%> 13 <%= f.label :word, "Test" %> 14 <%= f.text_field :word %> 15 <%= f.submit '取得する' %> 16 <% end %> 17</div> 18...

投稿2016/07/01 10:27

rifuch

総合スコア1901

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

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

RyuSA

2016/07/01 12:42

ご回答ありがとうございました…!二ついただけるとは、感謝です…! 二つとも、それぞれ動いてくれました。ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問