rails 4 form_for custom action - ruby-on-rails

I am using <%= form_for #invoice, url: {action: :pay} do |f| %> but the form is sent to the update action instead of the "pay" action I defined in the controller. What am I missing?

You have param :id in your route, and you should assign a param to id in your form action, try this
<%= form_for #invoice, url: {action: "pay", params: {id: #invoice.id}} %>
Or you can use a path
<%= form_for #invoice, url: for_pay_path(#invoice) %>
for_pay_path change to path of pay action, you can see on ouput of rake routes

Related

rails `form_with` `redirect_to` 303 `:see_other` makes the request does not redirect

I have a Rails form on a "show" view that looks something like:
<%= form_with(url: "/relative-path-to-index-view", method: "post") do |f| %>
<%= f.submit "Submit" %>
<% end %>
The create action of the controller responsible for handling the form submit looks like this:
def create
redirect_to action: "index", status: :see_other
end
When I submit the form, I see in the network tab that it made the POST request, received a 303 :see_other response, and then made a GET request to the index action. However, the end-user doesn't see any redirect in the browser. Any ideas on why?
✅ This was fixed by simply appending local: true to the form_with parameters.
- <%= form_with(url: "/relative-path-to-index-view", method: "post") do |f| %>
+ <%= form_with(url: "/relative-path-to-index-view", method: "post", local: true) do |f| %>
https://guides.rubyonrails.org/form_helpers.html

How to make create and update use the same route

I have a route and form that looks like this:
<%= form_for #address, url: {action: "update_contact", controller: "checkouts"}, html: {class: ""} do |f| %>
My route looks like:
post "checkouts/:cart_token/update_contact" => "checkouts#update_contact", as: "checkouts_update_contact"
For updates the form is looking for a PATCH which I haven't defined, and so I get an error when the #address model already exists i.e. updates
How can I make my form always POST no matter what?
Add method: :post
<%= form_for #address,
url: {action: "update_contact", controller: "checkouts"},
html: {class: ""},
method: :post do |f| %>
Without that Rails adds a hidden field which is used to fake a PATCH request when the form is used to update an object.
<input type="hidden" name="_method" value="patch" />

Rails create a model from form that isn't stored in a database

I have an account model that i want to create from the registration controller and form.
index.html.erb
<div id="registration">
<%= form_for(#account) do |f| %>
<% if #account.errors.any? %>
register_controller.rb
class RegisterController < ApplicationController
def index
#account = Account.new
end
def create
#account = Account.new(parames[:account])
end
end
end
end
routes.rb
Rails.application.routes.draw do
get 'register/index'
get 'register/create'
My current issue is undefined method `accounts_path' for #<#:0x007fd9f2fd8468> from the form_for() method
Am I mixing things up because of the names of the classes?
form_for(#account) will auto-set some of the attributes of the form, such as "action", which is where the form submits to. If you want to change this to something else then use the url option. You can pass this a path helper or just put the url in. eg
<!-- if you have a named path you can use the helper for it -->
<%= form_for #article, url: create_register_path %>
or
<!-- alternatively just pass the url -->
<%= form_for #article, url: "/register/create" %>
or
<!-- or you can pass the url as a hash if you prefer -->
<%= form_for #article, url: {controller: "register", action: "create"} %>
http://guides.rubyonrails.org/form_helpers.html
EDIT: I just noticed in your edit to your post that "register/create" is set as a get route. This means you will need to tell your form to use the get method as well: it defaults to post.
<%= form_for #article, url: "/register/create", :method => :get %>

How to add id tag in form_for in ruby on rails

I'm trying to add an id tag to a form_for tag is below:
<%= form_for(:session, url: sessions_path) do |f| %>
And is it possible for me to add an id to the form_for embedded ruby, or do I have to create a form_tag field and add the id there? If I have to create a form_tag field, how do I add the id and create the form_tag field properly?
Kindly suggest me, waiting for your reply.
Thanks.
Something like this:
<%= form_for(:session, url: sessions_path,:html => {:id => "your id"}) do |f| %>
You can do with form_tag also
<%= form_tag(:session, url:sessions_path, id:'your id') do |f| %>

Using a partial from another view while that model's routes are nested in the current view Rails 3

I've got two models/views, User and PersonalInfo and I'm trying to call the _form.html.erb partial in the 'user#show' action, but I'm getting an error:
No route matches {:controller=>"personal_info"}
I suspect the issue is that my PersonalInfo routes are nested within the User routes and Rails isn't using the right one, but I don't know how to make it use the right one. Here's the line that's calling the form:
app/views/users/show.html.erb:
<%= render :partial => "/personal_info/form", :locals => {personal_info: #personal_info } %>
app/views/personal_info/_form.html.erb:
<%= form_for #personal_info, url: user_personal_info_index_path, html: { method: :post } do |f| %>
routes.rb:
resources :users do
resources :personal_info
end
Do I either have to declare something in the personal_info controller or specify the app to use the users/:user_id/personal_info route?
I was able to get this resolved by changing:
<%= form_for #personal_info, url: user_personal_info_index_path, html: { method: :post } do |f| %>
to:
<%= form_for #personal_info, url: new_user_personal_info_index_path, html: { method: :post } do |f| %>

Resources