Rails - Silmple Form calls a specified action of my user controller - ruby-on-rails

My problem is that when i try to call a specified method from a simple_form_for form it doesn't work.
Here is my code :
<%= simple_form_for #user, :url => {:action => :register_iban}, :html => { :method => :post } do |f| %>
<div class="col-md-8">
<%= f.input :first_name, :label => t('user-show.payment.form.first_name'), placeholder: "Prénom" %>
<%= f.input :last_name, :label => t('user-show.payment.form.last_name'), placeholder: "Nom" %>
<%= f.input :iban, :label => t('user-show.payment.form.iban'), placeholder: "IBAN" %>
<%= f.input :bic, :label => t('user-show.payment.form.bic'), placeholder: "BIC" %>
</div>
<div class="col-md-8 text-center">
<%= f.submit t('user-show.payment.title'), class: 'btn btn-danger' %>
</div>
<% end %>
So, as you can see, i try to call register_iban method from my user controller.
But when i do that, i have an error : No route matches {:action=>"register_iban", :controller=>"users", :id=>"5", :locale=>nil}
Everytime i create a new method in a controller, i have to create a route in the routes.rb file ? Here, i'd like to make this url : /users/5/register_iban (where "5" is the user id) call my method.
Sorry but i start in ruby and i'm pretty stuck :/

in your config/routes.rb try to add in the users resources
resources users do
member do
post :register_iban
end
end

No route matches {:action=>"register_iban", :controller=>"users", :id=>"5", :locale=>nil}
This error means you're trying to access a route which doesn't exist.
The routes are defined at config/routes.rb:
#config/routes.rb
resources :users do
post :register_iban
end
This will allow you to call the register_iban method in the users controller through your form.
You'll also want to make sure you're calling routes with the appropriate helpers:
<%= simple_form_for #user, url: user_register_iban(#user) %>

Related

No route matches [patch] "/happy/node/10003

routes
put '/happy/node/:node_id', to: 'nodes#happy', as: :happy
node controller
def happy
#node = Node.find(params[:node_id])
if #node.update_attributes(:node_status => "happy",
:location_id => params[:location_id],
:hostname => params[:hostname])
redirect_to node_url
end
end
view - form
<%= form_for(#node, url: happy_path(#node), method: :patch, do |f| %>
<%= f.label :location_id, "Location" %>
<%= collection_select :location_id, Location.order(:name), :id, :name, :prompt => "Select Location" %>
<%= f.submit "Save Changes" %>
I am trying to update the node form using a custom action. When i tried this it failed miserably. I will be extremely grateful for your help.
In your routes you are using put, so your method should also be put:
<%= form_for(#node, url: happy_path(#node), method: :put, do |f| %>
You'll need to add a patch route, if you want to continue using PATCH:
patch '/happy/node/:node_id', to: 'nodes#happy', as: :happy

creating a form to route a method inside controller

I am trying to create a form that redirects to a method called accept_item on the form controller. My issue is that it's not going through the accept_item method when I submit the form.
form_controller.rb
def accept
#form = Form.find(params['id'])
end
def accept_item
redirect_to inventories_path
end
accept.html.erb
<%= form_tag accept_item_forms_url do %>
<% #form.items.each do |i| %>
<%= label_tag 'Item Name' %>
<p><%= i.name %></p>
<%= label_tag 'Quantity' %>
<p><%= text_field_tag 'quantity', i.quantity %></p>
<% end %>
<%= submit_tag 'Accept', class: 'btn btn-default btn-about pull-left', data: {confirm: 'Are you sure you want to accept?'} %>
<% end %>
routes.rb
resources :forms do
collection do
get :accept_item, :as => :accept_item
end
end
Error Message
No route matches [POST] "/forms/accept_item"
The form_tag helper uses the HTTP POST method by default. You defined your routes with get:
get :accept_item, :as => :accept_item
You should use post instead:
post :accept_item, :as => :accept_item
Also I don't think you need the as: :accept_item part, unless you're going to use accept_item_url instead of accept_item_forms_url.
just remove :as => :accept_item, and change method to post
post :accept_item
You will get /foo when using :as => 'foo'.

undefined method `contact_forms_path'

I am running into an error with a form I am trying to create:
ActionView::Template::Error (undefined method `contact_forms_path' for #<#<Class
The thing is I never created a contact_forms route, so I do not know why I am getting an undefined method for the contact_forms_path.
My route for the contact form is:
get "/contact_form/new", to: "contact_form#new"
My view for this form is new.html.erb within my contact_form directory
<%= simple_form_for #contact_form do |f| %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email, placeholder: 'example#email.com' %>
<%= f.input :address %>
<%= f.input :city %>
<%= f.input :state %>
<%= f.input :zip_code %>
<%= f.input :phone %>
<%= f.button :submit %>
<% end %>
My model is contact_form.rb and my controller is contact_form_controller.rb.
I could use a little direction. Any help is appreciated. I can pass along more info if needed.
Thanks!
Please try this:
add this to your routes:
post "/contact_form", to: "contact_form#create"
open up the terminal and run:
rake routes|grep contact_form
You should get something like this as a response:
contact_form_new GET /contact_form/new(.:format) contact_form#new
contact_form POST /contact_form(.:format) contact_form#create
This gives you the path for the route. Now you can specify that path in the simple_form_for:
<%= simple_form_for #contact_form, url: contact_form_path, method: :post do |f| %>
form_for assumes a default url for new Foo records as foos_path as explained here...
http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for
You need to specify the path that the contact_form will post to...
post "/contact_forms", to: "contact_forms#create", as: 'contact_forms"
That will be the route that receives the params when you submit the form.
Note that as a shorthand you could just specify in your routes.rb
resources :contact_forms, only: [:new, :create]
(This assumes that you will use the more conventional contact_forms#new ... convention-over-configuration is to use plural for controller names)

Do not post a form in a Rails root route

I have a controller with just one method:
index_controller.rb
def index
# some code
end
The form:
index.html.erb
<%= form_tag :class => "form-inline signup" do %>
<div class="form-group">
<%= text_field_tag :url, nil, :class => "form-control", :placeholder => "URL do tópico" %>
</div>
<%= submit_tag "Enviar", method: :post, :class => 'btn btn-theme' %>
<% end %>
And a simple root route:
root 'index#index'
post '/', to: 'index#index'
The problem is that when I load the root page, the form is posted automatically, when the preferable was to POST just on the button call.
What am I missing here?
You should move the code for the post out to another action that can handle that.
post '/', :to => "index#submit"
Then you can define a submit action within your IndexController to handle the form, and the index action won't run the form code anymore.

rails admin edit view path routes

I've got an admin section setup, but am having trouble getting the "update" route to work.
Getting error when hitting "update" via the edit view:
"No action responded to 2."
For some reason the route is responding to the :id as the :action.
Parameters:
Parameters: {"commit"=>"Update", "action"=>"2", "_method"=>"put", "admin"=>{"ended_at(1i)"=>"2010", "ended_at(2i)"=>"8", "ended_at(3i)"=>"22"}, "id"=>"edit", "controller"=>"admin/subscriptions"}
The edit view uri:
/admin/subscriptions/2/edit
Edit view:
<% form_for :admin, #subscription, :html => {:method => :put} do |f| %>
<p>
<%= f.label :ended_at %><br />
<%= f.date_select :ended_at %>
</p>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>
Route:
map.namespace :admin do |admin|
admin.resources :subscriptions
end
I assume I need to do something differently in the form_for method or maybe the routes, but everything I've tried isn't working.
Thanks for the help!!
It should be this:
<% form_for [:admin, #subscription] do |f| %>
By putting :admin and #subscription in square-brackets, this makes it into an array which is passed as the first argument to form_for. The benefit of this is if #subscription is a pre-existing record (as-in, one found by find, not created with new) then Rails will know to set the form method to PUT.
This works:
<% form_for :admin, #subscription, :html => {:method => :put}, :url => { :action => "update" } do |f| %>
Seems verbose though. Any better ideas?
Try
- form_for :subscription, #subscription do |f|
We're using formtastic here.

Resources