form_for does not append the action - ruby-on-rails

I have this html.erb rails code :
<%= form_for(:dash_action, method: :post, url: {controller: 'brokers', action: 'dashboard'}) do |f| %>
<%= f.submit("Suchen", class: "btn btn-primary", id: "change_status") %>
<% end %>
When I check the generated HTML code, I see this for the form element :
<form action="/brokers" accept-charset="UTF-8" method="post">
But I expect to see this as action: action="/brokers/dashboard"
Whats going wrong ?
......................................................
Here all brokers relates routes:
get 'brokers/login_page'
get 'brokers', to: 'brokers#dashboard'
post 'brokers/dashboard', to: 'brokers#dashboard'
post 'brokers/eval_login', to: 'brokers#eval_login'
get 'brokers/logout'
get 'brokers/edit_order_now'
Also when I do a url_for(controller: 'brokers', action: 'dashboard')
I got only :
/brokers

You can use absolute route on the form_for tag, your route should be like this dashboard_brokers_path
<%= form_for(:dash_action, url: dashboard_brokers_path, method: :post) do |f| %>
<%= f.submit("Suchen", class: "btn btn-primary", id: "change_status") %>
<% end %>

try below code:
<%= form_for(:dash_action, url: "/brokers/dashboard") do |f| %>
<%= f.submit("Suchen", class: "btn btn-primary", id: "change_status") %>
<% end %>

Related

form_with producing wrong controller (only on some routes)?

This is in application.html.erb, and it works on 95% of the pages in my app:
<% ['usd', 'eur', 'aud'].each do |currency| %>
<%= form_with url: {controller: :home, action: :currency_select}, method: :post do |currency_form| %>
<%= currency_form.hidden_field :preferred_display_currency, value: currency %>
<%= currency_form.submit currency, class: "btn-info shadow-none" %>
<% end %>
<% end %>
But when I visit a certain view, before the page even loads, it gives this error:
ActionView::Template::Error (No route matches {:action=>"currency_select", :controller=>"users/home"}):
191:
192: <%= form_with url: {controller: :home, action: :currency_select}, method: :post do |currency_form| %>
193: <%= currency_form.hidden_field :preferred_display_currency, value: currency %>
194: <%= currency_form.submit currency, class: "btn-info shadow-none" %>
195: <% end %>
196:
197: <% end %>
I'm pretty sure something to do with :controller=>"users/home" (where it should simply be :controller=>"home")
Why is the form suddenly confused about the controller?
Solution 1
Run
rails routes | grep currency
it returns
currency_select POST /currency_select(.:format)
home#currency_select
Now just use url: currency_select_path like so:
<% ['usd', 'eur', 'aud'].each do |currency| %>
<%= form_with url: currency_select_path, method: :post do |currency_form| %>
<%= currency_form.hidden_field :preferred_display_currency, value: currency %>
<%= currency_form.submit currency, class: "btn-info shadow-none" %>
<% end %>
<% end %>
Solution 2
Replace :home with "/home":
<% ['usd', 'eur', 'aud'].each do |currency| %>
<%= form_with url: {controller: "/home", action: :currency_select}, method: :post do |currency_form| %>
<%= currency_form.hidden_field :preferred_display_currency, value: currency %>
<%= currency_form.submit currency, class: "btn-info shadow-none" %>
<% end %>
<% end %>
(not totally sure why that works, but can confirm that it indeed works!)
A work around
The above two solutions are best, but a work around for the problem could be:
<% if !current_page?(edit_user_registration_path) %>
# all existing code
<% end %>
This way it simply avoids displaying the form on the route that errors. It's not ideal, but a practical work around.

Does Rails support link_to with method post?

I have a rails link_to that I want to run a post with...
<%= link_to 'Unfollow', follows_path(user_id: u.id), method: :delete, class: 'text-right btn btn-primary' %>
The route for it:
resource :follows, only: %i[create destroy]
However, when I click it runs as a get:
Started GET "/follows?user_id=1" for ::1 at 2020-05-27 00:51:00 +0100
ActionController::RoutingError (No route matches [GET] "/follows"):
I have checked multiple questions on SO on using link_toas a post method to no avail. I believe it is worth mentioning that the HTML rendered as:
<a class="text-right btn btn-primary" rel="nofollow" data-method="delete" href="/follows?user_id=8">Unfollow</a>
However, I eventually resigned to using a small form like so:
<% if !(current_user.is_following?(#user)) %>
<% #follow = Follow.new %>
<%= form_for (#follow) do |f| %>
<%= hidden_field_tag :user_id, #user.id %>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>
<% else %>
<% #follow = Follow.new %>
<%= form_for (#follow), method: :delete do |f| %>
<%= hidden_field_tag :user_id, #user.id %>
<%= f.submit "Unfollow", class: "btn btn-primary" %>
<% end %>
<% end %>
Is it no longer possible to use post methods with link_to or has the syntax changed?
put follow_path with singular not follows
<%= link_to 'Unfollow', follow_path(user_id: u.id), method: :delete, class: 'text-right btn btn-primary' %>

Rails 4 Bootstrap Search Form in Navbar

I have some rails-bootstrap-form code in a header partial that gets rendered in the Rails 4 application layout:
<div class="navbar-form navbar-right">
<%= bootstrap_form_tag controller: 'devices', action: 'index', method: 'get' do |f| %>
<%= f.search_field :search, hide_label: true, placeholder: 'Search' %>
<%= f.submit "Submit", :name => nil %>
<% end %>
</div>
I want this search form to always perform a GET request on the devices controller's index action. The search doesn't actually link to the devices index url, it appends the search parameter to the current url like this: /devices/1?search=foo. Since it just appends to the current URL, the search works fine from /devices.
I thought that if I pass in controller: 'devices', action: 'index', method: 'get' to bootstrap_form_tag it would force the URL to rewrite, but I'm not having any luck.
I ended up stripping out bootstrap_form_tag and I went with a standard form_tag. I had to add the bootstrap form classes in manually.
<%= form_tag(devices_path, method: 'get', class: 'navbar-form navbar-right') do %>
<%= search_field_tag 'search', nil, class: 'form-control', placeholder: 'Search' %>
<%= submit_tag "Submit", class: 'btn btn-default', :name => nil %>
<% end %>
I thought the bootstrap-forms gem would save time, but it definitely didn't for the search.

form_tag with same params as current rails

How do I create a form_tag with the same current params but an additional param?
<%= form_tag(params.merge( :controller => "integrations", :action => "show" ), method: :get) do %>
<%= text_field_tag :params, params[:q] %>
<%= submit_tag "Search", class: "btn btn-default"%>
<% end %>
where my url looks like:
http://localhost:3000/integrations/64?utf8=%E2%9C%93&q%5Bbio_or_topics_or_title_or_company_cont_any%5D=mcmaster&q%5Btitle_cont%5D=&q%5Bcompany_cont%5D=&q%5Blocation_cont_any%5D=&sort_by=twitter_followers&q%5Bfacebook_username_not_eq%5D=&q%5Btwitter_username_not_eq%5D=&q%5Blinkedin_username_not_eq%5D=&q%5Blocation_not_eq%5D=&commit=Search
Thanks!

Rails routes on query string

I'm new to Ruby on rails. Now I have a search form in my homepage. When I enter something (like abc) in the form and submit, I would like the page to call my action info in my controller search. How should I configure the routes?
The current url is ~/info/?utf8=%E2%9C%93&location=berkeley&commit=submit
<div id='search_form'>
<%= form_tag('info/',method: "get") do %>
<%= text_field_tag('location', #location, :size => 30) %>
<%= submit_tag "submit", class: "btn btn-large btn-primary" %>
<% end %>
</div>
Add the following to your routes file:
get '/info', to: "search#info", as: 'search_info'
This gives you search_info_path and you can use it in your form_for declaration as follows:
<%= form_tag(search_info_path, method: "get") do %>
<%= form_tag some_path, method: "get" do %>
<%= text_field_tag('location', #location, :size => 30) %>
<%= submit_tag "submit", class: "btn btn-large btn-primary" %>
<% end %>
You can use path helper in you form_tag that leads to your controller.
i think this is wrong ('info/',method: "get")
run rake routes for look helper`s

Resources