###前提・実現したいこと
お世話になります。 今、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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/07/01 12:42