Home > database >  Redirect to show action instead of destroy in rails 7
Redirect to show action instead of destroy in rails 7

Time:09-06

Ruby version is 3.1.2 and the rails version is 7.0.3.1

when deleting a user, it's redirecting to show action instead of destroy action in user's controller. Routes for users

resources :users

app/controller/users_controller.rb

before_action :set_user, only: %i[ destroy ]
def destroy
    @user.destroy
    respond_to do |format|
    format.html { redirect_to users_url, notice: "User was successfully destroyed." }
      format.json { head :no_content }
    end
end

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

app/views/users/index.html.erb

<% @users.each do |user| %>
    <div id="users">
        <%= render user %>
    <% end %>
</div>

app/views/users/_user.html.erb

<p>
  <%= link_to "Destroy this user", user, method: :delete %>
</p>

we are not able to delete user.

i have followed below link but that's also not working in my project link_to helper still perfomring get request

thanks!

CodePudding user response:

Less magic with form

<%= button_to "Destroy this user", user, method: :delete %>

or more magic with link

<%= link_to "Destroy this user", user, data: { turbo_method: :delete } %>

And don't forget to add status: :see_other to redirect_to users_url

Rails 7 work such way

  • Related