简体   繁体   English

如何从静态页面向Rails应用程序提交表单内容?

[英]How can I submit form content to a Rails app from a static page?

I have an existing static HTML page and I wish to add an email signup form. 我有一个现有的静态HTML页面,我希望添加一个电子邮件注册表单。 In the interest of time I don't want to re-create the page as a view. 为了时间的利益,我不想重新创建页面作为视图。 Is it possible to use a static form to submit new emails to a Rails controller? 是否可以使用静态表单向Rails控制器提交新电子邮件?

On the HTML page: 在HTML页面上:

<form method="POST" action="" class="emails">
<input type="text" class="inputbox">
<input type="submit" value="Sign up" class="button" />
</form>

existing Rails controller: 现有的Rails控制器:

def create
  @subscription = Subscription.new(params[:subscription])

  respond_to do |format|
    if @subscription.save
      format.html { redirect_to @subscription, notice: 'Subscription was successfully created.' }
      format.json { render json: @subscription, status: :created, location: @subscription }
    else
      format.html { render action: "new" }
      format.json { render json: @subscription.errors, status: :unprocessable_entity }
    end
  end
end

The name attribute on an input will determine the hash key in params . 输入上的name属性将确定params的散列键。

So then 那么

<input type="text" name="foo">

will be available through 将通过

params[:foo]

使用<input type="text" class="inputbox" name ="subscription[name]">假设name是订阅表中的字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM