Rails 生成空白记录甚至输入数据

Rails Generates Blank Record Even Input Data

我正在学习使用CRUD,并设置了一个页面来添加一条记录,但是它只生成了空白记录?你能看看我的代码吗?

谢谢!

这是页面控制器:

class PagesController < ApplicationController
   def index
    @test = Page.all
  end

  def new
    @test = Page.new
  end

  def create
    @test = Page.new(params.permit(:subject_id,:name,:position,:visible,:permalink))
    if @test.save
      flash[:notice] = "Page created successfully!"
      redirect_to(:action => 'index')
    else
      render('new')
    end
  end

end

这里是 new.html.erb

<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>

<div class="pages new">
  <h2>Create Subject</h2>

  <%= form_for(:Page, :url => {:action => 'create'}) do |f| %>

    <table summary="Page form fields">
     <tr>
        <th>Subject_ID</th>
        <td><%= f.text_field(:subject_id) %></td>
      </tr>
      <tr>
        <th>Name</th>
        <td><%= f.text_field(:name) %></td>
      </tr>
      <tr>
        <th>Position</th>
        <td><%= f.text_field(:position) %></td>
      </tr>
      <tr>
        <th>Visible</th>
        <td><%= f.text_field(:visible) %></td>
      </tr>
       <tr>
        <th>Permalink</th>
        <td><%= f.text_field(:permalink) %></td>
      </tr>
    </table>

    <div class="form-buttons">
      <%= submit_tag("Create Subject") %>
    </div>

  <% end %>
</div>

这里是index.html.erb

<div class="pages index">
  <h2>Pages</h2>

 # <%= link_to("Add New Page", {:action => 'new'}, :class => 'action new') %>

  <table class="listing" summary="Page list">
    <tr class="header">
      <th>Position</th>
      <th>Page Name</th>
      <th>Visible</th>
      <th>Pages</th>
      <th>Actions</th>
    </tr>
    <% @test.each do |f| %>
    <tr>
      <td><%= f.position %></td>
      <td><%= f.name %></td>
      <td class="center"><%= f.visible %></td>
      <td class="center"><%= f.permalink %></td>
      <td class="actions">
        <%= link_to("Show", {:action => 'show', :id => f.id}, :class => 'action show') %>
        <%= link_to("Edit", {:action => 'edit', :id => f.id}, :class => 'action edit') %>
        <%= link_to("Delete", {:action => 'delete', :id => f.id}, :class => 'action delete') %>
      </td>
    </tr>
    <% end %>
  </table>
</div>

非常感谢!

强参数

Rails 在嵌套哈希中发送表单参数。

{ page: { subject_id: 1, name: 'Hello World.' } }

因此要将参数列入白名单。

class PagesController < ApplicationController
   def index
    @test = Page.all
  end

  def new
    @test = Page.new
  end

  def create
    @test = Page.new(page_params)
    if @test.save
      flash[:notice] = "Page created successfully!"
      redirect_to(:action => 'index')
    else
      render('new')
    end
  end

  private 

  def page_parameters
    params.require(:page)
          .permit(:subject_id,:name,:position,:visible,:permalink)
  end
end

这就像在做:

params[:page].slice(:subject_id,:name,:position,:visible,:permalink)

form_for 和型号

此外,您的表格应为:

 <%= form_for(:page, :url => {:action => 'create'}) do |f| %>

或者:

 <%= form_for(@page, :url => {:action => 'create'}) do |f| %>

:Page 会在您的参数中提供错误的密钥,并阻止 rails 将表单绑定到您的 @page 对象。 (当它无效时,张贴的值将从表单中消失)。