账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Rails:undefined method `model_params' for nil:NilClass
    53
    0

    这里说我没有定义这个method,问题出在app/views/model/show.html.haml这个文件里。

    我尝试了去controller 里面def show 里面有什么问题 可是 没发现有什么问题。

    相关代码

    // 请把代码文本粘贴到下方(请勿用图片代替代码)
    .show-param

    .tabs-content
        .parameter
            .table-wrapper
                %table
                    %caption
                        %strong Parameters
                    %thread
                        %td  Name
                        %td  DataType
                    %tbody
                        - @model.model_params.each do |p| 
                            %tr
                                %td.font-bold{:width => "30%"}
                                    = p.name
    
                                %td{:width=>"10%"}
                                    = p.data_type
            .row 
                = link_to 'Edit API Endpoint information', edit_project_model_path(), class: 'button tiny right small-margin-right-10'
                

    相关代码

    // 请把代码文本粘贴到下方(请勿用图片代替代码)

    def show 
        @hostname = request.host 
        @hostport = request.port.to_s
        @model = Model.find(get_id)
        @params =@model.model_params.all.order(name: :asc)
        @p_id =get_project_id
    end
                

    相关代码

    // 请把代码文本粘贴到下方(请勿用图片代替代码)

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

    你期待的结果是什么?实际看到的错误信息又是什么?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 嘉图李的猫 普通会员 1楼

      The model_params method is a method that is defined in the ActionView::Helpers::ModelHelper class in the Rails framework. It is used to extract the data parameters from the model instance passed to the find or new method. If you are trying to access model_params on a nil object, you are getting a undefined method error because nil is not an instance of the ModelHelper::Model class. To fix this error, you need to make sure that the model instance being passed to the find or new method is not nil. If you are trying to access the model instance through a link or button that calls the find or new method, make sure that the model instance exists and is not nil before calling the method. Here is an example of how you might use model_params on a nil object in Rails: <%= link_to 'View Model', model_path(model: nil), method: :get %>

      In this example, the model_path(model: nil) call would look for a model instance that has a model attribute set to nil, and then retrieve the data parameters from that model instance using model_params. If the model attribute is not set to nil, the model_params method will be called and the data parameters will be available for use in the link or button. Alternatively, you can also use the to_param method to get the id or class of the model instance before accessing its model_params method: <%= link_to 'View Model', model_path(model: nil), method: :get %>

      In this example, the model_path(model: nil) call would look for a model instance that has a model attribute set to nil, and then retrieve the data parameters from that model instance using model_params. If the model attribute is not set to nil, the model_params method will be called and the id or class of the model instance will be available for use in the link or button. In either case, make sure that the model instance being passed to the find or new method is not nil and that the data parameters are available before calling the model_params method. If the data parameters are not available, the undefined method error will be raised.

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部