form_for with validation does not called create action correctly
Remove the line match ':controller(/:action(/:id(.:format)))', :via => :all
from your routes.rb
file and never use it anymore. This pattern matches any route and it is on the first position, other routes in your file don't have a chance at all because of that.
Using redirect_to to :create action
You can't post on a redirect. This is part of the HTTP spec - a redirect issues a http header.
How is the 'new' action redirected to 'create' in Rails?
OOP
The bottom line answer to your question is that Rails is object orientated (by virtue of being built on top of Ruby). This is very important, as it means everything inside Rails should be based around objects:
This leads me to the routes - the resourceful
nature of Rails' routes is down to the same idea, that you need to work with objects in your application - hence the resources
directive providing 7 key actions to manipulate those objects
To fully understand Rails, you really need to look into how it works with objects, specifically how they interact with each other
Redirect
To answer your question regarding the redirect
, the simple answer is that Rails doesn't "redirect" to any action specifically
Remember, Rails is stateless - it does not persist data through requests - it only has the data which you either initialize at the time, or have sent it
What you're confused about is how some of the Rails actions seem to be sending your requests to the appropriate "request" action. The answer to this lies in the helpers / methods you use, specifically form_for
form_for
form_for builds forms from their ActiveRecord objects.
Therefore, if you perform the following:
#app/controllers/your_controller.rb
Class YourController < ActiveRecord::Base
def new
@model = Model.new
end
end
This will give Rails the knowledge that it's loading a new
object, and therefore will use the form_for
method to send the request to the create action
If you used form_tag
, you would not get a redirect to the create
action -- that's the magic of Rails -- it's been built to accommodate objects
Rails 4 avoiding repeated POST request, slow redirect_to
You should disable the submit button using JavaScript upon form submission:
$('form').on('submit', function () {
$(this).find('input:submit').prop('disabled', true);
});
Fiddle
How to set the action with form_for?
This is from memory, but I think you can do something like this:
form_for :user, @user, :url => { :action => :prompt_user } do |f|
Rails, redirect from NEW to CREATE
def new
#assuming boats is an array
if boats.size > 1
redirect_to boats_path(:user => params[:user], :boat => params[:boat]), :method => :post
else
#new stuff
end
end
boats_path
or whatever object you are trying to create.
Rails calls unexpected controller action instead of redirecting
Well, it seems to have been an issue with the name of the action :location
When I changed def location
to def map
, everything works fine now.
I hope that this helps somebody at some point!
Rails action not found for controller when saving web forms
You should be redirecting to :list
instead of list
, or better
redirect_to(action: 'list')
Rails 3.2.8 form_for submit button is using the 'new' action instead of 'create' action
Remove the url option in the form helper and try again
To be sure its not from you,
Run a scaffold generator for your post resource
rails g scaffold post is_link:boolean url description:text
you can use twitter bootstrap themed generator option to regenerate your bootstrap style if you want
Related Topics
How to Display a Link to Individual Microposts? (Ruby on Rails 3)
Tilt (Kramdown) Preventing Erb Processing When Rendering Markdown
Make a Ruby Script with Text Io Double Clickable Executable File
Rexml::Document.New How to Give Encode Parameters on This Line
API Post with Array Using Http Gem (Or Restclient)
Sum of Arrays of Different Size
How to Add to an Existing Hash in Ruby
Using a Duration Field in a Rails Model
Rspec - How to Test Activerecord::Recordnotfound
How to Create Nested Model from Partial Rails 6
Ruby, No Implicit Conversion of Symbol into Integer
Newbie: Text Replacement in My Case