0

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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

so how can I fix this issue?!
Make @task an instance of your Task model - something like this: @task = Task.new(JSON.parse(response.body)['task'])
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.