【问题标题】:Web Service using Rails + MySql Best Practices使用 Rails + MySql 最佳实践的 Web 服务
【发布时间】:2009-04-14 16:34:52
【问题描述】:

好的,我需要一些 Rails 人员的建议和最佳实践。

我对这个平台还很陌生,但我之前用 Java 做过数据库支持的 Web 开发工作。至此,我已经完成了大约 20 个教程,并且一直在同一个地方受到阻碍。

这是我所做的以及我正在经历的。我使用

创建了一个新应用程序
rails my_rails_app

我在 MySQL 中创建了架构和表,并为 rails (rails_root/pass1234) 创建了一个帐户,并更新了 config/databases.yml 以反映新帐户:

development:
    adapter: mysql
    encoding: utf8
    reconnect: false
    database: demo_development
    username: rails_root
    password: pass1234
    host: localhost

此时,我为“客户”表生成了脚手架:

ruby script/generate scaffold customer

成功返回。所以现在我们启动服务器:

ruby script/server

这会启动 Mongrel 并按预期在本地计算机上的 3000 端口启动服务器。将浏览器定向到http://localhost:3000/customers 后,页面正确显示“列出客户”,但没有列出任何字段。当我按下“新客户”的链接时,仍然没有要修改的字段,但我可以创建一个空的客户记录。由于此操作在所有字段(其中一些不可为空)中放置了“空值”(其中一些不可为空),因此会引发 Mysql 错误——它试图运行的 SQL 查询出现“列 'name' 不能为空”。

我很困惑,主要是因为如果它在那时拿起我的字段,为什么不早点显示它们?显然它正在连接到正确的数据库并识别出正确的表。有什么关系?

另一方面,如果我在启动服务器之前将条目放入表中,则页面现在会在“列出客户”标题下方显示“显示”、“编辑”和“销毁”链接选项。但是,不会反映任何字段或数据。如果我点击“销毁”,表中的记录确实被销毁了。但是我仍然没有看到由 rails 生成的网页上的任何记录或字段,除非它是错误消息。

这是我的生成脚手架命令的输出:

\ROR\my_app>ruby script/generate scaffold contact
      exists  app/models/
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/contacts
      exists  app/views/layouts/
      exists  test/functional/
      exists  test/unit/
      create  test/unit/helpers/
      exists  public/stylesheets/
      create  app/views/contacts/index.html.erb
      create  app/views/contacts/show.html.erb
      create  app/views/contacts/new.html.erb
      create  app/views/contacts/edit.html.erb
      create  app/views/layouts/contacts.html.erb
      create  public/stylesheets/scaffold.css
      create  app/controllers/contacts_controller.rb
      create  test/functional/contacts_controller_test.rb
      create  app/helpers/contacts_helper.rb
      create  test/unit/helpers/contacts_helper_test.rb
       route  map.resources :contacts
  dependency  model
      exists    app/models/
      exists    test/unit/
      exists    test/fixtures/
      create    app/models/contact.rb
      create    test/unit/contact_test.rb
      create    test/fixtures/contacts.yml
      create    db/migrate
      create    db/migrate/20090414185634_create_contacts.rb

这里是生成的主要文件:

app\controllers\contacts_controller.rb

class ContactsController < ApplicationController
  # GET /contacts
  # GET /contacts.xml
  def index
    @contacts = Contact.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @contacts }
    end
  end

  # GET /contacts/1
  # GET /contacts/1.xml
  def show
    @contact = Contact.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @contact }
    end
  end

  # GET /contacts/new
  # GET /contacts/new.xml
  def new
    @contact = Contact.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @contact }
    end
  end

  # GET /contacts/1/edit
  def edit
    @contact = Contact.find(params[:id])
  end

  # POST /contacts
  # POST /contacts.xml
  def create
    @contact = Contact.new(params[:contact])

    respond_to do |format|
      if @contact.save
        flash[:notice] = 'Contact was successfully created.'
        format.html { redirect_to(@contact) }
        format.xml  { render :xml => @contact, :status => :created, :location => @contact }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @contact.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /contacts/1
  # PUT /contacts/1.xml
  def update
    @contact = Contact.find(params[:id])

    respond_to do |format|
      if @contact.update_attributes(params[:contact])
        flash[:notice] = 'Contact was successfully updated.'
        format.html { redirect_to(@contact) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @contact.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /contacts/1
  # DELETE /contacts/1.xml
  def destroy
    @contact = Contact.find(params[:id])
    @contact.destroy

    respond_to do |format|
      format.html { redirect_to(contacts_url) }
      format.xml  { head :ok }
    end
  end
end

app\views\contacts\index.html.erb

<h1>Listing contacts</h1>

<table>
  <tr>
  </tr>

<% @contacts.each do |contact| %>
  <tr>
    <td><%= link_to 'Show', contact %></td>
    <td><%= link_to 'Edit', edit_contact_path(contact) %></td>
    <td><%= link_to 'Destroy', contact, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New contact', new_contact_path %>

app\views\contacts\show.html.erb

<%= link_to 'Edit', edit_contact_path(@contact) %> |
<%= link_to 'Back', contacts_path %>

app\views\contacts\new.html.erb

<h1>New contact</h1>

<% form_for(@contact) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', contacts_path %>

app\views\contacts\edit.html.erb

<h1>Editing contact</h1>

<% form_for(@contact) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.submit 'Update' %>
  </p>
<% end %>

<%= link_to 'Show', @contact %> |
<%= link_to 'Back', contacts_path %>

任何(进一步的)建议将不胜感激!

【问题讨论】:

  • 听起来你的脚手架没有正确生成。你能发布它创建的代码吗?
  • 当然。我已经发布了它生成的源代码。看起来“show.html.erb”做的不是很多!谢谢,如果您有任何问题,请告诉我。

标签: mysql ruby-on-rails service


【解决方案1】:

如果您在创建脚手架时指定它们,我相信 Rails 只会为它在脚手架中创建的视图添加字段,即“script/generate scaffold post title:string body:text published:boolean”(这也会为相关表创建数据库迁移,顺便一提)。我认为它不会在脚手架期间对数据库进行任何检查,但我还没有深入研究脚手架生成代码以绝对确定。

【讨论】:

    【解决方案2】:

    您是否为联系人模型编写了迁移?

    Rails 直接从数据库中读取字段,并根据表上的内容动态生成属性。如果你只是运行脚手架生成器,迁移仍然是空的。编写迁移后,您可以使用rake db:migrate 运行迁移,然后我相信这些字段应该会显示在脚手架中。

    然而,我已经很久没有接触脚手架了。恕我直言,它们仅在您第一次学习 Rails 时才有用,应该尽早放弃使用它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 2014-07-07
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多