If you want to separate the action by calling link_to (method::delete)

Asked 2 years ago, Updated 2 years ago, 29 views

Click either to call the same action, but
I'd like to decide which link I was called from and divide the process.

In this case, what are the methods?

Thank you for your cooperation.

What I've tried (1)

def destroy
    if params [:micropost_type] = 'delete'
      @ micropost.destroy
      flash[:info] = "I gave up on my goal"
      redirect_to request.reference||root_url and return
    elsif params[:micropost_type] = 'complete'
      @ micropost.destroy
      flash[:success] = "You have achieved your goal"
      redirect_to request.reference||root_url 
    end
  end

<%=link_to 'give up', micropost, method::delete, micropost_type: 'delete', data: {confirm: "Do you really want to give up?"}%>
<%=link_to 'achieve', micropost, method::delete, micropost_type: 'complete'%>

What I've tried (2)

def destroy
    if params [:delete]
      @ micropost.destroy
      flash[:info] = "I gave up on my goal"
      redirect_to request.reference||root_url and return
    elsif params [:complete]
      @ micropost.destroy
      flash[:success] = "You have achieved your goal"
      redirect_to request.reference||root_url 
    end
  end

<%=link_to 'give up', micropost, method::delete, name: "delete", data: {confirm: "Do you really want to give up"}%>
<%=link_to 'achieve', micropost, method::delete, name: "complete"%>

ruby-on-rails ruby

2022-09-29 22:37

1 Answers

If I were to fix it, it would look like this.

def destroy
  if params[:type] == 'give_up'
    flash[:info] = 'I gave up on my goal'
  elsif params[:type] == 'complete'
    flash[:success] = 'You have achieved your goal'
  end
  @ micropost.destroy
  redirect_to request.reference||root_url 
end

<%=link_to 'give up', micropost_path(type: 'give_up', method::delete, data:{confirm: 'Do you really want to give up'}%>
<%=link_to 'achieve', micropost_path(type: 'complete', method::delete%>

The point is

Yes.
 By the way, I would like to change the model name micropost, but I don't know what it means, so I left it as it is.


2022-09-29 22:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.