[英]Ruby on Rails proper way to add a favorite button for front end
So far I have two tables User and Company. 到目前为止,我有两个表User和Company。 Users are able to favorite many companies.
用户能够收藏很多公司。 I established all the back end code to add favorites (a join table with many to many relationship) I also have a general setup to add favorites.
我建立了所有后端代码以添加收藏夹(具有多对多关系的联接表),我也有一个常规设置来添加收藏夹。
I need some help with the front end side though. 我在前端方面需要一些帮助。
So far what I have is a search page where users can query a company ticker to get companies. 到目前为止,我所拥有的是一个搜索页面,用户可以在其中查询公司代码来获取公司。 I display it like the following
我显示如下
<% if @results %>
<h4>Search results for <%= @symbol %></h4>
<table>
<tr>
<th>ID</th>
<th>Symbol</th>
<th>Name</th>
</tr>
<% @results.each do |res| %>
<tr>
<td><%= res.id %></td>
<td><%= res.symbol %></td>
<td><%= res.name %></td>
<td><%= link_to "Favorite", create_favorite_path(current_user.id, res.id), method: :post, :remote => true %></td>
</tr>
<% end %>
</table>
<% end %>
Right now I have a "favorite button" implemented with 现在,我有一个“收藏夹按钮”
<td><%= link_to "Favorite", create_favorite_path(current_user.id, res.id), method: :post, :remote => true %></td>
It seems messy and I dont think this is right. 看起来很乱,我不认为这是正确的。 I also want to add a few more features
我还想添加更多功能
I would love suggestions to improve this and do it properly 我很乐意提出建议,以改善此问题并正确执行
I prefer using a front-end framework to handle things like this. 我更喜欢使用前端框架来处理这样的事情。 They allow the components to be much more dynamically interactive.
它们使组件可以更加动态地交互。
re: I want to know if the post method was successful or not in the absence of a front-end framework you can catch failed save in the favorites_controller and add a flash[:error] to the page. 回复:我想知道在没有前端框架的情况下post方法是否成功,您可以将失败的保存保存到收藏夹控制器中并在页面中添加flash [:error]。
flash[:errors] = @favorite.errors.full_message unless @favorite.save!
re: Based on the post, I want to be able to change the button text to something like "saved" You can use jQuery to change the button CSS on click of the favorite button. 回复:根据帖子,我希望能够将按钮文本更改为类似“保存”的内容。单击“收藏夹”按钮时,可以使用jQuery更改按钮CSS。
<% if @results.favorited? %>
# render button with favorited class (with star)
<% else %>
# render button without favorited class (without star)
<% end %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.