Rails: 为什么我不能将新的模型实例保存到数据库中?

Rails: Why can't I save a new model Instance to the database?

我的控制器的#create 操作如下所示。我不知道,为什么 ruby/rails 不接受参数赋值,因此我保存的对象有 al "nil"-attributes

def create
  @new_page = Page.new 
  @new_page.slug = helpers.create_slug_from_title(params[:title])

  # Yes, I should also save the rest of params and make
  # params a strongparameter object
        
  # how to handle errors on create ?
  begin 
    @new_page.save
  rescue
  end

end

如有任何提示,我将不胜感激。

你的

冯·斯波茨

PS:在控制台中看起来没什么不同。

2.7.0 :038 > pn = Page.new
2.7.0 :039 > pn.slug = "index-page"
2.7.0 :040 > pn.slug
 => "index-page" 
2.7.0 :041 > pn
 => #<Page id: nil, text: nil, slug: nil, created_at: nil, updated_at: nil, title: nil, streams_id: nil> 
2.7.0 :042 > pn.save
   (0.2ms)  begin transaction
  Page Create (0.6ms)  INSERT INTO "pages" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2021-05-07 17:01:15.111775"], ["updated_at", "2021-05-07 17:01:15.111775"]]
   (31.5ms)  commit transaction
 => true 
2.7.0 :043 > Page.all
Page Load (0.2ms)  SELECT "pages".* FROM "pages" LIMIT ?  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Page id: 1, text: nil, slug: nil, created_at: "2021-05-07 15:40:59", updated_at: "2021-05-07 15:40:59", title: nil, streams_id: nil>, #<Page id: 2, text: nil, slug: nil, created_at: "2021-05-07 16:46:00", updated_at: "2021-05-07 16:46:00", title: nil, streams_id: nil>, #<Page id: 3, text: nil, slug: nil, created_at: "2021-05-07 17:01:15", updated_at: "2021-05-07 17:01:15", title: nil, streams_id: nil>]> 
2.7.0 :044 > 

PPS:具有创建操作和 page_params 的新控制器,由 max

友情发布
class PagesController < ApplicationController
    
    def home
  end
    
    def show
        @page = Page.where(slug: params[:slug])
    end
    
    def new
        @new_page = Page.new
    end
    
    def edit
        @page = Page.where(slug: params[:slug])
        # error if @page.nil?
    end
    
# POST /pages
  def create
    @page = Page.new(page_params) do |page|
      # smelly - should be handled inside the model not by the controller
      page.slug = helpers.create_slug_from_title(page.title)
    end
        
    if @page.save
      redirect_to @page, status: :created
    else
      render :new, status: :unprocessable_entity
    end
  end
        
  private

  def page_params
    params.require(:page)
                    .permit(:slug, :title, :text)
  end
        
end
class PagesController
  # POST /pages
  def create
    @page = Page.new(page_params) do |page|
      # smelly - should be handled inside the model not by the controller
      page.slug = helpers.create_slug_from_title(page.title)
    end
    if @page.save
      redirect_to @page, status: :created
    else
      render :new, status: :unprocessable_entity
    end
  end

  private

  def page_params
    params.require(:page)
          .permit(:foo, :bar, :title)
  end
end

您通过重新呈现表单来处理经典网络应用中的无效用户输入。 begin ... rescue 在这里不起作用,因为 .save 不会加注。 save! 确实如此,但它在这里的使用是有问题的,因为例外情况应该用于特殊情况——无效的用户输入是日常事件。

在表单中使用错误对象向用户显示验证错误:

<%= form_with(model: @page) do |f| %>
  <% # @todo extract this into a partial %>
  <% if f.object.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(f.object.errors.count, "error") %> prohibited this page from being saved:</h2>
    <ul>
    <% f.object.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
  <% end %>
  
  <div class="field">
    <%= f.label :title %>
    <%= f.text_field :title %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>