Open a Bootstrap Modal to edit a record in Ruby on Rails

后端 未结 2 1235
余生分开走
余生分开走 2021-01-06 15:21

I have a \"user\" model. I have a list of users on the user index page with an edit button beside each. I want to click on the edit button for each user which will open a bo

2条回答
  •  清酒与你
    2021-01-06 15:33

    The current form is not for editing the record, change it to:

    users/index.html.erb

    Listing users

    <% @users.each do |user| %> <% end %>
    Name Company Email
    <%= user.name %> <%= user.company %> <%= user.email %> <%= link_to 'Show', user %> <%= link_to 'Edit', '#', 'data-target' => "#myModal_#{user.id}", 'data-toggle' => 'modal' %> <%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %>

    user/_form.html.erb

    <%= form_for(user, remote: true) do |f| %>
        <% if @user.errors.any? %>
            

    <%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:

      <% @user.errors.full_messages.each do |msg| %>
    • <%= msg %>
    • <% end %>
    <% end %>
    <%= f.label :name %>
    <%= f.text_field :name %>
    <%= f.label :company %>
    <%= f.text_field :company %>
    <%= f.label :email %>
    <%= f.text_field :email %>
    <%= f.submit %>
    <% end %>

    If you want clicking the submit to refresh the entire page, just remove remote: true from the _form.html.erb

提交回复
热议问题