2017-04-10 39 views
1

我正在使用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 
+0

你可以打开rails控制台:'rails c --sandbox'并运行'Story.inspect'?你也可以向我们展示你的'耙路线'吗? – DiodonHystrix

+0

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

+0

路由给出了一堆输出太多,放在这里 – Jay

回答

0

如果你想添加current_member ID在故事的表,然后在创建你可以做it.either手动分配@story.member_id = current_member.id或更合适的方法进行,这将是@story = current_member.stories.build(story_params)行动。

如果您需要在故事中添加成员标识(不是current_member标识,而是一个不同的成员),则从视图发送它。您可以使用hidden_field将member_id作为参数与其他表单数据一起发送,并将white list发送到故事参数中。

+0

好的,谢谢,目前的行是@ story = Story.new(story_params) 如何添加你的(第二)代码?最引人注目的是“新”标签 – Jay