railでフリマサイトを作成しています。商品投稿一覧の実装中です。
エラーが解決できません。よろしくおねがいいたします。
products_controller.rb
class ProductsController < ApplicationController
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
@product.status = 0
if params[:product][:image1] upload_file1 = params[:product][:image1] upload_file_name1 = upload_file1.original_filename output_dir = Rails.root.join('public', 'images') output_path = output_dir + upload_file_name1 File.open(output_path, 'w+b') do |f| f.write(upload_file1.read) end @product.image1 = upload_file_name1 end if params[:product][:image2] upload_file2 = params[:product][:image2] upload_file_name2 = upload_file2.original_filename output_dir = Rails.root.join('public', 'images') output_path = output_dir + upload_file_name2 File.open(output_path, 'w+b') do |f| f.write(upload_file2.read) end @product.image2 = upload_file_name2 end if params[:product][:image3] upload_file3 = params[:product][:image3] upload_file_name3 = upload_file3.original_filename output_dir = Rails.root.join('public', 'images') output_path = output_dir + upload_file_name3 File.open(output_path, 'w+b') do |f| f.write(upload_file3.read) end @product.image3 = upload_file_name3 end if @product.save flash[:success] = "出品しました。" redirect_to profiles_path and return else flash[:danger] = "出品に失敗しました。" redirect_to products_new_path end
end
def show
render "users/products"
@product = Product.find_by(id:params[:id])
end
def index
@product = Product.find(params[:id])
@product = Product.all.order(created_at: :asc)
@product = Product.select("image1") @product = Product.select("goods_name") @product = Product.select("goods_price")
end
private
def product_params
params.require(:product).permit(
:goods_name,
:goods_ex,
:goods_select,
:goods_price,
:image1,
:image2,
:image3
)
end
end
products.html.erb
<div class="main"> <div class="main2"> <div class="product"> <%= image_tag "pen.png", width: "150px" %> <div class="p_deta"></div> </div> <%= render partial: 'users/mypage' %><% @product.each do |p| %> <%= p.goods_name %> <!--<p class="goods_name">ペン</p>--> <p class="value">$20</p> <% end %> </div> </div>
product.rb
class Product < ApplicationRecord
belongs_to :category, dependent: :destroy
belongs_to :user
validates :goods_name, presence: true
validates :goods_ex, presence: true
validates :goods_price, presence: true
validates :goods_select, presence: true
end
routes.rb
Rails.application.routes.draw do
get '/users/products/new', to:'users#products_new', as: :products_new
post '/users/products/new', to: 'users#products_new'
get '/users/products/' => 'products#show'
post 'users/products/new' => 'products#creat'
get '/users/products', to:'users#products', as: :products
get '/users/products', to:'products#index'
get '/users/products/(:id)', to:'users#item', as: :item
get '/users/products/(:id)/edit', to:'users#edit', as: :edit
end
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/05/21 04:20