有没有办法可以使用表单来更新用户在rails中的点数,其中包含一个简单的表单,其中包含我想要追加的名称和点数?
这是我所做的应用程序的链接:enter link description here
我对索引的看法是:
<div class="container">
<h1 class="text-center">Listing Students</h1>
<div class ="jumbotron">
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Points</th>
</tr>
</thead>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.points %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Delete', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<br>
<%= link_to 'New Student', new_user_path, class:'btn btn-primary' %>
</div>
我尝试使用编辑页面中的代码:
<div class="container">
<div class=" text-center">
<div class="col-md-6 col-md-offset-3">
<div class= "well well-lg">
<%= render 'form' %>
<%= link_to 'Back', users_path, class:"back" %>
</div>
</div>
</div>
</div>
在索引中呈现表单但是它给了我一个错误@user不能是nill
我只需要更新索引页面中的用户点,如果我可以更改要显示的模式然后输入名称并进行编辑,那将会很棒。我已经工作了几个小时,似乎更新它。
答案 0 :(得分:0)
当然,您可以使用simple_form或form_for。或者你可以进行现场编辑。关于你究竟需要什么,你的问题不是很准确。
更新1:
假设您已将simple_form gem添加到gemfile中,并且您在routes.rb:resources :users
中有类似的内容,那么UsersController的编辑视图可能如下所示:
#in controller
def edit
@user = User.find(params['id'])
end
观点:
<%= simple_form_for @user do |f| %>
<%= f.input :name %>
<%= f.input :points %>
<%= f.button :submit %>
<% end %>
请尝试一下,让我知道它是否适合您(编辑视图;为了坚持,您还需要并更新操作)。
答案 1 :(得分:0)
在索引页面上添加div以打开模式弹出窗口:
<div class='modal_popup>
</div>
要更新索引页面上的用户名和点,您必须更改编辑链接,如下所示:
<%= link_to 'Edit', edit_user_path(user), remote: true %>
控制器更改:
def edit
@user = User.find(params[:id])
end
_modal.html.erb
<div class="modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<%= form_for @user, remote: true do |f| %>
<div class="modal-body">
<%= f.text_field :name %>
<%= f.text_field :points %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<%= f.submit 'Update', class: "btn btn-primary" %>
</div>
<% end %>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
edit.js.erb
$('.modal_popup').html("<%= j(render partial: '/users/modal') %>");
$('.modal').show();
在update.js.erb中更新用户后,添加以下代码行:
$('.modal').hide();
window.location.reload();
希望这可能会有所帮助..