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
Related
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' %>
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 %>
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.
I want to keep my params afterr i do submit.
In my Rails 3.2.0 application i have something like that:
http://0.0.0.0:3000/journals?class_code=11v&subject_name=Math
And i have form:
<%= form_for #lesson, :html => {:class => "form-horizontal"} do |f| %>
<%= field_set_tag do %>
....
<%= f.submit "Create", :class => "btn btn-large btn-success" %>
<% end %>
<% end %>
I want to keep my params (class_code and subject_name) after f.submit. How can i do that?
To store all parameters in one field, you could use:
<%= hidden_field_tag :parameters, request.query_string %>
And then you can access them in controller, using:
parameters = parse_nested_query(params[:parameters])
hidden fields
....
<%= hidden_field_tag :class_code, params[:class_code] %>
<%= hidden_field_tag :subject_name, params[:subject_code] %>
<%= f.submit "Create", :class => "btn btn-large btn-success" %>
<% end %>
but - if those are attributes of your model, then assign them in the new action of the controller
def new
#lesson = Lesson.new(:class_code => params[:class_code], :subject_code => params[:subject_code])
end
# in this case the view code is slightly different
<%= f.hidden_field :class_code %>
<%= f.hidden_field :subject_code %>
I've got the following code for a search form, but how would I add an ID or a class to the submit button?
<% form_tag '/wine/search/', :method => 'get' do %>
<%= label_tag "Search" %>
<%= text_field_tag :search_string, params[:search_string] %>
<%= submit_tag "Go" %>
<% end %>
Thanks
submit_tag "Go", :class => "some_class"
submit_tag "Go", :id=> "some_id"
From the Rails API FormTagHelper