Undefined method for edit page - ruby-on-rails

I am new to Rails. My new.html.erb file works perfect as it shows at http://localhost:3000/signup. However I can't seem to get /edit to work. I receive this error:
undefined method `model_name' for NilClass:Class
Extracted source (around line #3):
1: <h1>Account Information</h1>
2:
3: <%= form_for #user do |f| %>
4: <% if #user.errors.any? %>
5: <div class="error_messages">
6: <h2>Form is invalid</h2>
Here's my edit.html file which is a replica of the new.html that works. I tried removing the error messages code and it still just displayed another error with the page.
<h1>Account Information</h1>
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br/>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br/>
<%= f.password_field :password_confirmation %>
</div>
<div class="field">
<%= f.label :username %><br/>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :zip_code %><br/>
<%= f.text_field :zip_code %>
</div>
<div class="field">
<%= f.label :birthday %><br/>
<%= f.text_field :birthday %>
</div>
<div class="actions"><%= f.submit %></div>
<% end %>
Here's my users_controller that I'm not sure if you need to look at or not. Maybe I have the def edit part wrong.
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
UserMailer.registration_confirmation(#user).deliver
session[:user_id] = #user.id
redirect_to root_url, notice: "Thank you for signing up!"
else
render "new"
end
def edit
#user = User.find(params[:id])
end
def update
#user = User.find(params[:user])
if #user.update_attributes(params[:user])
flash[:success] = "Account updated"
sign_in #user
redirect_to #user
else
render 'edit'
end
end
end
end

Your code indentation is a tell-tale sign here; you're defining the edit and update methods inside def create; the end immediately prior closes the if #user.save, not the def create.

Related

error.full_messages are hidden and I can't figure out why

I'm working through a rails tutorial and seem to be stuck on getting some error messages to display on my signup window when an e-mail address, or password is typed in incorrectly. I've included my user controller, new user erb, and error message erb below. Any help would be great. I feel like I've been staring at this forever and getting nowhere. Thanks in advance for helping.
Error_Message erb
<% if #user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= #user.errors.count %> errors.
</div>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li> <%= msg %> </li>
<% end %>
</ul>
</div>
<% end %>
User Controller
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
# Handle a succesful save
else
render "new"
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
New User erb
<%provide(:title, 'Sign up')%>
<h1>Sign up</h1>
<div class="row">
<div class ="col-md-6 col-md-offset-3">
<%= form_for(#user) do |f| %>
<% render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
Just change this line:
<% render 'shared/error_messages' %>
to
<%= render 'shared/error_messages' %>

I receive an error that "No Route Matches" {...id=>nil} *Wicked*

I have seen a lot of similar routing issues and id=>nil posts, but none of the solutions have resolved my error.
First here is the entire error:
ActionController::UrlGenerationError in ProfileSteps#personal
Showing ...profile_steps/personal.html.erb where line #1 raised:
No route matches {:action=>"show", :controller=>"profile_steps", :id=>nil
I am using Wicked to create a multi step form and it seems like the I am not fetching the :id properly.
Here is the profiles_controller.rb following 'signup' and creating the 1st step of profile
def create
#profile = Profile.new(profile_params[:profile])
if #profile.save
session[:profile_id] = #profile.id
redirect_to profile_steps_path
else
render :new
end
end
Here is the profile_steps_controller.rb which is the next step of form it is redirected to
class ProfileStepsController < ApplicationController
include Wicked::Wizard
steps :personal
def show
#profile = Profile.new(params[:profile])
session[:profile_id] = #profile.id
render_wizard
end
def update
#profile = Profile.new(profile_params)
#profile.attributes = (profile_params)
render_wizard #profile
end
private
def profile_params
params.require(:profile).permit(:description, :name, :website)
end
def redirect_to_finish_wizard
redirect_to root_url, notice: "Thank you for signing up."
end
end
views/profile_steps/personal.html.erb
<%= form_for #profile, url: wizard_path do |f| %>
<br>
<div class="field">
<%= f.label :name, "Company Name" %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :website %><br>
<%= f.text_field :website %>
</div>
<br>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
routes.rb
project::Application.routes.draw do
resources :profiles
resources :profile_steps
devise_for :users, :controllers => { :registrations => "registrations" }
root "pages#home"
get "profile" => "pages#profile"
match "profile_steps/personal", to: "profile_steps#personal", via: "post"
Thanks in advance and my apologies if this has been addressed in previous posts.
Updated:
Here is the 1st page of the multistep form:
new.html.erb
<%= form_for(#profile) do |f| %>
<% if #profile.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#profile.errors.count, "error") %> prohibited this profile from being saved:</h2>
<ul>
<% #profile.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<h1>New profile</h1>
<div>
<%= f.radio_button ..., true %> <%= f.label ... %>
</div>
<div>
<%= f.radio_button ..., false %> <%= f.label ... %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I had this problem in the view on the first page of the wizard, I solved it using a helper method:
def safe_previous_wizard_path(id)
id ? previous_wizard_path : root_path
end
or even:
<% if applicant.persisted? %>
Back
<% end %>

How to re-populate a form from another controller if it contains errors

I am studying the get start with rails guide, and add error validation on comments but if I have errors, the form is not re-populated.
I post dates from /car/show.html.erb
I check errors with flash (works correctly) but my form become empty if error.
Below code of my comments_controller.rb controller :
def create
#car = Car.find(params[:car_id])
if #comment = #car.comments.create(params[:comment].permit(:email,:sujet,:commentaire))
flash[:error] = #comment.errors
flash.keep[:error]
render 'car/show'
else
redirect_to car_path(#car)
end
end
Below cars/show.html.erb
<h1>Fiche détaillée</h1>
<%= #car.marque %><br>
<%= #car.modele %><br>
<%= #car.nbkm %><br>
<%= #car.couleur %><br>
<%= #car.disponibilite %><br>
<hr>
<h1><%= pluralize(#car.comments.count,'Commentaire') %></h1>
<% #car.comments.each do |k| %>
<%= k.email %><br>
<%= k.sujet %><br>
<%= k.commentaire %><br>
<hr>
<% end %>
<hr>
<h1>Ajouter votre commentaire</h1>
<div style='width:300px;'>
<% flash.each do |key, msg| %>
<p class="bg-danger" style='padding:10px;'><%= pluralize(msg.count,'error') %></p>
<ul><% msg.full_messages.each do |m|%>
<li><%= m %></li>
<% end %>
<% end %>
</ul>
<%= form_for ([#car,#car.comments.build]) do |co| %>
<%= co.label :'Email' %><br>
<%= co.text_field :email , class: 'form-control' %><br>
<br>
<%= co.label :'Sujet' %><br>
<%= co.text_field :sujet , class: 'form-control'%><br>
<br>
<%= co.label :'Commentaire' %><br>
<%= co.text_area :commentaire , class: 'form-control' %><br>
<br>
<%= co.submit :'Envoyer votre commentaire', class: 'btn btn-info'%>
<% end %>
I don't know how to deal with render when controller is not the same.
Try this,
Edit:
def create
#comment = Comment.new(params[:comment].permit(:email,:sujet,:commentaire))
if !#comments.save
flash[:error] = #comment.errors
flash.keep[:error]
render 'car/show'
else
redirect_to car_path(#car)
end
end
car/show method:
def show
#car = Car.find(params[:id])
#comment = #car.comments.build
end
Change
<%= form_for ([#car,#car.comments.build]) do |co| %>
To
<%= form_for #comment do |co| %>

MHartl Tutorial Error: undefined method `model_name' for NilClass:Class

So I'm working my way through the MHartl Rails Tutorial and it has been awesome so far, albeit I have just ran into my first error and I am having trouble finding a working solution.
The error:
NoMethodError in Users#new
Showing /Users/myname/Sites/rails_projects_updated/first_app/app/views/users/new.html.erb where line #6 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #6):
3:
4: <div class="row">
5: <div class="span6 offset3">
6: <%= form_for(#user) do |f| %>
7:
8: <%= f.label :name %>
9: <%= f.text_field :name %>
New.html.erb:
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
And User Controller
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def create
#user = User.new(params[:user])
if #user.save
# Handle a successful save.
else
render 'new'
end
end
end
Any help would be appreciated! Thanks.
Surely you are missing the following method from your controller:
def new
#user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #user }
end
end
What is happening is that when the form is responding to the create action and is hitting render 'new' which you do not have in this question. So that when the new action is rendered it will do #user = User.new which will enable you to create a new user.

i have a form to create new users, but dont work

i have a problem with a form to create a user... this is my code of "new"
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<% for message in #user.errors.full_messages %>
<div class="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
<%= message %>
</div>
<% end %>
<% end %>
<div class="field">
<%= f.label :username %>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions"><%= f.submit "Registrarse" %></div>
<% end %>
and "create" in controller
def create
#user = User.new(params[user])
if #user.save
redirect_back_or_to root_url, :notice => "Usuario Registrado Satisfactoriamente!"
else
render :new, :notice => "No se ha podido registrar"
end
end
when i create the user, this go to login form, and a receive a message empty, because i have an "if" if i have message, this show me a div with some css, so i can see that an error ocurred, but this come empty, what i have wrong? i dont see anything... and of course, the user is not created.!
thanks for the help, i am a newbie in ruby. and sorry for my bad english xd
The problem is that you most likely are referencing password_confirmation which is usually an input tag. You most likely do not have this field in your database as it would just be horrible practice. Are you salting your passwords?
Check out sorcery on github and the Railscasts. It does a nice job on covering authentication.
Also, you need to use :user in the params => User.new(params[:user])

Resources