我正在將我的待辦應用程序構建爲我未來項目的項目。目前它缺乏很多功能,並且正在遭受可怕的糟糕的代碼疾病。我打算拋光它,並有一定的運氣和噸skillz也許有一天它會幫助我找到一份工作,作爲一名初級開發人員或至少一名實習生;-)Rails 4.0.4,希望當item.done == true時檢查複選框
目前,我正在與複選框打骯髒的鬥爭。我希望他們能夠(並保持!)檢查他們連接的Item是否具有Item.done == true,否則應該取消選中它們。我嘗試在自己的check_box_tag中實現它,有條件地添加checked =「checked」prop,如果item.done == true,但它失敗。我使用的是現在的黑客有幾個缺陷:
0)(非常爲您呈現與這樣的陰雨,潮溼,骯髒的MOIST代碼) 1)很抱歉它是非常糟糕的代碼的縮影,如果我見過一個 2)它並不總是有效的 - 如果你連續多次檢查一個盒子,所有盒子都會呈現爲空白。 3),它只是看起來不正確,坦白地說
鏈接到github上: https://github.com/Demoniszcze/todo_app
鏈接的Heroku: http://murmuring-citadel-7289.herokuapp.com/
_items.html.erb:
<table>
<tbody>
<% @items.order('created_at asc').each do |item|%>
<tr>
<td><%= check_box_tag "item-#{item.id}", item.id, false,
data: {
remote: true,
url: url_for(action: :do_item, id: item.id),
method: :patch,
done: item.done
} %>
</td>
<td width="300px"><div class="items" id="item-<%= item.id %>"><%= item.content %></div></td>
<td><%= link_to "delete", item_path(item.id), method: :delete, remote: true %></td>
</tr>
<% end %>
</tbody>
</table>
<script>
(function() {
$('input[data-done="true"]').prop('checked', 'checked');
$('input[data-done!="true"]').removeAttr('checked');
})();
</script>
items_controller.rb:
def do_item
@items = Item.all
@item = Item.find(params[:id])
respond_to do |format|
if @item.done
if @item.update(done:false)
format.html { redirect_to :index }
format.js {}
else
format.html { redirect_to :index }
end
else
if @item.update(done:true)
format.html { redirect_to :index }
format.js {}
else
format.html { redirect_to :index }
end
end
end
end
是的,使用jQuery每單一friggin時間。當我看着這個時候,上帝的眼睛受傷了。請,請告知。
確實如此!我多麼愚蠢的想念這一個,我感到羞愧@ _ @ – user3471753