简体   繁体   中英

Multiple submit buttons rails

I am trying to make multiple submit buttons for a form, but I keep getting this error:

Couldn't find User with id=edit_individual

Found in:

# Use callbacks to share common setup or constraints between actions.
def set_user    
@user = User.find(params[:id])
end

My controller page:

def edit_individual
  if User.find_by_id (params[:user_ids])
    if params[:Delete_multiple]
      @users = User.find(params[:user_ids])
      @users.each do |user|
        user.destroy
      end
      redirect_to :back, notice: 'All selected users were successfuly deleted'
    else
      @users = User.find(params[:user_ids])
    end
  else
    redirect_to :back, alert: 'No users were selected for editing'
  end
end

def update_individual

  @users = User.update(params[:users].keys, params[:users].values).reject { |p| p.errors.empty? }
    if @users.empty?
      redirect_to :back, notice: "Users updated"
    else
    render :action => "edit_individual"
    end

end

My index page:

<%= form_tag edit_individual_users_path do %>
<table class="table table-hover">
  <thead>
    <tr>
      <th></th>
      <th><%= sort_link @q, :firstName, "First name" %></th>
      <th><%= sort_link @q, :lastName, "Last name" %></th>
      <th><%= sort_link @q, :registrationNumber, "Registration number" %></th>
      <th><%= sort_link @q, :email, "Email" %></th>
      <th></th>
     </tr>
  </thead>
  <tbody>
    <% @users.each do |user| %>
    <% if (user.archive == false) %>
      <tr>
      <td><%= check_box_tag "user_ids[]", user.id %></td>
      <td><%= user.firstName %></td>
      <td><%= user.lastName %></td>
      <td><%= user.registrationNumber %></td>
      <td><%= user.email %></td>
      <td><%= link_to 'View', user, class: 'btn btn-info btn-mini', title: 'View users\'s information' %></td>
      <td><%= link_to 'Edit', edit_user_path(user), class: 'btn btn-warning btn-mini', title: 'Edit users\'s information', method: 'get' %></td>
      <td><%= link_to 'Delete', user, class: 'btn btn-danger btn-mini', title: 'Delete user', method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
    <% end %>
  </tbody>
  </table>
  <p><%= select_tag :field, options_for_select([["All Fields", ""], ["First name", "firstName"], ["Last name", "price"], ["Registration number", "registrationNumber"], ["Email", "email"]]) %></p>

<p><%= submit_tag "Edit checked", name: 'Edit_multiple' %></p>
<p><%= submit_tag "Delete checked", name: 'Delete_multiple', data: { confirm: 'Are you sure you want to delete multiple users?' } %></p>
<% end %>

The edit_individual page:

<% form_tag update_individual_users_path, :method => :put do %>
  <% for product in @products %>
    <% fields_for "users[]", users do |f| %>
      <h2><%=h user.name %></h2>
      <%= render :partial "fields", :locals => { :f => f, :user => user } %>
    <% end %>
  <% end %>
  <p><%= submit_tag "Submit" %></p>
<% end %>

The routes file:

resources :users do
  get 'archive', on: :collection
  post 'edit_individual', on: :collection
  put 'update_individual', on: :collection
end

The delete part works fine. Also, the edit part actually edits the field, but displays that error (so it does not redirect back to the index page which makes it look bad).

Can anyone help me fix this? Spent a lot hours to have both edit and delete so it would be pretty bad if I had to delete the edit part.

EDIT 1: Also, If I am editing for example 3 users and click the submit button allowing only 2/3 to pass (due to validation), there is no error (I just get redirected back to edit_individual.html to finish the last user) and then if I have click submit again and everything passes, I get an error.

I have actually managed to find what was wrong. The problem was with:

def update_individual

  @users = User.update(params[:users].keys, params[:users].values).reject { |p| p.errors.empty? }
  if @users.empty?
    redirect_to :back, notice: "Users updated"
  else
    render :action => "edit_individual"
  end

end

Instead of:

redirect_to :back

It should be:

redirect_to users_path

Or any other fixed path I suppose. Is there a way to keep "redirect_to :back", or do I have to create new methods for other views (within the same controller) that will use this?

it should not be run because you has not any params[:id]

if you user before_filter then

before_filter: set_user except: [:update_individual, :edit_individual]

def set_user    
 @user = User.find(params[:id])
end

and where you call update_individual in edit_individual

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM