/ projects / 2 / model / []:Array的新未定义方法“名称”处的Rails:NoMethodError

时间:2018-11-06 05:30:09

标签: ruby-on-rails

在创建新的模型参数后出现此错误: 这是app / views / model / parameterarea.html.haml

parameterarea.html.haml

这是我的模型控制器:

class ModelController < ApplicationController
    skip_authorization_check

    def index
        @hostname = request.host 
        @hostport = request.port.to_s
        @model = Model.all
        @user_relations =Relation.where(user_id: current_user)
        @p_id = get_project_id
    end 

    def new
        @model=Model.new
        @p_id=get_project_id
    end

    def create
        @model = Model.new(m_params)
        if @model.name.strip.length != 0
            if @model.save
                redirect_to  project_model_index_path, notice: 'Model was successfully created.'
            else 
                render 'new'
            end
        else
            render 'new'
        end
    end


    def destroy

    end


end

    def show 
        @hostname = request.host 
        @hostport = request.port.to_s

    end


    def update

    end


    def get_project_id
        params[:project_id]
    end

    def m_params
        params.require(:model).permit(:name,:description,:response, model_params_attributes: [:id, :name, :data_type, :_destroy])
    end


    def get_id
    params[:id]
  end

这是我的模特

class ModelParam < ApplicationRecord
    belongs_to :model, optional: true


    enum data_type: {
        String:            0,
        Integer:           1,
        Bool:              2,
        Hash:              3,
        Date:              4,
        Datetime:          5,
        Float:             6,
        Decimal:           7,
        Text:              8,
        Time:              9,
        # "Array[]":         10,
        File:              11,
        Image:             12,
        "Array[String]":   13,
        "Array[Integer]":  14,
        "Array[Bool]":     15,
        "Array[Hash]":     16,
        "Array[Date]":     17,
        "Array[Datetime]": 18,
        "Array[Float]":    19,
        "Array[Decimal]":  20,
        "Array[Text]":     21,
        "Array[Time]":     22,
        "Array[File]":     23,
        "Array[Image]":    24
    }

end

1 个答案:

答案 0 :(得分:0)

唯一将其添加到 model.rb

中的行的修复程序
"accepts_nested_attributes_for :model_params"