0

我正在尝试找出在创建项目后获取项目 ID 并将该 ID 传递到另一个表的最佳方法。即用户创建了一个保存到团队数据库中的团队,我想将其分配team_id到我的用户表中。

我尝试了以下但没有成功

    def create
    @team = Team.new(params[:team])
    @user = current_user
    respond_to do |format|
      if @team.save
        format.html { redirect_to(teams_url,
                                  :notice => "Team #{@team.name} was successfully created.") }
        format.json { render :json => @team, :status => :created, :location => @team }
        @user.update_attributes(params[:user][@team.id])
      else
        format.html { render :action => "new" }
        format.json { render :json => @team.errors, :status => :unprocessable_entity }
      end
    end
  end

团队模式

class Team < ActiveRecord::Base
  has_many  :users
  has_many  :events
  belongs_to :division
  belongs_to :sport
  attr_accessible :name, :division_id, :sport_id
  validates :name, :presence => true

end

用户模型

class User < ActiveRecord::Base


devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable

  belongs_to :sport
  belongs_to :team
  has_many :availabilities

sql执行

    Started POST "/teams" for 127.0.0.1 at 2013-02-24 15:24:40 +1100
Processing by TeamsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"ztr8E+jg3hCe3cQoDefS3Rw5GQJGZfHsffffbCZiGRs=", "team"=>{"sport_id"=>"19", "division_id"=>"", "name"=>"test"}, "commit"=>"Create Team"}
  User Load (2.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  User Load (3.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
   (0.2ms)  BEGIN
  SQL (0.9ms)  INSERT INTO "teams" ("created_at", "division_id", "name", "sport_id", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["created_at", Sun, 24 Feb 2013 15:24:40 EST +11:00], ["division_id", nil], ["name", "test"], ["sport_id", 19], ["updated_at", Sun, 24 Feb 2013 15:24:40 EST +11:00]]
   (1.7ms)  COMMIT
   (0.2ms)  BEGIN
   (0.4ms)  ROLLBACK
Redirected to http://localhost:3000/teams
4

3 回答 3

0

最简单的修复方法是修复传递给 update_attributes 的参数

@user.update_attributes(:team_id => @team.id)

那时,params 散列没有参与其中。它主要包含来自 GET/POST 变量的信息,例如来自您的表单的 params[:team]。

更新

看到您的模型后,如果不使 team_id 可访问,update_attributes 将无法工作,这是一个安全问题,通常不应使外键可访问。而是将 update_attributes 替换为分配。

def create
  @team = Team.new(params[:team])
  respond_to do |format|
    if @team.save
      format.html { redirect_to(teams_url, :notice => "Team #{@team.name} was successfully created.") }
      format.json { render :json => @team, :status => :created, :location => @team }
      # use assignment instead to avoid mass assignment errors
      current_user.team = @team
    else
      format.html { render :action => "new" }
      format.json { render :json => @team.errors, :status => :unprocessable_entity }
    end
  end
end
于 2013-02-24T02:56:33.883 回答
0

这个问题应该使用活动记录关联来解决。http://guides.rubyonrails.org/association_basics.html
这是将一个项目 id 存储到不同数据库的另一个项目的最佳方法。所以尝试使用活动记录关联。
更新

    if @team.save
    @user.update_attributes(:team_id => @team.id)
    format.html { redirect_to(teams_url,
                                  :notice => "Team #{@team.name} was successfully created.") }
   format.json { render :json => @team, :status => :created, :location => @team }

保存后而不是重定向后使用更新代码。

于 2013-02-24T03:59:47.657 回答
0

以下3个选项

def create
  @team = Team.new(params[:team])
  saved = false

  Team.transaction do
    saved = @team.save
    if saved
      # 1) this should work ?
      @team.users << current_user
      # 2) if that doesn't then this most def
      current_user.team = @team
      current_user.save!
      # 3) or 
      current_user.update_attribute(:team_id, @team.id)
    end
  end

  respond_to do |format|
    if saved        
      format.html { redirect_to(teams_url, :notice => "Team #{@team.name} was successfully created.") }
      format.json { render :json => @team, :status => :created, :location => @team }        
    else
      format.html { render :action => "new" }
      format.json { render :json => @team.errors, :status => :unprocessable_entity }
    end
  end     
end
于 2013-02-24T04:53:50.700 回答