私はPublicActivity gemを使用してアクティビティフィードシステムを作成しています。 "belongs_to:member"を削除すると、システムはポストしますが、現在のmember_idは決して挿入されません。コントローラテーブルでは、それが送信されないことがわかります(私はチュートリアルを以下AM)をパラメータとしてMEMBER_IDRuby on Rails、PublicActivity。外部キーが挿入されていません
また、私は手動でテーブルに現在のMEMBER_IDを挿入することができます
class Story < ApplicationRecord
belongs_to :member
validates :title, presence: true
validates :body, presence: true
include PublicActivity::Model
tracked
end
class Member < ApplicationRecord
を...
has_many :stories
end
class StoriesController < ApplicationController
before_action :find_story, only: [:destroy, :show, :edit, :update]
def index
@stories = Story.order('created_at DESC')
end
def new
@story = Story.new
end
def create
@story = Story.new(story_params)
if @story.save
flash[:success] = 'Your story was added!'
redirect_to stories_path
else
render 'new'
end
end
def edit
end
def update
if @story.update_attributes(story_params)
flash[:success] = 'The story was edited!'
redirect_to stories_path
else
render 'edit'
end
end
def destroy
if @story.destroy
flash[:success] = 'The story was deleted!'
else
flash[:error] = 'Cannot delete this story...'
end
redirect_to stories_path
end
def show
end
private
def story_params
params.require(:story).permit(:title, :body)
end
def find_story
@story = Story.find(params[:id])
end
before_action :load_activities, only: [:index, :show, :new, :edit]
private
def load_activities
@activities = PublicActivity::Activity.order('created_at DESC').limit(20)
end
end
移行。?:
話に関連のclass CreateStories < ActiveRecord::Migration[5.0]
def change
create_table :stories do |t|
t.column :title, :string, :default => ""
t.belongs_to :member, index: true
t.text :body
t.text :tags
t.integer :type
t.integer :category
t.string :image_path
t.datetime :created_at, :null => false
t.datetime :updated_at
t.timestamps
end
end
end
Rails.application.routes.draw do
devise_for :members
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
#resources :layouts
get 'welcome/index'
root 'welcome#index'
resources :conversations, only: [:index, :show, :destroy]
resources :messages, only: [:new, :create]
resources :stories
#root to: 'stories#index'
end
ルート:
/stories(.:format)
stories#index
POST /stories(.:format)
stories#create
new_story_path GET /stories/new(.:format)
stories#new
edit_story_path GET /stories/:id/edit(.:format)
stories#edit
story_path GET /stories/:id(.:format)
stories#show
PATCH /stories/:id(.:format)
stories#update
PUT /stories/:id(.:format)
stories#update
DELETE /stories/:id(.:format)
stories#destroy
rails console: 'rails c --sandbox'を開いて' Story.inspect'を実行できますか?また、あなたの 'レイクルート'を見せてもらえますか?文字列、型:整数、カテゴリ:整数型、イメージ型:文字列、created_at:datetime、updated_at:datetime): – DiodonHystrix
Story.inspect => "ストーリー(ID:整数、タイトル:文字列、メンバID: " – Jay
ルートはあまりにも多くの出力をここに置くことをあまりにも多く与えます – Jay