0

我对rails比较陌生。这一定是一个非常基本的问题,但我找不到答案。

我的问题:按提交似乎没有更新我的记录。

<%= simple_form_for(@select_statement) do |f| %>

  <%= render 'form_error', f:f %>

  <div class="form-inputs">
    <%= f.input :title, :label => 'Required: Short title for this sql SELECT statment' %>
    <%= f.input :description, required: false,  :label => 'Optional: Long description of your sql SELECT statment' %>
    <%= f.input :sql_select_statement, :label => 'Required: Your sql SELECT statment' %>
    <%= f.input :user_email %>
    <%= f.input :favorite, :input_html => { :checked => false } %>

      <%= f.button :submit %>
  </div>
<% end %>

这是我的 .rb 文件

class RalphValidator < ActiveModel::Validator
  def validate(record)
    byebug if ralph_test_byebug
    xyz = 123
  end
end

class EmailValidator < ActiveModel::Validator
  def validate(record)
    # byebug if ralph_test_byebug

    email_regex = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
    record.errors[:base] << 'email address missing or invalid!!!  Internal system error!!!' unless record.user_email =~ email_regex
    # record.errors[:base] << 'surface mail address missing!!!' unless record.user_email =~ email_regex
    # xyz=123
    # record.errors.add :user_email, 'email address missing!!!' unless record.user_email =~ email_regex
  end
end


class SelectStatement < ActiveRecord::Base
  # See http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html
  include ActiveModel::Validations

  attr_accessor :user_email

  before_validation(on: :create) do
    byebug if ralph_test_byebug
    xyz = 123
  end


  validates_with RalphValidator

  validates :sql_select_statement, presence: true
  # validates :user_email, presence: true, email: true
  validates :favorite, inclusion: [true, false]
  byebug if ralph_test_byebug
  validates_with EmailValidator, fields: [:user_email]

  # see http://www.informit.com/articles/article.aspx?p=2220311&seqNum=2
  before_save :ralph_before_save

  def ralph_before_save
    byebug if ralph_test_byebug
    xyz=123
  end


  if true
    def initialize(args)
      byebug if ralph_test_byebug

      # See http://stackoverflow.com/questions/23050181/ruby-class-new-gives-class-not-initialized-error-in-rails-console
      super

      byebug if ralph_test_byebug

      @initialize_args = args           # May be unnecessary
      @user_email = args[:user_email]   # Probably unnecessary

      byebug if ralph_test_byebug
      xyz=123
    end
  end

  if true
    # See http://guides.rubyonrails.org/active_record_callbacks.html#after-initialize-and-after-find

    after_initialize do |s_hash|
      zyx = new_record?

      byebug if ralph_test_byebug

      s_hash[:user_email] = @user_email

      byebug if ralph_test_byebug
      # xyz=123
    end
  end
end

现在 app/controllers/select_statements_controller.rb

class SelectStatementsController < DeviseApplicationController
  before_action :set_select_statement, only: [:show, :edit, :update, :destroy]

  # GET /select_statements
  # GET /select_statements.json
  def index
    @here = s_here(__FILE__, __LINE__)
    byebug if ralph_test_byebug
    @select_statements = SelectStatement.all
    byebug if ralph_test_byebug
    xxx=123
  end

  # GET /select_statements/1
  # GET /select_statements/1.json
  def show
    @here = s_here(__FILE__, __LINE__)
    byebug if ralph_test_byebug
    xxx=123
  end

  # GET /select_statements/new
  def new
    @here = s_here(__FILE__, __LINE__)
    byebug if ralph_test_byebug
    @select_statement = SelectStatement.new({user_email:@current_user})

    byebug if ralph_test_byebug
    xyz=123
  end

  # GET /select_statements/1/edit
  def edit
    @here = s_here(__FILE__, __LINE__)
    byebug if ralph_test_byebug
    xxx=123
  end

  # POST /select_statements
  # POST /select_statements.json
  def create
    @here = s_here(__FILE__, __LINE__)
    # byebug if ralph_test_byebug

    # @select_statement = SelectStatement.new(select_statement_params:select_statement_params)
    # @select_statement = SelectStatement.new({user_email:@current_user, select_statement_params:select_statement_params})
    @select_statement = SelectStatement.new({user_email:@current_user})

    byebug if ralph_test_byebug

    respond_to do |format|
      if @select_statement.save
        format.html { redirect_to @select_statement, notice: 'Select statement was successfully created.' }
        format.json { render :show, status: :created, location: @select_statement }
      else
        format.html { render :new }
        format.json { render json: @select_statement.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /select_statements/1
  # PATCH/PUT /select_statements/1.json
  def update
    @here = s_here(__FILE__, __LINE__)
    byebug if ralph_test_byebug
    xxx=123

    respond_to do |format|
      if @select_statement.update(select_statement_params)
        format.html { redirect_to @select_statement, notice: 'Select statement was successfully updated.' }
        format.json { render :show, status: :ok, location: @select_statement }
      else
        format.html { render :edit }
        format.json { render json: @select_statement.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /select_statements/1
  # DELETE /select_statements/1.json
  def destroy
    @here = s_here(__FILE__, __LINE__)
    byebug if ralph_test_byebug

    @select_statement.destroy
    respond_to do |format|
      format.html { redirect_to select_statements_url, notice: 'Select statement was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_select_statement
      byebug if ralph_test_byebug

      @select_statement = SelectStatement.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def select_statement_params
      # byebug if ralph_test_byebug

      params.require(:select_statement).permit(:title, :description, :sql_select_statement, :user_email, :favorite)
    end
end

我的基本问题:Rails 在哪里将表单的字段插入到记录中?例如,我尝试过 before_validate、validates_with、before_save、...

每次我在上面的代码中遇到 byebug 断点时——除了与 :user_email 关联的属性——self 或 @select_statement 都充满了 nil。

显然,我错过了一些非常基本的东西。

4

1 回答 1

0

因此,令我惊讶的是,Rails 4 添加了一个全新的安全功能:强参数。

看来我所期待的行为 - 行属性将自动填充 - 在 Rails 4 中不再适用,并且必须手动处理行/记录分配。

我发现学习强参数的一个很好的起点是: http ://blog.trackets.com/2013/08/17/strong-parameters-by-example.html

这是我的菜鸟信念。欢迎评论和指正。

于 2017-03-11T00:51:48.647 回答