1

In my Rails 4 app I have two models - users and accommodations. Users has many accommodations and it has been set up in the relevant models (i.e. belongs_to, has_many, etc). I'm trying to validate data added to my accommodations model and display errors if there are any.

When I try to access accommodations/new action, I get the following error:

NoMethodError in Accommodations#new
undefined method `errors' for nil:NilClass

I have this working for when my users try and register and here is the code:

users/new.html.erb:

<% if @user.errors.any? %>
    <% @user.errors.full_messages.each do |msg| %>
         <p class="error"><%= msg %></p>
    <% end %>
<% end %>

but when I try to do the same with accommodation I get the error mentioned:

accommodations/new.html.erb:

<% if @accommodation.errors.any? %>
    <% @accommodation.errors.full_messages.each do |msg| %>
         <p class="error"><%= msg %></p>
    <% end %>
<% end %>

After reading around I realise that the first one works because of this in the UsersController:

def new
  @user = User.new
end

but after trying the following in my AccommodationsController #new action it still won't work

@accommodation = Accommodation.new

My Accommodation model is as follows:

class Accommodation < ActiveRecord::Base
  belongs_to :user

  validates :user_id, presence: true
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }

end
10
  • post your index.html.erb Commented Sep 2, 2013 at 19:23
  • Share your Accommodations controller index action, model and full error message. Commented Sep 2, 2013 at 19:24
  • I have nothing in my Accommodations index action as I'm not sure what to put there! I have edited the post with my Accommodation model and an extra line of the error message Commented Sep 2, 2013 at 19:40
  • @tommyd456 are you see my answer ? Commented Sep 2, 2013 at 20:04
  • 1
    In has_one associations that method gets replaced by build_accommodation Commented Sep 2, 2013 at 23:11

1 Answer 1

2

In the new method of your controller, you should use current_user.accommodations.build instead of accommodation.new, because you want an new accomodations of a user (accomodation belong_to user). when we have this type of relation remember that .new become .build instead

you have relation between users and accomodations like this :

class User < ActiveRecord::Base

   has_many :accommodations

end

and :

class Accommodation < ActiveRecord::Base

   belongs_to :user

end

you should have a new method like this :

class AccommodationsController < ApplicationControlle
   def new
      @accommodation = current_user.accommodations.build
   end
end

note : here current_user is user who is logged in .

then in your create method, i think if an error occurs you render new, else if creation succeed you redirect to accommodations#index page ?

in this case you should add index action to your accommodations controller :

class AccommodationsController < ApplicationControlle

    def index
       # add here what do you need to show, i think you want to show all accomodations, so just add the following line (you can also add pagination if you want later) :
       @accommodations = Accommodation.all
    end

    .
    .
end

finally you should have an index view page : views/accommodation/index.html.erb which loop for each element in @accommodations and show what do you need as information ...

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

11 Comments

Thanks for your detailed response - I have edited the original question slightly to clarify but when I add the ruby to display any errors as a results of the validation rules - this is when the NoMethodError occurs
Yes i have seen it thanks! Doesn't work I'm afraid - my error happens as soon as I add the ruby code to display validation errors (side note: accommodation is spelt with two m's for any who copy and paste).
and also - the error isn't when I try to submit the form - it's when I try and load accommodations/new
change [at]accommodation = Accommodation.new with [at]accommodation = current_user.accommodations.build , have you already a method which return the current user (who is connected)
yes this is what I did - I added to my new action in the Accommodations controller but doesn't work. But it must be something to do with this line because the users controller one works (@user = User.new) - prob has something to do with the relationship
|

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.