简体   繁体   English

Rails-未定义的方法“ model_name”

[英]Rails - undefined method `model_name' for

In the edit function, I am fetching the object that will be edited. 在编辑功能中,我正在获取将要编辑的对象。 Unfortunately I have been getting this weird error that I can' understand its cause so far. 不幸的是,到目前为止,我一直收到这个奇怪的错误,我无法理解其原因。

NoMethodError in Tasks#edit

undefined method model_name' for #Hash:0x007fe92d2afeb8 at line#1 in _form.html.erb undefined method MODEL_NAME”的#Hash:0x007fe92d2afeb8在线路#1 _form.html.erb

tasks_controller: task_controller:

def edit
    uri = URI.parse("http://localhost/tasks/public/api/tasks/"+params[:id])
    response = Net::HTTP.get_response(uri)
    @task = JSON.parse(response.body)['task']
end

_form.html.erb _form.html.erb

<%= simple_form_for(@task) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :name %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

simple_form_for is expecting you to pass it a model. simple_form_for希望您将其传递给模型。 Based on that assumption it's trying to call a method that exists on models, but not in your @task object, which is the result of a JSON parse. 基于该假设,它试图调用模型上存在的方法,而不是@task对象中存在的方法,这是JSON解析的结果。

You can only use ActiveRecord classes (or duck typed) with simple_form 您只能将ActiveRecord类(或鸭子类型)与simple_form一起simple_form

However simple_form can also take a hash instead of a model, something like: 但是, simple_form也可以使用哈希代替模型,例如:

simple_for_for(:task) But then you gotta re-structure you form a bit simple_for_for(:task)但是然后您需要重新构造,形成一些

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM