2016-09-16 102 views
0

嘗試更新職位的anonyminity:Rails的更新動作不更新

在控制檯

Started PATCH "/posts/9" for 94.187.88.109 at 2016-09-16 20:58:09 +0000 
Processing by PostsController#update as JS 
    Parameters: {"utf8"=>"✓", "post"=>{"anonymous"=>"1"}, "commit"=>"Save", "id"=>"9"} 
    User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]] 
    Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? ORDER BY created_at DESC LIMIT 1 [["id", 9]] 
    (0.2ms) begin transaction 
    (0.1ms) rollback transaction 
    (0.1ms) begin transaction 
    (0.1ms) rollback transaction 
    Rendered posts/update.js.erb (0.1ms) 
Completed 200 OK in 150ms (Views: 22.4ms | ActiveRecord: 0.9ms) 

在那裏的視圖

<%= form_for (post), remote: true do |f| %> 
          <%= f.check_box :anonymous %> 
          <span>Anonymous</span> 
          <%= f.submit 'Save' %> 
         <% end %> 

控制器

def update 
@post = Post.find(params[:id]) 
if @post.update_attributes(permit_post) 
    puts 'aaaaaaaaaaaaaaa' 
    puts @post.anonymous === true 
    puts 'aaaaaaaaaaaaaaa' 
    respond_to do |format| 
    format.html 
    format.js 
    end 
end 
end 
private 
def permit_post 
params.require(:post).permit(:image, :title, :long, :anonymous, :facenumber); 
end 

apears沒有錯誤b ut屬性沒有更新。 感謝您的幫助 更新 模型

class Post < ActiveRecord::Base 
include PublicActivity::Common 
cattr_accessor :current_user 
cattr_accessor :user_id 
acts_as_votable 
has_attached_file :image, styles: { medium: "550", large: "600", thumb: "100x100>" }, default_url: "/missing.png" 
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/ 
belongs_to :user 
belongs_to :category 
validates :user_id, presence: true 
validates :image, presence: true 
has_many :comments 

+0

什麼在'permit_post'? – RSB

+0

@RSB更新了問題 –

回答

1

在我看來,驗證沒有通過。正如你在控制檯中看到的那樣,你沒有投入任何投入評估,打印。這意味着update_attributes正在返回false

爲什麼不使用byebug來放置斷點並評估@post.errors.messages.inspect,以檢查它爲什麼不更新?另一種選擇是Rails.logger.info(@post.errors.messages.inspect),如果你不想在你的開發寶石中加入。當然,需要在@post.update_attributes(其中if完成後)

0

您在01ruby越來越布爾值理解truefalse。因此,您需要在保存之前將1轉換爲true0爲false。你可以這樣做,在Post模型這樣

before_validation :convert_boolean 

def convert_boolean 
    self.anonymous = (anonymous != nil and anonymous != '0')  
end 

爲了避免這種情況,或者你可以傳遞的true/false代替0/1 params中。

試試這個

<%= f.check_box :anonymous, {}, true, false %> 

希望幫助!

+0

沒有工作,仍然沒有變化 –

+0

'匿名'是一個布爾字段吧? – RSB

+0

你可以發佈你的型號代碼嗎? – RSB