Rails and Validation -


i'm still (for have helped before) having issues validating data.

the scenario

i have 2 models - user , accommodation each user having 1 accommodation (has_one). able access logged in user using current_user.

the problem

when validate users upon registration works fine validation error messages displayed accordingly each validation rule. however, trying validate accommodations when entered , rails error page:

unknown action

the action 'show' not found accommodationscontroller

but interestingly url has changed /accommodations//edit appears missing id accommodations id (i want divert edit if ok).

i don't think it's validation rules more how handling redirection (which confusing me honest!). data saves correctly , redirects correctly if passes validation rules not sure how handle "non-save" gracefully.

the code

accommodation model

class accommodation < activerecord::base   belongs_to :user    #validation rules   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 

/accommodations/new.html.erb

... <%= form_for :accommodation, url: accommodations_path |f| %>  <% if @accommodation.errors.any? %>   <% @accommodation.errors.full_messages.each |msg| %>     <p class="error"><%= msg %></p>   <% end %> <% end %>  ... 

accommodationscontroller (thanks @depa this)

... def index   if current_user.accommodation.present?     redirect_to edit_accommodation_path(current_user.accommodation)   else     redirect_to new_accommodation_path(current_user.accommodation)   end end  def new   @accommodation = accommodation.new end  def create   current_user.create_accommodation(accommodation_params)   flash[:success] = "accommodation details added"   redirect_to edit_accommodation_path(current_user.accommodation) end  def edit end  def update    if @accommodation.update(accommodation_params)     flash[:success] = "accommodation details updated successfully"     redirect_to edit_accommodation_path(@accommodation)   else     flash[:error] = "accommodation details not updated"     render 'edit'   end end  private   def accommodation_params     params.require(:accommodation).permit(:name, :email)   end ... 

to handle failed validations gracefully:

def create   @accommodation = current_user.build_accommodation(accommodation_params)   if @accommodation.save     flash[:success] = "accommodation details added"     redirect_to edit_accommodation_path(current_user.accommodation)   else     flash.now.notice = "error creating accommodation"     render "new"   end end 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -