librelist archives

« back to archive

POST'ing with Nesta

POST'ing with Nesta

From:
rowan
Date:
2011-06-08 @ 22:03
Hi everyone,

Just a quick query about creating contact form. I know it has been touched
on in the past from the mailing list archives, but I'm not sure how to 
actually implement it.


I can route the POST correctly (by including my post route in the 
Nesta::App namespace in app.rb), but where I'm getting stuck is how to 
route/redirect the user if A) they are missing values, and I need to send 
them back to the form to try again (with the params from their first POST 
- use a GET instead of a POST?) and B) render a thank you page once their 
request has been sent successfully. I'm not sure how the rendering/routing
in Nesta is different from Sinatra with the relationship between pages and
views, any hints?


Thanks,
rowan

Re: [nesta] POST'ing with Nesta

From:
Aaron Stroud
Date:
2011-06-09 @ 04:06
Hi rowan,

There's an excellent contact form 
(http://ididitmyway.heroku.com/past/2010/12/4/an_email_contact_form_in_sinatra/)
tutorial at the I Did It My Way: The Sinatra Songbook Project. Redirecting
in nesta is the same as sinatra since nesta is a sinatra app. Just add 
redirect '/thank-you' to the end of your post method.

Error correction is pretty straight forward once you have a working 
contact form. Here's how I approached it (app.rb @ github 
(https://github.com/astroud/Aaron/blob/master/themes/responsive/app.rb)). 
My post '/contact' do method first checks to see if the name, email, or 
message is missing:

def field_missing?
if params[:name] == "" || params[:email] == "" or params[:message] == ""
redirect '/contact' + "?name=#{params[:name]}" + 
"&email=#{params[:email]}" + "&message=#{params[:message]}" + 
"&spamcheck=#{params[:spamcheck]}"
end
end

If any of the fields are missing, the user is redirected back to the 
contact page with any of the fields they have already filled in. You can 
see the form in action here (http://aaronstroud.com/contact). Try hitting 
submit without filling in the fields.

If you append the form's fields to the url (when redirecting), it makes it
really easy to highlight empty fields or fields with bad data. Here's how 
I added error highlighting to my form:

# Grabbing field data that may be present if a user has already attempted 
to fill out the form
get "/contact" do
@name = params[:name]
@email  = params[:email]
@message  = params[:message]
@spamcheck = params[:spamcheck]



(contact_form @ github 
(https://github.com/astroud/Aaron/blob/master/themes/responsive/views/contact_form.haml)):
# all of the fields are given a value, it's just usually nil
# If the user has already attempted to submit the form, param[:name] 
shouldn't be nil. If it is, the field is slapped with a bad_data css class
%label{:for => "name"} Name
%input{:type => "text", :name => "name", :value => "#{@name}", :class => 
"#{"bad_data" if params[:name] == ""}"}


Warmly,
Aaron


On Wednesday, June 8, 2011 at 3:03 PM, rowan wrote:

> Hi everyone,
> 
> Just a quick query about creating contact form. I know it has been 
touched on in the past from the mailing list archives, but I'm not sure 
how to actually implement it.
> 
> I can route the POST correctly (by including my post route in the 
Nesta::App namespace in app.rb), but where I'm getting stuck is how to 
route/redirect the user if A) they are missing values, and I need to send 
them back to the form to try again (with the params from their first POST 
- use a GET instead of a POST?) and B) render a thank you page once their 
request has been sent successfully. I'm not sure how the rendering/routing
in Nesta is different from Sinatra with the relationship between pages and
views, any hints?
> 
> < /div>
> Thanks,
> rowan
> 
> 
> 
> 
>