###開発環境
- MacOS X 10.11 El Capitan
- Ruby 2.3.1
- Ruby on Rails 4.2.6
###発生している問題
Railsでデータベースにデータを保存する方法は、以下の流れの方法しかないのでしょうか?
- ビューにフォームを作る
- フォームに入力した内容(パラーメータ)をコントローラーで Hoge.create(パラメータ) を使ってデータベースに保存
他にデータベースへ保存する方法があればご教示ください。
よろしくお願いします。
※以下追記
現在ECサイトを開発しております。各商品(product)のページに複数のレビュー(review)を投稿して、複数のコメント(comment)が出来るようにアソシエーションを組みました。そして、commentsテーブルにreview_idカラムを作成し、reviewsテーブルのidをreview_idに保存したいのですが、コントローラーやビューで試行錯誤しても、うまくいかないため、質問をさせて頂きました。下記にソースコードを掲載致しました。
ビューは以下のとおりです。
Ruby
1# app/views/products/show.html.erb 2 3<h1>商品詳細</h1> 4 <%= @product.price %> 5 <%= @product.description %> 6 <%= image_tag @product.image.url %> 7 8 <%= form_tag(:controller => :products, :action => :add) do %> 9 <%= select_tag :item, options_for_select(["01","02","03","04","05"]) %> 10 <%= submit_tag "Add Cart" %> 11 <% end %> 12 13<%= link_to '編集', "/products/#{@product.id}/edit", method: :get %> 14<%= link_to '削除', "/products/#{@product.id}", method: :delete %> 15<% if session[:user_id] %> 16<h2>レビュー投稿</h2> 17 <%= form_for(@review_new, :url => {:controller => :reviews, :action => :create}) do |f| %> 18 <%= f.text_area :text %> 19 <%= f.hidden_field :user_id, :value => "#{session[:user_id]}" %> 20 <%= f.hidden_field :name, :value => "#{session[:name]}" %> 21 <%= f.hidden_field :product_id, :value => "#{@product.id}" %> 22 <%= f.submit %> 23 <% end %> 24<% end %> 25<h2>ユーザーレビュー</h2> 26<% if @reviews.nil? %> 27 まだユーザーレビューはありません。 28<% else %> 29<% @reviews.each do |r| %> 30 ユーザー名:<%= r.name %> 31 レビュー文:<%= r.text %> 32 <%= link_to 'コメントする', "/comments/new", method: :get %> 33 <% r.comments.each do |c| %> 34 コメント:<%= c.text %> 35 <% end %> 36<% end %> 37<% end %>
Ruby
1# app/views/comments/new.html.erb 2 3<h1>コメント投稿</h1> 4<%= form_for(@comment_new, :url => {:controller => :comments, :action => :create}) do |f| %> 5 <%= f.text_area :text %> 6 <%= f.hidden_field :user_id, :value => session[:user_id] %> 7 <%= f.hidden_field :name, :value => session[:name] %> 8 <%= f.hidden_field :product_id, :value => session[:product_show_id] %> 9 <%= f.submit %> 10<% end %>
コントローラーは以下のとおりです。
Ruby
1# app/controllers/products_controller.rb 2 3class ProductsController < ApplicationController 4 5 def index 6 @product_all = Product.all 7 end 8 9 def new 10 @product_new = Product.new 11 @review =Review.new 12 end 13 14 def create 15 Product.create(product_params_create) 16 redirect_to :action => "index" 17 end 18 19 def show 20 @product = Product.find(params[:id]) 21 session[:cart] ||= {} 22 session[:cart]["#{params[:id]}"] = Product.find(params[:id]) 23 @review_new = Review.new 24 session[:product_show_id] = params[:id] 25 @comment_all = Comment.all 26 @reviews = @product.reviews 27 end 28 29 def add 30 if session[:item] == nil then 31 session[:item] ||= {} 32 session[:item]["#{params[:id]}"] = params[:item] 33 else 34 session[:item]["#{params[:id]}"] = params[:item].to_i + session[:item]["#{params[:id]}"].to_i 35 end 36 end 37 38 39 def content 40 end 41 42 def destroy 43 product = Product.find(params[:id]) 44 product.destroy 45 end 46 47 def edit 48 @product_edit = Product.find(params[:id]) 49 end 50 51 def update 52 @product_update = Product.find(params[:id]) 53 @product_update.update(product_params_update) 54 redirect_to :action => "index" 55 end 56 57 def search 58 @products = Product.all 59 if params[:description].present? 60 @products = @products.get_by_description params[:description] 61 end 62 end 63 64private 65 def product_params_create 66 params.require(:product).permit(:price, :description, :image) 67 end 68 69 def product_params_update 70 params.permit(:price, :description, :image) 71 end 72 73end
Ruby
1# app/controllers/reviews_controller.rb 2 3class ReviewsController < ApplicationController 4 5 def index 6 @reviews = Review.all 7 end 8 9 def new 10 @review_new = Review.new 11 end 12 13 def create 14 review = Review.create(review_params_create) 15 redirect_to controller: "products", action: "show", id: "#{session[:product_show_id]}" 16 end 17 18 def edit 19 @review_edit = Review.find(params[:id]) 20 end 21 22 def update 23 @review_update = Review.find(params[:id]) 24 @review_update.update(review_params_update) 25 redirect_to controller: "products", action: "show", id: "#{session[:product_show_id]}" 26 end 27 28 def destroy 29 Review.find(params[:id]).destroy 30 redirect_to controller: "products", action: "show", id: "#{session[:product_show_id]}" 31 end 32 33 private 34 def review_params_create 35 params.require(:review).permit(:text, :user_id, :name, :product_id) 36 end 37 38 def review_params_update 39 params.require(:review).permit(:text) 40 end 41end
Ruby
1# app/controllers/comments_controller.rb 2 3class CommentsController < ApplicationController 4 5 def index 6 @comment_index = Comment.all 7 end 8 9 def new 10 @comment_new = Comment.new 11 end 12 13 def create 14 session[:comment] = Comment.create(comment_params_create) 15 email = User.find(session[:user_id]).email 16 name = session[:name] 17 url = "" 18 if ENV["RAILS_ENV"] == "development" 19 url = "http://localhost:3000/products/#{session[:product_id]}" 20 else 21 url = "https://slup-app.herokuapp.com/products/#{session[:product_id]}" 22 end 23 NoticeNewComment.notice_new_comment(name, url, email).deliver_now 24 redirect_to controller: "products", action: "show", id: "#{session[:product_show_id]}" 25 end 26 27 private 28 def comment_params_create 29 params.require(:comment).permit(:text, :user_id, :review_id) 30 end 31end
モデルは以下のとおりです。
Ruby
1# app/models/comment.rb 2 3class Comment < ActiveRecord::Base 4 belongs_to :user 5 belongs_to :review 6end
Ruby
1# app/models/product.rb 2 3class Product < ActiveRecord::Base 4 mount_uploader :image, ImageUploader 5 #ユーザー名による絞り込み 6 scope :get_by_description, ->(description) {where("description like ?", "%#{description}%")} 7 has_many :reviews 8end
Ruby
1# app/models/review.rb 2 3class Review < ActiveRecord::Base 4 belongs_to :user 5 belongs_to :product 6 has_many :comments 7end
Ruby
1# app/models/user.rb 2 3class User < ActiveRecord::Base 4 has_secure_password 5 validates :name, presence: true, :uniqueness => true 6 validates :email, presence: true, :uniqueness => true 7 has_many :reviews 8 has_many :comments 9end
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/03 03:51
2017/07/03 07:24 編集
2017/07/03 07:14
2017/07/03 07:25
2017/07/03 08:20
2017/07/03 08:27
2017/07/03 12:00
2017/07/03 12:37 編集