Contact form in ruby, sinatra, and haml

前端 未结 5 960
天命终不由人
天命终不由人 2021-01-30 09:15

I\'m new to all three, and I\'m trying to write a simple contact form for a website. The code I have come up with is below, but I know there are some fundamental problems with

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-30 09:55

    Uhmm, i tried in irb the following:

    foo = #{23}
    

    Of course it wont work! the '#' is for comments in Ruby UNLESS it occurs in a string! Its even commented out in the syntax highlighting. What you wanted was:

    name = "#{params[:name]}"
    

    as you did in your solution (which is not necessary, as it already is a string).

    Btw, the reason why the code does not throw an error is the following:

    a =
    b =
    42
    

    will set a and b to 42. You can even do some strange things (as you accidentally did) and set the variables to the return value of a function which takes these variables as parameters:

    def foo(a,b)
        puts "#{a.nil?} #{b.nil?}" #outputs 'true true'
        return 42
    end
    a =
    b =
    foo(a,b)
    

    will set a and b to 42.

提交回复
热议问题