2

I've jumped into learning Ruby by going straight to Padrino with Haml.

Most of the Padrino documentation assumes a high-level of knowledge of Ruby/Sinatra etc...

I am looking for samples that I can browse to see how things work. One specific scenario is doing a simple form. On my main (index) page I want a "sign up" edit box with button.

#app.rb
...
get :index, :map => "/" do
  @user = "test"
  haml: index
end

get :signup, :map => "/signup" do
  render :haml, "%p email:" + params[:email]
end
...

In my view:

#index.haml
...
#signup
  -form_for @user, '/signup', :id => 'signup' do |f|
    = f.text_field_block :email
    = f.submit_block "Sign up!", :class => 'button'
...

This does not work. The render in (/signup) never does anything.

Note, I know that I need to define my model etc...; but I'm building to to that in my learning.

Instead of just telling me what I'm doing wrong here, what I'd really like is a fairly complete Padrino sample app that uses forms (the blog sample only covers a small part of Padrino's surface area).

Where can I find tons of great Padrino samples? :-)

EDIT

The answer below was helpful in pointing me at more samples. But I'm still not finding any joy with what's wrong with my code above.

I've changed this slightly in my hacking and I'm still not getting the :email param passed correctly:

#index.haml
...
  #signup
     - form_for :User, url(:signup, :create), :method => 'post' do |f|
       = f.text_field_block :email
       = f.submit_block "Sign up!"
...    

#signup.rb
...
post :create do
  @user = User.new(params[:email])
  ...
end

EDIT Added Model:

#user.rb
class User
  include DataMapper::Resource
  property :id, Serial
  property :name, String
  property :email, String
  ...
end

When this runs, params[:email] is always nil. I've compared this to bunches of other samples and I can't see what the heck I'm doing wrong. Help!

tig
  • 3,424
  • 3
  • 32
  • 65

1 Answers1

2

You can browse some example sites here: https://github.com/padrino/padrino-framework/wiki/Projects-using-Padrino

Or you can browse sources of padrinorb.com here: https://github.com/padrino/padrino-web

The best way also is to generate admin: padrino g admin where you should see how forms works.

The tag form perform by default post actions unless you specify :method => :get|:put|:delete so in your controller you must change :get into :post

post :signup, :map => "/signup" do ...

Since you are using form_for :user params are in params[:user] so to get email you need to puts params[:user][:email]

DAddYE
  • 1,719
  • 11
  • 16
  • Link to `Projects-using-Padrino` was super helpful. I searched and searched and never saw that. Note `admin` uses partials and I am not (yet) so it was not helpful. I'm still not out of the woods, but your answer helped. Thanks. – tig Nov 15 '11 at 16:29
  • Also, the `render` I was asking about is in the `:signup` handler. The problem I have is `params[:email]` is always `nil`... – tig Nov 15 '11 at 16:29
  • try with a simple `text_field :email` – DAddYE Nov 16 '11 at 15:39
  • also remember that by default `form`(s) perform `post` action, so your controller must be: `post :signup, :map => "/signup"` – DAddYE Nov 16 '11 at 15:40
  • I tried `text_field :email` and same prob. Also, I am using `post`. See the 2nd example in my question above. Appreciate you trying to help!!!! – tig Nov 16 '11 at 17:51
  • try to do `params.to_yaml` at the end of your action removing `render` – DAddYE Nov 17 '11 at 09:56
  • FOUND! Since you are using `form_for :user` params are in `params[:user]` so to get email you need to puts `params[:user][:email]` – DAddYE Nov 17 '11 at 10:00
  • You rock. That raises 2 questions for me. 1) How would I do my view if I wanted just `params[:email]` to work? 2) Now why does `@user = User.new(params[:user][:email])` err with `undefined method 'each' for "test@test.com":String`? – tig Nov 17 '11 at 16:48
  • Note I added my User model above to further clarify. – tig Nov 17 '11 at 16:51
  • 1) to use params[:email] you should use `=text_field_tag :email` – DAddYE Nov 17 '11 at 17:18
  • 2) to use User.new you should pass an hash not a string so `User.new(params[:user])` – DAddYE Nov 17 '11 at 17:18