2015-10-19 20 views
-1

我正在使用Rails API。目前我想通過來自我的設備的發佈請求保存對象。請求正常工作,但將問題保存到數據庫時出現問題。 Rails的說:Rails中的未知屬性 - 創建新對象

ActiveRecord::UnknownAttributeError (unknown attribute 'idea_id' for IdeaTag.): 
    app/controllers/data_controller.rb:42:in `update_ideas' 

所以,我知道這意味着它不能在「IdeaTag」查找屬性「idea_id」。

赫雷什我data_controller.rb

class DataController < ApplicationController 
    skip_before_filter :verify_authenticity_token 

    def token 
    name=params[:owner] 
    password=params[:password] 

    @owner = Owner.authenticate_by_name(name, password) 
    if @owner 
     if @owner.user_uuid.blank? 
     @user = User.new 
     @user.token = SecureRandom.uuid 
     @user.name = @owner.name 
     @user.owner_uuid = @owner.uuid 
     @user.created_at = Time.now 
     @user.updated_at = Time.now 
     @user.isLoggedIn = false 
     @user.save! 
     end 

     @user = User.find_by_uuid(@owner.user_uuid) 
     if @user.token.blank? 
     token = SecureRandom.uuid 
     @user.token = token 
     @user.save 
     end 
    else 
    render nothing: true, status: :unauthorized 
    end 
    end 

    def update_ideas 
    uuid = params[:uuid] 
    text = params[:text] 
    title = params[:title] 
    owner_uuid = params[:owner_uuid] 
    tag_id_1 = params[:tag_id_1] 
    tag_id_2 = params[:tag_id_2] 
    tag_id_3 = params[:tag_id_3] 
    tag_id_4 = params[:tag_id_4] 
    updated_at = params[:timeStamp] 
    @idea = Idea.new(:uuid => uuid, :text => text, :title => title, :owner_uuid => owner_uuid, :tag_ids => [tag_id_1, tag_id_2, tag_id_3, tag_id_4], :updated_at => updated_at) 
    @idea.save! 
    render json: {:status => 200} 
    end 

    def getjson 
    token = params[:token] 
    @user = User.find_by_token(token) 
    @users = User.all 

    @owner = Owner.find_by_user_uuid(@user.uuid) 
    @Owners = Owner.all 

    ownerUuid = @owner.uuid 

    @tags=Tag.all 
    @ideas=Idea.where(:owner_uuid => ownerUuid) 
    @votes=Vote.all 
    @votings=Voting.all 
    end 

    # def token_auth 
    # token = params[:token] 
    # @owner = Owner.find_by_token(token) 
    # if @owner 
    #  update_ideas 
    # end 
    # end 

end 

在方法 「update_ideas」 下面的行中出現錯誤

@idea = Idea.new(:UUID => UUID,:文本=> TE ...

理念型號:

class Idea < ActiveRecord::Base 
    self.primary_key = :uuid 
    has_many :idea_tags 
    has_many :tags, through: :idea_tags 
    belongs_to :voting 
    has_many :votes 
    belongs_to :owner 
end 

理念遷移文件

class CreateIdeas < ActiveRecord::Migration 
    def change 
    create_table :ideas, :id => false, :primary_key => :uuid do |i| 
     i.string :uuid 
     i.string :title 
     i.string :text 
     i.string :owner_uuid 
     i.string :voting_uuid 
     i.datetime :created_at 
     i.datetime :updated_at 
    end 
    end 
end 

如何保存這樣的合適的對象?

+1

你向我們展示'ideas',但該表沒有任何與錯誤,這是關於'idea_tags'缺少的列。 – meagar

+0

向我們展示您的IdeaTag遷移,它必須錯過idea_id列 – Caillou

回答