1

我已经通过关联使用 has_many 设置了成员和团队模型。

成员.rb

has_many :teams, :through => :team_members
has_many :team_members

团队.rb

has_many :members, :through => :team_members
has_many :team_members

团队成员.rb

belongs_to :member
belongs_to :team

当我尝试创建一个新团队时,我收到此错误:

undefined method `name' for nil:NilClass

参数是:

{"utf8"=>"✓",
 "authenticity_token"=>"aXpMxWxGlhogfn9EbBWciSjoMrYXbPxG8Kzha14na58=",
 "team"=>{"name"=>"Ruby",
 "email"=>"email@email.com",
 "language"=>"En",
 "link"=>"",
 "logo"=>#<ActionDispatch::Http::UploadedFile:0xb3907f0 @original_filename="You-Are-Great-.gif",
 @content_type="image/gif",
 @headers="Content-Disposition: form-data; name=\"team[logo]\"; filename=\"You-Are-Great-.gif\"\r\nContent-Type: image/gif\r\n",
 @tempfile=#<File:/tmp/RackMultipart20120723-1907-m3bi79>>},
 "commit"=>"Create Team"}

team_controller.rb 中的 create 方法是:

@team = Team.new(params[:team])

除非我手动分配属性,否则不会创建团队,例如

@team = Team.new(:name => params[:team][:name], :email => params[:team][:email]...)

所以!任何想法为什么?

编辑:

团队控制器.rb:

class TeamsController < ApplicationController
  # GET /teams
  # GET /teams.json
  def index
    @teams = Team.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @teams }
    end
  end

  # GET /teams/1
  # GET /teams/1.json
  def show
    @team = Team.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @team }
    end
  end

  # GET /teams/new
  # GET /teams/new.json
  def new
    @team = Team.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @team }
    end
  end

  # GET /teams/1/edit
  def edit
    @team = Team.find(params[:id])
  end

  # POST /teams
  # POST /teams.json
  def create
    raise params.to_yaml
    @team = Team.new(params[:team])
    respond_to do |format|
      if @team.save
        @team_member = TeamMember.new(:team_id => @team.id, :member_id => current_member.id,
                                  :accepted => true, :leader => true, :joined => Time.now)
        if @team_member.save
          format.html { redirect_to team_path(@team), notice: 'Team was successfully created.' }
          format.json { render json: @team, status: :created, location: @team }
        else
          @team.destroy
          format.html { render action: "new" }
          format.json { render json: @team.errors, status: :unprocessable_entity }
        end
      else
        format.html { render action: "new" }
        format.json { render json: @team.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /teams/1
  # PUT /teams/1.json
  def update
    @team = Team.find(params[:id])

    respond_to do |format|
      if @team.update_attributes(params[:team])
        format.html { redirect_to @team, notice: 'Team was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @team.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /teams/1
  # DELETE /teams/1.json
  def destroy
    @team = Team.find(params[:id])
    @team.destroy

    respond_to do |format|
      format.html { redirect_to teams_url }
      format.json { head :no_content }
    end
  end

end

team.rb 模型:

class Team < ActiveRecord::Base

  attr_accessible :name, :email, :language, :link, :logo, :team_leader

  validates_presence_of :name
  validates_presence_of :email
  validates_presence_of :language
  validates_uniqueness_of :name

  has_many :leaders, :class_name => "TeamMember", :conditions => { :leader => true }
  has_many :members, :through => :team_members
  has_many :team_members, :conditions => { :accepted => true, :active => true }

  has_attached_file :logo,
    :styles => { :medium => "320x180>", :thumb => "100x100>" },
    :url  => "/assets/teams/:id/:style/:basename.:extension",
    :path => ":rails_root/public/assets/teams/:id/:style/:basename.:extension"
end
4

0 回答 0