0

i am using ruby 3.1.2 and rails 7.0.3.1, i have changed the edit request get to post in the routes file app/config/routes

 resources :users do
    member do
      post :edit
    end
  end

app/views/users/index.html.erb

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

app/views/users/_user.html.erb add link_to in _user.html.erb partial

<%= link_to "Edit this user", edit_user_path(user), method: :post %>

app/controller/users_controller.rb

def edit
    respond_to do |format|
      format.turbo_stream do
        render turbo_stream: turbo_stream.update("new_user", partial: "users/form", locals: { user: User.find(params[:id]) } ) 
      end
    end
  end

getting unknown format error when trying to edit user.

thanks!

Rahul Gupta
  • 53
  • 1
  • 1
  • 12
  • `method: :post` is the option for the old Rails UJS driver. For Turbo you use `data: { turbo_method: "post" }`. However Spickermann is completely right in that you should use GET and not POST here. https://guides.rubyonrails.org/working_with_javascript_in_rails.html#method – max Sep 05 '22 at 11:45

1 Answers1

0

Edit routes are automatically generated when you define resources :users in your config/routes.rb. And edit routes are GET.

Just remove the member definition from your routes file and change the link_to in the partial to:

<%= link_to "Edit this user", edit_user_path(user) %>

Btw. you can call rails routes on your command line to see all routes that are available in your app, how they are named, HTTP method and what controller and method they route to.

spickermann
  • 100,941
  • 9
  • 101
  • 131
  • still getting the same error unknown format for edit action – Rahul Gupta Sep 05 '22 at 11:40
  • 1
    Check your Rails logs. If the request is being treated as HTML by Rails then you most likely have a JavaScript error thats preventing Turbo from working properly. The next step is to use the web inspector in your browser to ensure that there are no script errors and that the correct assets are loaded. https://guides.rubyonrails.org/working_with_javascript_in_rails.html#turbo – max Sep 05 '22 at 11:44
  • i have used button_to instead of link_to helper it's working perfectly as we wanted. – Rahul Gupta Sep 06 '22 at 05:50