문제

나는 세 가지 모두를 처음 접했고 웹 사이트에 대한 간단한 연락 양식을 작성하려고합니다. 내가 생각해 낸 코드는 아래에 있지만, 그와 관련하여 몇 가지 근본적인 문제가 있다는 것을 알고 있습니다 (Sinatra와의 경험이 없기 때문입니다). 이 작업을 수행하는 데 도움이 될 것입니다. 이런 종류의 문서를 찾거나 찾을 수없는 것 같습니다.

연락처 페이지에서 HAML 코드 :

%form{:name => "email", :id => "email", :action => "/contact", :method => "post", :enctype => "text/plain"}
  %fieldset
    %ol
      %li
        %label{:for => "message[name]"} Name:
        %input{:type => "text", :name => "message[name]", :class => "text"}
      %li
        %label{:for => "message[mail]"} Mail:
        %input{:type => "text", :name => "message[mail]", :class => "text"}
      %li
        %label{:for => "message[body]"} Message:
        %textarea{:name => "message[body]"}
    %input{:type => "submit", :value => "Send", :class => "button"}

그리고 여기 Sinatra의 app.rb에 내 코드가 있습니다.

require 'rubygems'
require 'sinatra'
require 'haml'
require 'pony'

    get '/' do
        haml :index
    end 

    get '/contact' do
        haml :contact
    end

    post '/contact' do
        name = #{params[:name]}
        mail = #{params[:mail]}
        body = #{params[:body]}     
        Pony.mail(:to => '*emailaddress*', :from => mail, :subject => 'art inquiry from' + name, :body => body) 
    end
도움이 되었습니까?

해결책

나는 당신이 궁금해하는 당신을 위해 그것을 알아 냈습니다.

Haml :

%form{ :action => "", :method => "post"}
  %fieldset
    %ol
      %li
        %label{:for => "name"} Name:
        %input{:type => "text", :name => "name", :class => "text"}
      %li
        %label{:for => "mail"} email:
        %input{:type => "text", :name => "mail", :class => "text"}
      %li
        %label{:for => "body"} Message:
        %textarea{:name => "body"}
    %input{:type => "submit", :value => "Send", :class => "button"}

그리고 app.rb :

post '/contact' do
        name = params[:name]
        mail = params[:mail]
        body = params[:body]

        Pony.mail(:to => '*emailaddress*', :from => "#{mail}", :subject => "art inquiry from #{name}", :body => "#{body}")

        haml :contact
    end

다른 팁

누구나 이것을 사용할 수있는 경우, 다음은 Gmail 계정을 사용하여 메일을 보내는 데 필요한 것이 있습니다.

post '/contact' do 
require 'pony'
Pony.mail(
   :name => params[:name],
  :mail => params[:mail],
  :body => params[:body],
  :to => 'a_lumbee@gmail.com',
  :subject => params[:name] + " has contacted you",
  :body => params[:message],
  :port => '587',
  :via => :smtp,
  :via_options => { 
    :address              => 'smtp.gmail.com', 
    :port                 => '587', 
    :enable_starttls_auto => true, 
    :user_name            => 'lumbee', 
    :password             => 'p@55w0rd', 
    :authentication       => :plain, 
    :domain               => 'localhost.localdomain'
  })
redirect '/success' 
end

끝에 리디렉션을 주목하므로 성공이 필요합니다.

음, IRB에서 다음을 시도했습니다.

foo = #{23}

물론 작동하지 않습니다! '#'는 문자열에서 발생하지 않는 한 루비에서 주석을위한 것입니다! 구문 하이라이트에서도 언급했습니다. 당신이 원했던 것은 다음과 같습니다.

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

솔루션에서와 마찬가지로 (이미 줄인데, 필요하지 않습니다).

BTW, 코드가 오류를 던지지 않는 이유는 다음과 같습니다.

a =
b =
42

A와 B를 42로 설정합니다. 실수로 한 것처럼 이상한 일을 할 수도 있고 (실수로했던 것처럼) 변수를 이러한 변수를 매개 변수로 취하는 함수의 반환 값으로 설정할 수도 있습니다.

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

A와 B를 42로 설정합니다.

#{}는 "" "내부에 사용되는 보간입니다. 가변 할당을 위해 외부에서 사용하는 것만으로는 작동하지 않습니다.

다음과 같이 사용될 가능성이 더 높습니다.

number_of_people = 15 

Puts "There are #{number_of_people} scheduled tonight" 

GitHub에서 사용할 수있는 두 부분으로 이것의 예를 만들었습니다. 가입 양식 앱은 다음과 같습니다. 가입-형식-헤로쿠 그리고 이것과 상호 작용하는 정적 웹 사이트의 예는 다음과 같습니다. 정적-웨스 사이트-S3- 측정. 양식 앱은 Sinatra를 사용하여 구축되었으며 Heroku에 직접 배포 할 준비가되었습니다. 정적 사이트는 S3에 직접 배포하고 Amazon Cloudfront를 사용할 준비가되었습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top