Twitterログインからidを取得し、フォームからuserと一対多のpostを投稿したいのですが、createアクションにて取得できません。
createアクションのsave前でbinding.pryを行うと、以下のようにimageとuser_idが取得できていません。controllerのactionの書き方が間違っているのでしょうか?
[1] pry(#<PostsController>)> @post => #<Post:0x007f903269a6a8 id: nil, name: "a", body: "a", tec: "a", point: "a", link: "a", created_at: nil, updated_at: nil, image: nil, user_id: nil>
なおbelongs_to :userにoptional: trueをつける前はUser not existとエラーが出ていました。
主に使っているgemが以下です
# Image Uploader gem 'carrierwave' gem 'rmagick', require: 'RMagick' gem 'omniauth' gem 'omniauth-twitter'
コードは以下です。
indexはすべてのPostを表示させたいです。
ruby
1class PostsController < ApplicationController 2 before_action :set_post, only: [:show, :edit, :update, :destroy] 3 4 # GET /posts 5 # GET /posts.json 6 def index 7 @posts = Post.all 8 end 9 10 # GET /posts/1 11 # GET /posts/1.json 12 def show 13 end 14 15 # GET /posts/new 16 def new 17 @post = Post.new 18 end 19 20 # GET /posts/1/edit 21 def edit 22 end 23 24 # POST /posts 25 # POST /posts.json 26 def create 27 @post = Post.new(post_params) 28 binding.pry 29 30 respond_to do |format| 31 if @post.save 32 format.html { redirect_to @post, notice: 'Post was successfully created.' } 33 format.json { render :show, status: :created, location: @post } 34 else 35 format.html { render :new } 36 format.json { render json: @post.errors, status: :unprocessable_entity } 37 end 38 end 39 end 40 41 # PATCH/PUT /posts/1 42 # PATCH/PUT /posts/1.json 43 def update 44 respond_to do |format| 45 if @post.update(post_params) 46 format.html { redirect_to @post, notice: 'Post was successfully updated.' } 47 format.json { render :show, status: :ok, location: @post } 48 else 49 format.html { render :edit } 50 format.json { render json: @post.errors, status: :unprocessable_entity } 51 end 52 end 53 end 54 55 # DELETE /posts/1 56 # DELETE /posts/1.json 57 def destroy 58 @post.destroy 59 respond_to do |format| 60 format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' } 61 format.json { head :no_content } 62 end 63 end 64 65 private 66 # Use callbacks to share common setup or constraints between actions. 67 def set_post 68 @post = Post.find(params[:id]) 69 end 70 71 # Never trust parameters from the scary internet, only allow the white list through. 72 def post_params 73 params.require(:post).permit(:name, :user_id, :image, :body, :tec, :point, :link) 74 end 75end
class SessionsController < ApplicationController def create user = User.find_or_create_from_auth_hash(request.env['omniauth.auth']) # request.env['omniauth.auth']に、OmniAuthによってHashのようにユーザーのデータが格納されている。 session[:user_id] = user.id redirect_to root_path, notice: 'ログインしました' end def destroy session[:user_id] = nil redirect_to root_path end end
html
1<%= form_with(model: post, local: true) do |form| %> 2 <% if post.errors.any? %> 3 <div id="error_explanation"> 4 <h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2> 5 6 <ul> 7 <% post.errors.full_messages.each do |message| %> 8 <li><%= message %></li> 9 <% end %> 10 </ul> 11 </div> 12 <% end %> 13 14<div class="form"> 15 <table> 16 <tbody> 17 <%= form_for @post do |f| %> 18 <div class="field"> 19 <tr> 20 <th><%= f.label :TOP画像, class:"image_label" %></th> 21 <td><%= f.file_field :image, class:"image" %></td> 22 </tr> 23 </div> 24 25 <tr> 26 <div class="field"> 27 <th><%= f.label :サービス名 %></th> 28 <td><%= f.text_field :name, id: :post_name %></td> 29 </div> 30 </tr> 31 32 <tr> 33 <div class="field"> 34 <th><%= f.label :どんなサービス? %></th> 35 <td><%= f.text_area :body, id: :post_body %></td> 36 </div> 37 </tr> 38 39 <tr> 40 <div class="field"> 41 <th><%= f.label :どんな技術が使われている? %></th> 42 <td><%= f.text_area :tec, size: "20×10", id: :post_tec %></td> 43 </div> 44 </tr> 45 46 <tr> 47 <div class="field"> 48 <th><%= f.label :どこがオススメ? %></th> 49 <td><%= f.text_area :point, id: :post_point %></td> 50 </div> 51 </tr> 52 53 <tr> 54 <div class="field"> 55 <th><%= f.label :LINK %></th> 56 <td><%= f.text_area :link, id: :post_link %></td> 57 </div> 58 </tr> 59 60 <tr> 61 <div class="actions"> 62 <th></th> 63 <% f.hidden_field :user_id, value: @user.id %> 64 <td><%= form.submit %></td> 65 </div> 66 </tr> 67 <% end %> 68 </tbody> 69 </table> 70<% end %>
ruby
1class User < ApplicationRecord 2 has_many :posts 3 4 #引数に関連するユーザーが存在すればそれを返し、存在しまければ新規に作成する 5 def self.find_or_create_from_auth_hash(auth_hash) 6#OmniAuthで取得した各データを代入していく 7 provider = auth_hash[:provider] 8 uid = auth_hash[:id] 9 nickname = auth_hash[:info][:nickname] 10 image_url = auth_hash[:info][:image].to_s.sub('normal', 'bigger') 11 12 User.find_or_create_by(provider: provider, id: uid) do |user| 13 user.nickname = nickname 14 user.image_url = image_url 15 end 16 end 17end
ruby
1class Post < ApplicationRecord 2 mount_uploader :image, ImageUploader 3 belongs_to :user, optional: true 4end
ruby
1ActiveRecord::Schema.define(version: 20180701050628) do 2 3 create_table "posts", force: :cascade do |t| 4 t.string "name" 5 t.text "body" 6 t.text "tec" 7 t.text "point" 8 t.text "link" 9 t.datetime "created_at", null: false 10 t.datetime "updated_at", null: false 11 t.text "image" 12 t.integer "user_id" 13 end 14 15 create_table "users", force: :cascade do |t| 16 t.datetime "created_at", null: false 17 t.datetime "updated_at", null: false 18 t.string "provider" 19 t.string "nickname" 20 t.string "image_url" 21 end 22 23end
ruby
1class ApplicationController < ActionController::Base 2 protect_from_forgery with: :exception 3 before_action :check_login 4 5 6 7 private 8 def check_login 9 if session[:user_id] 10 @user = User.find_by(id: session[:user_id]) 11 end 12 end 13end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/10 06:48 編集
退会済みユーザー
2018/07/10 13:13
2018/07/12 01:10 編集