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
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…
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: