Joe Crocetta
Joe Crocetta

Reputation: 91

Dont understand why I'm encountering this TypeError (Can't convert nil into String)

Very simple process I'm trying to implement. On a home page or landing page I want to capture emails for an email list. If the email passes validation, it gets saved, Great! Everything works fine when there are no errors.

When there is an error like only inputting an 'a' character as shown in the log below. Or even just an empty string I continually get the same TypeError (can't convert nil to String)

Here is the log:

Started GET "/" for 127.0.0.1 at 2013-06-13 13:48:56 -0400
Connecting to database specified by database.yml
Processing by HighVoltage::PagesController#show as HTML
Parameters: {"id"=>"home"}
Rendered shared/_error_messages.html.erb (0.4ms)
Rendered layouts/_visitor_form.html.erb (7.9ms)
Rendered pages/home.html.erb within layouts/application (13.1ms)
Completed 200 OK in 82ms (Views: 39.6ms | ActiveRecord: 1.8ms)
[2013-06-13 13:48:57] WARN  Could not determine content-length of response body. Set       content-length of the response or set Response#chunked = true


Started POST "/visitors" for 127.0.0.1 at 2013-06-13 13:48:59 -0400
Processing by VisitorsController#create as HTML
Parameters: {"utf8"=>"✓",authenticity_token"=>"esTPNzNtkmNPTe7Jh+E2aDHNrgocU5Z8g49Nj0QiOhQ=",    "visitor"=>{"email"=>""}, "commit"=>"Add to newsletter list"}
(0.1ms)  begin transaction
Visitor Exists (0.2ms)  SELECT 1 AS one FROM "visitors" WHERE LOWER("visitors"."email") =     LOWER('') LIMIT 1
(0.1ms)  rollback transaction
Completed 500 Internal Server Error in 39ms

TypeError (can't convert nil into String):
app/controllers/visitors_controller.rb:12:in `create'

Here is the visitors_controller.rb

 def create
    @visitor = Visitor.new(params[:visitor])
     if @visitor.save
        flash[:success] = "Your email has been added!"
       redirect_to '/'
     else
       render '/'
     end
 end

I've also tried

@visitor = Visitor.new(:visitor => params[:visitor][:email])

and a few other sequences with no success.

How can I get the unsuccessful saves to gracefully show the validation errors and allow the user to resubmit.

Here is the layout:

    <%= form_for Visitor.new  do |f| %>
            <%= render 'shared/error_messages' %>
            <%= f.label :email %>
            <%= f.text_field :email %>

    <%= f.submit "Add to newsletter list", class: "btn btn-large btn-primary" %>
    <% end %>

Maybe the high_voltage gem is screwing me up. I don't have enough experience to know.

UPDATE:

rake routes
visitors POST /visitors(.:format) visitors#create
home      /home(.:format)     pages#home
root      /                   high_voltage/pages#show {:id=>"home"}
page GET  /pages/*id          high_voltage/pages#show

Upvotes: 1

Views: 231

Answers (1)

Ilia Khokhriakov
Ilia Khokhriakov

Reputation: 3687

The problem is, render '/' does not render index page. You have to specify which action page you want to be rendered. According to your routes, this line should look like this:

render 'pages/home'

Upvotes: 1

Related Questions