Rails - 未知属性 - 无法在创建/更新时向表单添加新字段

时间:2016-01-01 00:00:08

标签: ruby ruby-on-rails-4

您好我是Rails的新手。

这是我的controller / projects_controller.rb代码

def create
@project = current_user.projects.new(project_params)
#@project.website = params[:website]
respond_to do |format|
  if @project.save
    Member.create!(
                   user_id: current_user.id,
                   project_id: @project.id,
                   project_manager: true,
                   status: "ready")

    format.html { redirect_to @project, notice: 'Project was successfully created.' }
    format.json { render :show, status: :created, location: @project }
  else
    format.html { render :new }
    format.json { render json: @project.errors, status: :unprocessable_entity }
  end
end
end

def update
#@project.website = params[:website]
respond_to do |format|
  # if (@project.active_was == true) &&
  #   disabled = true
  # end

  if @project.update(project_params)
    # if disabled && (@project.active == false)
    #   flash[:modal] = true
    # end
    @project.website = params[:website]
    format.html { redirect_to @project, notice: 'Project was successfully updated.' }
    format.json { render :show, status: :ok, location: @project }
  else
    format.html { render :edit }
    format.json { render json: @project.errors, status: :unprocessable_entity }
  end
end
end

  def project_params
  params.require(:project).permit(
                                  :university_id,
                                  :project_name,
                                  :location,
                                  :tagline,
                                  :photos,
                                  :industry,
                                  :category,
                                  :description,
                                  :category_id,
                                  :expertise_string,
                                  :website,
                                  :active,
                                  :slug,
                                  :project_groups_attributes => [:id, :active, :university_id, :user_group_id]
  )
    end

以下是我的model / project.rb的代码

class Project < ActiveRecord::Base
  # has_and_belongs_to_many :users
    has_many :members, :dependent => :destroy
    has_many :users, :through => :members
    has_one :survey
    has_many :project_groups, foreign_key: "project_id", dependent: :destroy
    has_many :groups, :through => :project_groups, :source => :university
    accepts_nested_attributes_for :project_groups, :allow_destroy => true
    has_many :project_expertises, foreign_key: "project_id",
                            dependent: :destroy
    has_many :expertises, :through => :project_expertises, :source => :expertise
    belongs_to :category
    belongs_to :website

这是我的db / migrate / [timestamp] _create_projects.rb代码

class CreateProjects < ActiveRecord::Migration
  def change
    create_table :projects do |t|
      t.integer :university_id
      t.string :project_name
      t.string :tagline
      t.string :photos
      t.string :industry
      t.integer :category
      t.text :description
      t.text :website

      t.timestamps
    end
  end
end

为什么我不能在此添加“网站”字段?我还有什么需要做的吗?

2 个答案:

答案 0 :(得分:1)

根据您的评论,没有任何Website模型可用,因此没有任何关联的含义:

只需从project型号

中删除此行即可
belongs_to :website

答案 1 :(得分:0)

Project模型中,您指定了关系belongs_to :website。在迁移模式中而不是:

t.text :website

必须是这样的:

t.references :website, index: true, foreign_key: true

生成模型而不是 rails g ... website:text使用rails g ... website:references

ActiveRecord找不到将Project与网站关联的外键。