rails 5.1.4
ruby 2.4.1
carrierwave (1.2.1, 0.10.0)
carrierwave-postgresql (0.2.0)
RoRにてcarrierwaveをしようしてpostgresに画像を保存するアプリを作成中です。
なんとか、画像をblobで保存できるところまで来たのですが、うまく取り出し方がわかりません、oidの文字列かバイナリーデータの形で取り出してしまっているようです。
上手な取り出し方があればお教えください。
users_controller.rb
class
1 before_action :set_user, only: [:show, :edit, :update, :destroy] 2 3 # GET /users 4 # GET /users.json 5 def index 6 @users = User.all 7 end 8 9 # GET /users/1 10 # GET /users/1.json 11 def show 12 13 end 14 15 # GET /users/new 16 def new 17 @user = User.new 18 end 19 20 # GET /users/1/edit 21 def edit 22 end 23 24 # POST /users 25 # POST /users.json 26 def create 27 @user = User.new(user_params) 28 29 respond_to do |format| 30 if @user.save 31 format.html { redirect_to @user, notice: 'User was successfully created.' } 32 format.json { render :show, status: :created, location: @user } 33 else 34 format.html { render :new } 35 format.json { render json: @user.errors, status: :unprocessable_entity } 36 end 37 end 38 end 39 40 # PATCH/PUT /users/1 41 # PATCH/PUT /users/1.json 42 def update 43 respond_to do |format| 44 if @user.update(user_params) 45 format.html { redirect_to @user, notice: 'User was successfully updated.' } 46 format.json { render :show, status: :ok, location: @user } 47 else 48 format.html { render :edit } 49 format.json { render json: @user.errors, status: :unprocessable_entity } 50 end 51 end 52 end 53 54 # DELETE /users/1 55 # DELETE /users/1.json 56 def destroy 57 @user.destroy 58 respond_to do |format| 59 format.html { redirect_to users_url, notice: 'User was successfully destroyed.' } 60 format.json { head :no_content } 61 end 62 end 63 64 private 65 # Use callbacks to share common setup or constraints between actions. 66 def set_user 67 @user = User.find(params[:id]) 68 end 69 70 # Never trust parameters from the scary internet, only allow the white list through. 71 def user_params 72 params.require(:user).permit(:name, :password, :avatar) 73 end 74end 75 76
index.html.erb
<h1>Users</h1> <table> <thead> <tr> <th>Name</th> <th>Password</th> <th colspan="3"></th> </tr> </thead> <tbody> <% @users.each do |user| %> <tr> <td><%= user.name %></td> <td><%= user.password %></td> <td><%= user.avatar %></td> <td><%= link_to 'Show', user %></td> <td><%= link_to 'Edit', edit_user_path(user) %></td> <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </tbody> </table> <br> <%= link_to 'New User', new_user_path %> コード
show.html.erb
<p> <strong>Name:</strong> <%= @user.name %> </p> <p> <strong>Password:</strong> <%= @user.password %> </p> <p> <strong>avatar:</strong> <%= @user.avatar.file.read %> </p> <%= link_to 'Edit', edit_user_path(@user) %> | <%= link_to 'Back', users_path %>
schema.rb
ActiveRecord::Schema.define(version:
1 2 # These are extensions that must be enabled in order to support this database 3 enable_extension "plpgsql" 4 5 create_table "users", force: :cascade do |t| 6 t.string "name" 7 t.string "password" 8 t.datetime "created_at", null: false 9 t.datetime "updated_at", null: false 10 t.oid "avatar" 11 end 12 13end 14
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/21 00:32
2017/12/21 00:35
2017/12/21 00:55
2017/12/23 12:38
2017/12/24 03:08