JohnSmith
JohnSmith

Reputation: 1268

NameError - undefined local variable or method `user_details' for #<Sinatra::Application:0x0000000381bde0>:

I have a basic Sinatra app when posting form details. I'm getting this error, especially when running on Heroku

NameError - undefined local variable or method `user_details' for #:

Mye post method:

  post "/" do
  user_details = UserDetails.new(params["name"], params["email"])
  if user_details.valid?
    begin
      worksheet.insert_rows(worksheet.num_rows+1, [user_details.to_row])
      worksheet.save
      erb :thanks
    rescue
      erb :index
    end
  else
    erb :index
  end
end

View:

<form action="/" method="POST">
          <div class="form-group<%= 'has-error' if user_details.errors.include?(:name) %>">
            <label for="name" class="control-label">Name</label>
            <input type="text" class="form-control" name="name" id="name" value="<%= user_details.name %>">
            <% user_details.errors.full_messages_for(:name).each do |message| %>
              <span class="help-block"><%= message %></span>
            <% end %>
          </div>
</form.

Logs say it can't find the user_details variable in the first div block/class.

Any ideas?

thanks.erb

<div class="jumbotron">
  <div class="container">
    <div class="row">
      <div class="col-md-12">
        <h1>Thanks!</h1>
        <p>We'll let you know when our platform is ready. Stay tuned!</p>
      </div>
      <div class="col-md-4">
        <a href="/"><button type="submit" class="btn btn-primary">Back</button></a>
      </div>
    </div>
  </div>
</div>

Upvotes: 0

Views: 805

Answers (1)

tbuehlmann
tbuehlmann

Reputation: 9110

You defined user_details a a local variable, which is not available in the view. Two options:

  1. Define an instance variable @user_details and use that in the view.
  2. Define a local variable user_details (as it is now) and render the view, passing the variable: erb :thanks, locals: {user_details: user_details} (or erb :index, locals: {user_details: user_details}, not sure where the error comes from)

Upvotes: 1

Related Questions