Osborne Saka
Osborne Saka

Reputation: 509

No route matches [POST] in Rails

On submitting the forms I get a routing error

resources :sellers do
    resources :seller_profiles
end
<%= form_for seller_seller_profiles_path do |form| %>
  <div class="field">
    <%= form.label :first_name %>
    <%= form.text_field :first_name %>
  </div>

  <div class="field">
    <%= form.label :other_name %>
    <%= form.text_field :other_name %>
  </div>

   -- more similar fields ---

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>
 def create 
    @seller_profile = @seller.seller_profile.create!(seller_profile_params)

    respond_to do |format|
      format.html { redirect_to root_path}
    end

Error Message: Started POST "/sellers/1/seller_profiles/new" for ::1 at 2021-01-11 21:37:01 +0000

ActionController::RoutingError (No route matches [POST] "/sellers/1/seller_profiles/new"):

UPDATED

I changed the view to

<%= form_with(model: [@seller, @seller_profile], local: true) do |form| %>
--- code continues ---

Now the new routes work but the edit routes throws this error ActionView::Template::Error (undefined method `seller_profile_path' for #ActionView::Base:0x0000000001d060 Did you mean? seller_path):

Upvotes: 0

Views: 220

Answers (1)

max
max

Reputation: 102036

Pass a model instance instead so that your inputs are bound to the model instance. For nested resources pass an array:

<%= form_for([@seller, @seller_profile]) do |form| %>

That ensures that the inputs will contain the values entered by the user when validation fails. And it also hooks into the I18n API for translations so that it will use the translation keys for that model instead of generic keys.

If you're using Rails 5.1+ use form_with(model: [@seller, @seller_prodile], local: true) instead.

Upvotes: 0

Related Questions