overallduka
overallduka

Reputation: 1549

Error no routes matches whit the controller and action corrects

I want to make a simple submit, ideally with no routes, to insert a sample into a database, but I am having some trouble.

This is the index of finance:

index.html.erb(finance path)

<%= form_for(@place,:url => new_place_finance_path,:method => :put) do |f| %>

  <%= f.text_field :place %>
  <%= f.submit %>
<% end %>

If possible I don't want create a member, how do I make in routes:

resources :finances do
  member do
    put :new_place
  end
end

and my controller of finances:

def index
  @finances = Finance.all
  respond_with @finances
  @place = Place.new
end

and action new_place:

def new_place
  @place = Place.create(params[:place])
  @place.save
  if @place.save
    redirect_to finances_path,:notice => 'Lugar criado com sucesso.'
  else
    redirect_to finances_path,:notice => 'Falha ao criar lugar.'
  end 
end

I'm getting this error:

No route matches {:action=>"new_place", :controller=>"finances", :id=>nil}

When I execute rake routes:

new_place_finance PUT    /finances/:id/new_place(.:format) finances#new_place

Upvotes: 1

Views: 69

Answers (2)

jvnill
jvnill

Reputation: 29599

new_place_finance_path needs a finance object in order to be evaluated correctly. In the routes you pasted above

 new_place_finance PUT    /finances/:id/new_place(.:format) finances#new_place

so new_place_finance_path(@finance) will replace :id by @finance.id (or to_param if you created that method in the model)

Upvotes: 0

Mark Swardstrom
Mark Swardstrom

Reputation: 18100

You have some unusual things in here. If you want to create a new place, you would usually do this in routes. If finance can have many places, then it would be like so...

resources :finances do
  resources :places
end

This will create a method called new_finance_place(@finance) that will take you to the new form. And it will create another url to the /finances/:finance_id/posts that will expect a POST - it will call the create method in the PostsController.

From the new form, you will be able to write:

<%= form_for [@finance, @place] do |f| %>

Upvotes: 1

Related Questions