简体   繁体   English

Web服务使用Rails + MySql最佳实践

[英]Web Service using Rails + MySql Best Practices

Alright, I need some advice and best practices from some Rails people. 好吧,我需要一些Rails人士的一些建议和最佳实践。

I'm fairly new to the platform but I've done database-backed web development work before in Java. 我刚接触该平台,但是我之前使用Java完成了数据库支持的Web开发工作。 At this point I've worked through about twenty tutorials, and I keep getting obstructed at the same place. 至此,我已经完成了大约二十个教程,并且在同一地方一直受阻。

Here's what I've done and what I'm experiencing. 这是我所做的事情和正在经历的事情。 I've created a new application using 我使用创建了一个新的应用程序

rails my_rails_app

I've created my schema and tables in MySQL, and I've created an account for rails (rails_root/pass1234) and updated config/databases.yml to reflect the new account: 我已经在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

At this point, I generate the scaffold for the 'customers' table: 至此,我为“客户”表生成了支架:

ruby script/generate scaffold customer

Which returns successfully. 哪个成功返回。 So now we start the server: 现在,我们启动服务器:

ruby script/server

Which boots Mongrel and starts the server up as expected at port 3000 on the local machine. 它将引导Mongrel并按预期在本地计算机上的端口3000上启动服务器。 After directing the browser to http://localhost:3000/customers , the page correctly says "Listing customers" but has no fields listed . 将浏览器定向到http:// localhost:3000 / customers后 ,页面正确显示“列出客户”,但未列出任何字段 When I press the link for "new customer" there are still no fields to modify, but I am allowed to create an empty customer record. 当我按下“新客户”的链接时, 仍然没有可以修改的字段,但是我可以创建一个空的客户记录。 Since this operation places 'nulls' in all fields (some of which aren't nullable,) a Mysql error is thrown -- "column 'name' cannot be null" with the SQL query it was attempting to run. 由于此操作在所有字段中都放置了“ null”(其中某些字段不能为空),因此引发了Mysql错误-尝试运行的SQL查询出现“ column'name'不能为null”。

I'm confused mainly because if it's picking up my fields at that point, why is it not displaying them earlier? 我感到困惑的主要原因是,如果那时正在接我的字段,为什么不更早显示它们? Clearly it's connecting to the right database and has identified the correct table. 显然,它正在连接到正确的数据库,并已确定正确的表。 What's the deal? 这是怎么回事?

Now, on the other hand, if I place entries into the table prior to starting the server, the page now shows 'Show,' 'Edit,' and 'Destroy' link options below the "Listing customers" header. 现在,另一方面,如果我在启动服务器之前将条目放置到表中,则页面现在在“列出客户”标题下方显示“显示”,“编辑”和“销毁”链接选项。 However, no fields or data are reflected. 但是,不会反映任何字段或数据。 If I click "Destroy," the record in the table is indeed destroyed. 如果单击“销毁”,则表中的记录确实被销毁了。 But I still haven't seen any of the records or fields on a web page generated by rails unless it was an error message. 但是除非出现错误消息,否则我仍然看不到rails生成的网页上的任何记录或字段。

This was the output from my generate scaffold command: 这是我的generate scaffold命令的输出:

\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

And here are the major files generated: 以下是生成的主要文件:

app\\controllers\\contacts_controller.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 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 app \\ views \\ contacts \\ show.html.erb

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

app\\views\\contacts\\new.html.erb 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 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 %>

Any (further) advice would be greatly appreciated! 任何(进一步)的建议将不胜感激!

I believe Rails will only add fields for the views it creates in a scaffold if you specify them at the time the scaffold is created, ie " script/generate scaffold post title:string body:text published:boolean " (this will also create a database migration for the table in question, by the way). 我相信,如果您在创建脚手架时指定了它们,Rails只会为在脚手架中创建的视图添加字段,即“ script/generate scaffold post title:string body:text published:boolean ”(这也会创建一个顺便说一下,该表的数据库迁移)。 I don't think it does any inspection of the database during scaffolding, but I haven't dug into the scaffold generation code to be absolutely certain. 我认为在脚手架期间它不会对数据库进行任何检查,但是我没有完全确定要研究脚手架生成代码。

Have you written a migration for the contacts model? 您是否为联系人模型编写了迁移?

Rails reads fields directly from the database and dynamically generates the attributes based on what's present on the table. Rails直接从数据库读取字段,并根据表上的内容动态生成属性。 If you just run the scaffold generator, the migration will still be empty. 如果仅运行脚手架生成器,则迁移将仍然为空。 Once the migration is written, you can run the migrations with rake db:migrate and then I believe the fields should show up in the scaffold. 写入迁移后,您可以使用rake db:migrate运行rake db:migrate ,然后我相信这些字段应该显示在支架中。

It has, however, been a very long time since I touched scaffolds. 但是,自从我接触脚手架以来已经很长时间了。 They are, IMHO, only useful when you're first learning Rails, and their use should be abandoned at the earliest possible moment. 恕我直言,它们仅在您初次学习Rails时有用,并且应尽早放弃使用它们。

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

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