【问题标题】:Ruby on rails Landing PageRuby on rails 登陆页面
【发布时间】:2014-08-11 06:41:08
【问题描述】:

我有一个非常基本的 rails 4 应用程序。这就像一个登录页面,其中包含从 youtube 嵌入的视频,并带有一个供用户输入电子邮件和邮政编码的字段...

所以,我很难保存这个用户的电子邮件和邮政编码,实际上,我是 Rails 新手,我不知道该怎么做......我创建了一个名为 Information 的模型,带有电子邮件和邮政编码(两者字符串)...我有一个名为 home 的视图,其中包含以下代码:

    <%= form_for :information do |f| %>
  <div class="form-group">
       <%= f.label :Email_address %>
       <%= f.text_field :email, class: "form-control", :autofocus => true, placeholder: "Enter email" %>
  </div>

  <div class="form-group">
       <%= f.label :Zip_Code %>
       <%= f.text_field :zipcode, class: "form-control", :autofocus => true, placeholder: "Zip Code" %>
  </div>

  <div class="form-group">
        <%= f.submit "Submit", class: "btn btn-danger" %>
  </div>
<% end %>

但是当我点击提交时没有任何反应,我想我应该创建一个控制器,但我不知道要放什么让它工作!我应该怎么做才能以最好的方式收集这两个信息?非常感谢!

【问题讨论】:

  • 您阅读过“入门”rails 指南吗? guides.rubyonrails.org/getting_started.html我认为读完你应该有知识来写这篇文章。有一个表格示例,可以使用评论者姓名和正文添加评论。使用电子邮件和邮政编码创建条目的表单将非常相似。

标签: ruby-on-rails ruby


【解决方案1】:

您将需要具有两个操作的控制器::new:create。 在控制台rails g controller informations(我假设您的模型称为Information)。 在这个文件中

def new
  @information = Information.new
end

def create
  @information = Information.new(information_params)
  redirect_to @information
end

private

def information_params
 params.require(:information).permit(:email, :zipcode)
end

然后这段代码应该作为new.erb进入/view/informations/new

<%= form_for @information do |f| %>
  <div class="form-group">
       <%= f.label :Email_address %>
       <%= f.text_field :email, class: "form-control", :autofocus => true, placeholder: "Enter email" %>
  </div>

  <div class="form-group">
       <%= f.label :Zip_Code %>
       <%= f.text_field :zipcode, class: "form-control", :autofocus => true, placeholder: "Zip Code" %>
  </div>

  <div class="form-group">
        <%= f.submit "Submit", class: "btn btn-danger" %>
  </div>
<% end %>

并查看一些快速教程,以基本了解 MVC 是如何建立的。本指南http://www.railstutorial.org/book 几乎是每个人的起点。

【讨论】:

  • 您的create 操作将导致MissingTemplate 错误
  • &lt;%= form_for information do |f| %&gt; 将抛出 undefined method: information 的异常
  • 好!你已经更新了form,现在也更新create 动作。
【解决方案2】:

让我为你解释一些事情,因为你是新人


对象

Ruby(以及 Rails 是基于 Ruby 构建的)是 object orientated。这意味着您与 Rails 后端的每次交互(着陆页最初不与后端交互)都必须以 objects

为中心

虽然您通过创建相应的对象(使用Information 模型)做得很好,但您需要了解创建、填充和初始化相关对象的过程

--

表格

您正在使用form_for 需要一个 ActiveRecord 对象。这是您的主要失败 - 假设您的目标网页位于 application#welcome,这是

#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
   def welcome
      @information = Information.new #-> creates "information" object
   end
end

然后form_for 方法可以采用@information 对象,根据需要填充数据:

#app/views/application/welcome.html.erb
<%= form_for @information do |f| %>
   <%= f.text_field :email %>
   <%= f.text_field :zipcode %>
   <%= f.submit %>
<% end %>

注意您在此处如何使用@information 对象?这就是 ActiveRecord 对象的用武之地 - 允许您根据需要“填充”它

--

后端

#config/routes.rb
root: "application#welcome"
resources :information, only: :create

form_for 会将您的请求发送至information_controller.rb

#app/controllers/information_controller.rb
Class InformationController < ApplicationController
   def create
      @information = Information.new(information_params)
      if @information.save
         flash[:notice] = "Message Sent - Thank you!"
         redirect_to "application#welcome"
      end
   end

   private

   def information_params
       params.require(:information).permit(:email, :zipcode)
   end
end

这将能够获取@information 对象,将其填充到数据库中,然后再次重定向到原始“登陆”页面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多