2017-03-10 166 views
0

我對rails比較陌生。這一定是一個非常基本的問題,但我無法找到答案。simple_form_for似乎沒有更新記錄

我的問題:按提交似乎並沒有更新我的記錄。

<%= 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 

而現在應用程序/控制器/ 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 - 自我或@select_statement只是充滿尼爾斯的。

顯然,我錯過了一些非常基本的東西。

+0

可以請你分享你的控制器文件 –

+0

哦哇。如果問一個問題可以集中注意力並讓你在不同的地方尋找,這真是太神奇了。 好的,我在**創建**時查看參數,我得到了'{「utf8」=>「✓」,「authenticity_token」=>「xxx ==」,「select_statement」=> {「title」 =>「」,「description」=>「」,「sql_select_statement」=>「abcd;」,「favorite」=>「0」},「commit」=>「Create Select statement」,「controller」=> select_statements「,」action「=>」create「} 這是正確的。 還有 '@select_statement = SelectStatement.new({user_email:@current_user})'不會在params中合併。 我必須手動進行合併嗎? (我添加了控制器。) – RalphShnelvar

回答

0

所以,令我驚訝的是,Rails 4增加了一個全新的安全特性:強參數。

看來,我期待的行爲 - 行屬性會自動填充 - 在Rails 4中不再是真實的,並且行/記錄分配必須手動處理。

我找到了一個很好的起跳點,學習強大的參數是: http://blog.trackets.com/2013/08/17/strong-parameters-by-example.html

這是我的小白信念。我歡迎評論和更正。