부울 열이 published
인 모델 항목이 있습니다. 기본값은 false
입니다. 나는 다음과 같은 방법을 추가 내 models/entry.rb
에서모델 부울 메서드가 제대로 작동하지 않습니다.
class CreateMultifloraEntries < ActiveRecord::Migration[5.0]
def change
create_table :multiflora_entries do |t|
t.string :type, index: true
t.string :title
t.string :slug, unique: true
t.json :payload
t.integer :user_id, index: true
t.boolean :published, default: false
t.timestamps
end
end
end
: 다음은 전체 마이그레이션 파일의 내가 만들 때, 그러나
<% @entries.each do |entry| %>
# ...
<% if entry.published? %>
<p> Published <p>
<% else %>
<!-- There will be an AJAX request to set entry published later -->
<%= link_to "Publish", "whatever-path" %>
<% end %>
<% end %>
:
def published?
Entry.where("published", true)
end
및 index.html.erb
에 나는이있어 항목을 내 색인보기로 이동하면 항목이 게시 된 것으로 표시됩니다.
'self.published?'메소드가 항상 true를 반환합니다. –
나는 그것을 얻었고 나의 질문을 편집했다. 나는'self.published '대신에 실수를했다.''published?' '방법이있다. 어떻게 다시 쓸 수 있니? – AlexNikolaev94
여전히 true를 반환합니다. 이 모델에 대한 메소드가 필요하지 않습니다. 아래의 답변이 옳습니다. –