form_forで@inviteをデータベースに保存することができません。
下記にrouting、controller,viewのコードをまとめました。
恐らくnewとcreate内の@inviteの書き方が間違っているとは思うのですが正解が導き出せません。
urlに関しては:idを:usernameに変化させ、そのあとinviteをネストさせた形になっています。
privateでparamsをリファクタリングしていない点はご容赦ください。
回答いただけたら幸いです。
routes
1new_user_invite GET /:user_username/invites/new(.:format) invites#new
InvitesController
1class InvitesController < ApplicationController 2 3 def new 4 @user = User.find_by(username: params[:user_username]) 5 @invite = Invite.new 6 end 7 8 def create 9 @invite = Invite.new(fromid: params[:user_id], content: params[:content], title: params[:title]) 10 @invite.save 11 redirect_to user_invite_path(id: @invite.id) 12 end 13 14 def show 15 @user = User.find_by(username: params[:user_username]) 16 end 17end
new
1<%= form_for([@user, @invite]) do |f| %> 2 <%= f.label :title %> 3 <%= f.text_field :title %> 4 5 <%= f.label :content %> 6 <%= f.text_field :content %> 7 8 <%= hidden_field_tag :user_id, @user.id %> 9 <%= f.submit "投稿する"%> 10<% end %>
submitを実行すると
ActionController::UrlGenerationError in InvitesController#create
No route matches {:action=>"show", :controller=>"invites", :id=>nil, :user_username=>"tarou"}, possible unmatched constraints: [:id]
parameters
{"utf8"=>"✓",
"authenticity_token"=>"*********************",
"invite"=>{"title"=>"タイトル", "content"=>"コンテンツ"},
"user_id"=>"4",
"commit"=>"投稿する",
"user_username"=>"tarou"}
と表示されました。
idが付与されていないのは恐らく@inviteが保存されていないことが原因だと思われます。
<-------------------------------------------追記------------------------------------------>
invite.rb
invite.rb
1class Invite < ApplicationRecord 2 has_many :messages, foreign_key: "fromid" 3 belongs_to :users 4end 5
user.rb
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable has_many :messages, foreign_key: "toid" has_many :invites, foreign_key: "fromid" devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :lockable, :timeoutable, :omniauthable def self.find_for_oauth(auth) user = User.where(uid: auth.uid, provider: auth.provider).first unless user user = User.create( uid: auth.uid, provider: auth.provider, email: User.dummy_email(auth), username: auth.info.nickname, password: Devise.friendly_token[0, 20], image: auth.info.image ) end user end def to_param username end private def self.dummy_email(auth) "#{auth.uid}-#{auth.provider}@example.com" end end
schema.rb
# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema.define(version: 2018_12_06_044716) do create_table "invites", force: :cascade do |t| t.integer "fromid" t.text "content" t.text "title" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "messages", force: :cascade do |t| t.integer "fromid" t.integer "toid" t.text "content" t.text "title" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.string "confirmation_token" t.datetime "confirmed_at" t.datetime "confirmation_sent_at" t.string "unconfirmed_email" t.integer "failed_attempts", default: 0, null: false t.string "unlock_token" t.datetime "locked_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "provider" t.string "uid" t.string "username" t.string "image" t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true end end
回答2件
あなたの回答
tips
プレビュー