1

I know this question been ask before and they said 'models are independent of the controller.' So, following my same code I did for other models I just renamed my working code to fit to my new model. But I'm getting an error undefined method 'userimages_path' Here the code I'm using.

The model is userimage and the controller is uploads.

Controller/uploads_controller.rb

  def new
    @userimage = Userimage.new
  end                       


Model/userimage.rb is an empty file

Views/uploads/new.html.erb (This line is throwing the error.)

  <%= form_for @userimage do |f|%>

In my routes.rb

resources :uploads

I have rake db:migrate several times to make sure I did migrate the database thinking this might be why it can't find the Userimage.

What I have I done wrong/missing here?

4
  • 1
    can you show your schema file / migration that covers user image? Commented Apr 12, 2017 at 16:34
  • Can you confirm your model is Userimage compared to UserImage? Commented Apr 12, 2017 at 16:37
  • create_table "userimages", force: :cascade do |t| t.text "description" t.string "title" t.datetime "created_at", null: false t.datetime "updated_at", null: false end Commented Apr 12, 2017 at 16:49
  • I looked at the model and every other file and it i Userimages not UserImages. Commented Apr 12, 2017 at 16:49

2 Answers 2

1

It is a Rails magic, when you don't specify second option(url) for form_for, Rails try to set it, in your case <%= form_for @userimage do |f|%> converts by Rails to <%= form_for @userimage, url: userimages_path do |f|%>, in your routes, there is no such _path helper. To resolve this issue run bundle rake routes and set the right url option.

Check the documentation

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

1 Comment

To me, Rails is alien technology. I'm impress to how much it automatically does things for me and it just works with less code. .... Oh, oh! My light bulb just came on! So rails was routing according to the model/crud not controller/crud?..interesting. It works!! I was mystify why it was looking for userimages_path. Thought it was smart enough to route to new_uploads in the form_for so I didn't think to look there! Marked as answer! Thank you!
0

try below code:

view

 <%= form_for @userimage, url: "/uploads" do |f|%>

uploads controller

def create
  ...
end

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.