我正在使用PublicActivity gem創建活動供稿系統。然而,「member_id」永遠不會作爲外鍵插入到「stories:表」中,如果我刪除「belongs_to:member」,系統將會發布,但是當前的member_id永遠不會被插入到控制器表中,您可以看到它永遠不會發送。在member_id作爲一個參數(我下面的教程)Ruby 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控制檯:'rails c --sandbox'並運行'Story.inspect'?你也可以向我們展示你的'耙路線'嗎? – DiodonHystrix
Story.inspect =>「Story(id:integer,title:string,member_id:integer,body:text,tags:text,type:integer,category:integer,image_path:string,created_at:datetime,updated_at:datetime) 「 – Jay
路由給出了一堆輸出太多,放在這裏 – Jay