いつもお世話になっております。
この度はfields_forにて送信した画像データが表示されない問題について質問させて頂きます。
画像はcarrierwaveを導入済みです。
createアクションの一部部分
respond_to do |format| if @twi.save && @picture.save format.html { redirect_to '/twis', notice: 'Twi was successfully created.' } format.json { render :show, status: :created, location: @twi } else format.html { render :new } format.json { render json: @twi.errors, status: :unprocessable_entity } end end
にて、
@pictureは無事saveされ redirect_to '/twis' によってページが遷移するのですが、
ターミナルを確認したところ、どうもこのsaveとページ遷移の間で問題が生じているみたいです・・・
以下問題のあるターミナルの処理コードを貼らせて頂きます。
Started POST "/twis" for 124.141.117.247 at 2018-02-16 14:18:31 +0000 Cannot render console from 124.141.117.247! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by TwisController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"uNwKcn8UbJbtM6WIX9jzU2SI/f5jmfF4f/sr5iXkt7V0JbBAcK+zfDu5qLcY9QQaPjLCcZGfXKMAJItar5w/Lw==", "picture"=>{"twiimage"=>"images-1.jpg", "twi_id"=>"", "user_id"=>""}, "twi"=>{"msg"=>"テスト", "user_id"=>"1"}, "commit"=>"投稿する"} Account Load (0.4ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = ? ORDER BY "accounts"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] User Load (0.4ms) SELECT "users".* FROM "users" WHERE (account_id == 1) (0.1ms) begin transaction User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] SQL (0.4ms) INSERT INTO "twis" ("msg", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["msg", "テスト"], ["user_id", 1], ["created_at", "2018-02-16 14:18:31.741657"], ["updated_at", "2018-02-16 14:18:31.741657"]] (81.7ms) commit transaction (0.1ms) begin transaction SQL (0.6ms) INSERT INTO "pictures" ("created_at", "updated_at", "twiimage") VALUES (?, ?, ?) [["created_at", "2018-02-16 14:18:31.826876"], ["updated_at", "2018-02-16 14:18:31.826876"], ["twiimage", nil]] (14.0ms) commit transaction Redirected to https://work-space-wataru1202.c9users.io/twis
"picture"=>{"twiimage"=>"images-1.jpg" でtwiimageにデータが格納されているのですが、
最後の方に["twiimage", nil]] とtwiimageデータに対してnilが表示されています・・・
理由がさっぱりわからないのでわかる方おりましたらご教示お願いいたします。。
各モデル情報
Twiモデル msg:text user_id:integer has_many :picture belongs_to :user Pictureモデル twiimage:string user_id:integer twi_id:integer belongs_to :twi, optional: true mount_upload :twiimage,TwiimageUploader
入力フォーム
<%= form_with(model:twi, local: true) do |form| %> <table> <tr> <th>ユーザー名</th> <td><%= @user.name %></td> </tr> <%= fields_for @picture do |p| %> <tr> <th><%= p.label :画像 %></th> <td><%= p.file_field :twiimage %></td> </tr> <%= p.hidden_field :twi_id %> <%= p.hidden_field :user_id %> <% end %> <tr> <th><%= form.label :呟き %></th> <td><%= form.text_area :msg, cols:40,rows:3 %></td> </tr> <%= form.hidden_field :user_id %> <tr> <th></th><td><%= form.submit '投稿する' %></td> </tr> </table> <% end %>
newアクション
def new users = User.where 'account_id == ?', current_account.id if users[0] == nil then user = User.new user.account_id = current_account.id user.save users = User.where 'account_id == ?', current_account.id end @user = users[0] @twi = Twi.new @twi.user_id = @user.id @picture = Picture.new @picture.twi_id = @twi.id @picture.user_id = @twi.user_id end
createアクション
def create @twi = Twi.new(twi_params) @picture = Picture.new(picture_params) @user = User.where('account_id == ?',current_account.id)[0] respond_to do |format| if @twi.save && @picture.save format.html { redirect_to @twi, notice: 'Twi was successfully created.' } format.json { render :show, status: :created, location: @twi } else format.html { render :new } format.json { render json: @twi.errors, status: :unprocessable_entity } end end end def twi_params params.require(:twi).permit(:msg, :user_id) end def picture_params params.require(:picture).permit(:twiimage, :twi_id, :user_id) end
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。