rails初心者です。
現在、データの動きを見るための小さなアプリケーションをrailsで作成しています。
機能といたしましては、以下になります。
・データ登録
・登録したデータの表示
・登録したデータの編集・更新
『一度の登録・更新で別々のデータをDBに登録する』には、どうすればいいのでしょうか。
以下、ソースになります。
DBの名前は『posts』、登録しようとしているデータが入るカラム名は『profile』です。
posts_controller.rb
class PostsController < ApplicationController def index @posts = Post.all.order(id: :asc) end def show @post = Post.find_by(id:params[:id]) end def new end def create @post = Post.new(content:params[:content], profile:params[:profile]) //ここで悩んでいます。この辺りだと思うのですが…… @post.save redirect_to("/posts/index") end def edit @post = Post.find_by(id:params[:id]) end def update @post = Post.find_by(id: params[:id]) @post.content = params[:content] @post.save redirect_to("/posts/index") end def destroy @post = Post.find_by(id: params[:id]) @post.destroy redirect_to("/posts/index") end end
new.html.erb //新規登録画面
<div class="main posts-new"> <div class="container"> <h1 class="form-heading">キャラクター登録</h1> <%= form_tag("/posts/create") do%> <div class="form"> <div class="form-body"> <p>名前</p> <textarea name="content"></textarea> <p>プロフィール</p> <textarea name="profile"></textarea> //値を入れる用のテキストエリア <input type="submit" value="登録"> </div> </div> <%end%> </div> </div>
よろしくお願い致します。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。