2016-09-16 10 views
0

を更新しない:ビューでレール更新アクションは、ポストのanonyminityを更新しようと

<%= 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 
コンソールで

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) 

エラーはないと思う属性は更新されません。ヘルプ ため おかげ更新 は、モデル

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

妥当性検査が合格しないようです。コンソールで見ることができるように、あなたが置いた、印刷したputsの評価はありません。つまり、update_attributesfalseを返します。

byebugを使用して、ブレークポイントを設定して@post.errors.messages.inspectを評価して、なぜ更新していないのかを確認してください。あなたの開発宝石にbyebugを含めたくない場合は、もう一つのオプションはRails.logger.info(@post.errors.messages.inspect)です。もちろん、それは@post.update_attributesの後に置く必要があります(ifは終了します)

0

にあなたは01にブール値を取得しますがtruefalseruby理解しています。したがって、保存する前に1trueに、0をfalseに変換する必要があります。あなたはこの

before_validation :convert_boolean 

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

ようPostモデルに代わり、あなたがのparamsでtrue/false代わりの0/1を渡すことができ、これを避けるためにそれを行うことができます。

役に立てば幸いこの

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

をお試しください!

+0

の質問を更新しましたが、まだ変更はありません –

+0

'anonymous'はブール値フィールドですか? – RSB

+0

モデルコードを掲載できますか? – RSB

関連する問題