Rails 按钮_至或链接_至图像
菜鸟需要一些帮助。我正在为我的孩子创建一个家务表,它应该看起来像在线版本:
http://learningandeducationtoys.guidestobuy.com/i-can-do-it-reward-chart
杂务在 Y 轴上列出,日期(星期日、星期一...)在顶部 X 轴上列出。孩子们可以点击盒子,完成家务即可获得星星。
Chores/index.html.erb 表是丑陋的代码(抱歉):
列出杂务
<table>
<th><%= "" %></th>
<th><%= "SUN" %></th>
<th><%= "MON" %></th>
<th><%= "TUES" %></th>
<th><%= "WED" %></th>
<th><%= "THURS" %></th>
<th><%= "FRI" %></th>
<th><%= "SAT" %></th>
<% @chores.each do |chore| %>
<tr class="<%= cycle('list-line-odd', 'list-line-even') %>">
<% ##TODO.. Fix to be sure to link "post" action to "show" child. %>
<td>
<%= image_tag(chore.image_url, :class => 'list-image') %>
<dt><%=h chore.title %></dt>
</td>
<td class="button"><%= button_to "Add to Wallet", wallets_path(:chore_id => chore, :child_id => session[:child_id]),
:remote => true %></td>
<td class="button"><%= button_to "Add to Wallet", wallets_path(:chore_id => chore, :child_id => session[:child_id]),
:remote => true %></td>
<td class="button"><%= button_to "Add to Wallet", wallets_path(:chore_id => chore, :child_id => session[:child_id]),
:remote => true %></td>
<td class="button"><%= button_to "Add to Wallet", wallets_path(:chore_id => chore, :child_id => session[:child_id]),
:remote => true %></td>
<td class="button"><%= button_to "Add to Wallet", wallets_path(:chore_id => chore, :child_id => session[:child_id]),
:remote => true %></td>
<td class="button"><%= button_to "Add to Wallet", wallets_path(:chore_id => chore, :child_id => session[:child_id]),
:remote => true %></td>
<td class="button"><%= button_to "Add to Wallet", wallets_path(:chore_id => chore, :child_id => session[:child_id]),
:remote => true %></td>
</tr>
<% end %>
</table>
</div>
上面为每天的每项杂务创建了“添加到钱包”按钮。 Wallets 是 Children 表和 Chores 表之间的连接表。两个问题:
- 如何将按钮切换到表格中的框,单击该框时,该框会变成星星图像(按照示例)?
- 如何重构表中的代码以免违反 DRY 规则?
Noob with some help needed. I am creating a chore chart for my children which should look like a online version of:
http://learningandeducationtoys.guidestobuy.com/i-can-do-it-reward-chart
Chores listed down the Y-axis and Days (Sun, Mon... ) on top X-axis. Boxes for kids to click and receive stars for completed chores.
The chores/index.html.erb table is ugly code (sorry):
Listing chores
<table>
<th><%= "" %></th>
<th><%= "SUN" %></th>
<th><%= "MON" %></th>
<th><%= "TUES" %></th>
<th><%= "WED" %></th>
<th><%= "THURS" %></th>
<th><%= "FRI" %></th>
<th><%= "SAT" %></th>
<% @chores.each do |chore| %>
<tr class="<%= cycle('list-line-odd', 'list-line-even') %>">
<% ##TODO.. Fix to be sure to link "post" action to "show" child. %>
<td>
<%= image_tag(chore.image_url, :class => 'list-image') %>
<dt><%=h chore.title %></dt>
</td>
<td class="button"><%= button_to "Add to Wallet", wallets_path(:chore_id => chore, :child_id => session[:child_id]),
:remote => true %></td>
<td class="button"><%= button_to "Add to Wallet", wallets_path(:chore_id => chore, :child_id => session[:child_id]),
:remote => true %></td>
<td class="button"><%= button_to "Add to Wallet", wallets_path(:chore_id => chore, :child_id => session[:child_id]),
:remote => true %></td>
<td class="button"><%= button_to "Add to Wallet", wallets_path(:chore_id => chore, :child_id => session[:child_id]),
:remote => true %></td>
<td class="button"><%= button_to "Add to Wallet", wallets_path(:chore_id => chore, :child_id => session[:child_id]),
:remote => true %></td>
<td class="button"><%= button_to "Add to Wallet", wallets_path(:chore_id => chore, :child_id => session[:child_id]),
:remote => true %></td>
<td class="button"><%= button_to "Add to Wallet", wallets_path(:chore_id => chore, :child_id => session[:child_id]),
:remote => true %></td>
</tr>
<% end %>
</table>
</div>
The above creates "Add to Wallet" buttons for each chore for each day. Wallets is a join table between Children and Chores Tables. Two questions:
- How do i switch the buttons to boxes in the table which, when clicked, turn to the stars images (as per the example)?
- How do i refactor the code in the table so i don't violate the DRY rule?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有两个问题合二为一,很难回答。对于 DRY 部分,您应该认识到您多次复制粘贴相同的语句,这是不好的形式。一般来说,这种事情最好表达为带有单个语句实例的某种循环。
一个例子是将您的天数汇总到存储在某处的常量中,然后使用它:
可以在方便的地方定义
DAYS
,例如在与此相关的模型或类似的东西中。如果是这种情况,它可能需要像MyModel::DAYS
这样的前缀,但工作原理是一样的。确实不清楚为什么要使用模式
<%= "X" %>
,它的效果是每次X
都会返回相同的字符串也无所谓。您还可以使用相同的原则迭代其他七个
td
元素:即使未使用
day
变量,这里的驱动因素显然是天,所以这是有道理的。如果您每周工作五天,这些列就会相应缩小。至于问题的第二部分,您正在寻找的内容最好表达为这些表格单元格的 jQuery onclick 处理程序。使用不显眼的 JavaScript 技术,您可以非常轻松地在整个 DOM 元素类上定义操作:
您可以使用获取和更新单元格的 AJAX 调用填充
...
部分。此时单元格内不需要实际的按钮,只需一些 CSS 即可使单元格具有正确的大小和颜色。出于样式原因,通过相应调整选择器,可以轻松地将其应用于单元格内的元素。There's sort of two questions in one here which makes it awkward to answer. For the DRY part you should recognize that you're copy-pasting the same statement many times over and this is bad form. Generally that sort of thing is better expressed as some kind of loop with a single instance of the statement.
An example would be rolling up your days into a constant stored somewhere, then using that:
DAYS
could be defined somewhere convenient, like in a model associated with this, or something of the sort. It may require a prefix likeMyModel::DAYS
if this is the case, but will work the same.It's really not clear why you're using the pattern
<%= "X" %>
which has the effect of returning the same string every time whenX
would do just as well.You can also iterate over the seven other
td
elements with the same principle:Even though the
day
variable isn't used, the driving factor here is, apparently, the number of days, so it makes sense. If you had a five day week, those columns would shrink accordingly.As for the second part of your question, what you're looking for would be best expressed as a jQuery onclick handler for those table cells. Using unobtrusive JavaScript techniques, you can define an action on an entire class of DOM elements quite easily:
You'd fill in the
...
part with the AJAX call that fetches and updates the cell. There's no need for an actual button inside the cell at this point, just some CSS to make the cell the right size and color. For style reasons this could just as easily be applied to an element inside the cells by adjusting the selector accordingly.