omarsafwany
omarsafwany

Reputation: 3801

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

Upvotes: 0

Views: 1833

Answers (2)

Ruslan
Ruslan

Reputation: 86

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

Upvotes: 0

Some Guy
Some Guy

Reputation: 1511

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.

Upvotes: 1

Related Questions