【问题标题】:Haml Form Not Submitting in Sinatra AppHaml 表单未在 Sinatra App 中提交
【发布时间】:2011-10-18 16:49:58
【问题描述】:

现在我在一个简单的 sinatra 应用中从 erb 转换为 haml,提交表单时遇到问题。

新的.haml

%form{ :action => "/new", :method => "post"}
  %fieldset
    %ol
      %li
        %label{:for => "username"} Name:
        %input{:type => "text", :username => "name", :class => "text"}
    %input{:type => "submit", :value => "Send", :class => "button"}

在我的 app.rb 中

get '/new' do
    haml :new
end

post '/new' do
  radcheck = Radcheck.new(:username => params[:username])
  if radcheck.save
    redirect '/'
  else
      "Hello World" 
  end
end

每次我得到 Hello World 语句时都会出现。我的日志没有显示任何有趣的内容。

有什么想法吗?与 erb 一起工作得很好??

【问题讨论】:

  • 你粘贴正确了吗? :username => "name" 不应该反过来吗?
  • @Slartibartfast - 我也试过 %input{:type => "textbox", :username => "username", :id => "username"} 没有成功:(
  • 你试过:name => "username"吗?
  • @Slartibartfast 也不起作用,仍然无法保存。我需要调整我的行为吗?
  • 请注意,使用 Haml 时,您可以使用 HTML-style attributes 为简单起见,例如%form(action="new" method="post")%label(for="username);只有当您需要对值使用复杂的 Ruby 表达式时,使用 Ruby 哈希的好处才会出现;否则,它只是更多的打字。

标签: sinatra haml


【解决方案1】:

这是我测试过的

get '/new' do
    haml :new
end

post '/new' do
    #radcheck = Radcheck.new(:username => params[:username])
    username = params[:username]
    if username
        username
    else
        "Hello World" 
    end
end

和新的.haml

%form{ :action => "/new", :method => "post"}
  %fieldset
    %ol
      %li
        %label{:for => "username"} Name:
        %input{:type => "text", :name => "username", :class => "text"}
    %input{:type => "submit", :value => "Send", :class => "button"}

它按预期工作。所以出于某种原因radcheck.save 返回 false,但这与 haml 无关。 (但请注意,我已经用:name => "username" 更正了输入)

【讨论】:

  • 嗨,现在提交表单并显示用户名的值但不保存到数据库?为什么会这样?
  • 因为我没有打电话给save 我只是想检查一下post是否有效。如果您不提供有关该部分代码的更多详细信息,为什么您的代码中的 radcheck.save 返回 false 我无法回答,但表单提交(您的原始问题)按预期工作。
猜你喜欢
  • 1970-01-01
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
相关资源
最近更新 更多