・http://localhost:3000/posts/newへアクセスすると、ルーティングによって、new.html.erbを表示する
↓
・new.html.erb
⇒nameとcontentを入力してsubmit
↓
・posts_controller.rb
⇒new.html.erbで入力したnameとcontentを変数(@setting)に保存
↓
・result.html.erb
⇒Controllerのredirect_to '/posts/result'によって、ここに来て@settingのnameとcontentを表示する
上記を実現したいと思っています。
下記に書けるとろこまで書いてみましたが、ダメでした。
ちなみに、入力した値をデータベースへ保存するまでの方法は知っていますが、
今回はデータベースへ保存したいわけではなく、ユーザーが入力した値を一時的にどこかに保存するのがゴールです。
お手数ですが、よろしくお願いいたします。
###new.html.erb
ruby
1<h1>テキスト入力</h1> 2 3<%= form_tag('posts_path') do |f| %> 4<%= text_field_tag :name %><br/> 5<%= text_field_tag :content %><br> 6<%= submit_tag "追加" %> 7<% end %>
###posts_controller.rb
ruby
1class PostsController < ApplicationController 2 def index 3 end 4 5 def show 6 end 7 8 def result 9 end 10 11 def new 12 end 13 14 def create 15 @setting = Hash.new 16 @setting.name = params[:name] 17 @setting.content = params[:content] 18 redirect_to '/posts/result' 19 end 20 21end
###result.html.erb
ruby
1<h1>結果表示</h1> 2 3<%= @setting.name %> 4<%= @setting.content %>
###routes.erb
ruby
1Rails.application.routes.draw do 2 get 'posts/index' 3 get 'posts/show' 4 get 'posts/result' 5 get 'posts/new' 6 post "posts" => "posts#create" 7end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/20 15:30
2017/11/21 01:00 編集