Rails - undefined method `model_name' for

偶尔善良 提交于 2020-01-06 13:29:32

问题


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 methodmodel_name' for #Hash:0x007fe92d2afeb8 at line#1 in _form.html.erb

tasks_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

<%= 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 %>

回答1:


simple_form_for is expecting you to pass it a model. 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.




回答2:


You can only use ActiveRecord classes (or duck typed) with simple_form

However simple_form can also take a hash instead of a model, something like:

simple_for_for(:task) But then you gotta re-structure you form a bit



来源:https://stackoverflow.com/questions/37535332/rails-undefined-method-model-name-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!