Right now I have a link_to. It looks like a button the the user. It's an upgrade button so it routes the user to the plans page.
I not only want to route the user to the plans page but I want to pre-select the plan that supports the feature they are upgrading for. I was told that I could pass a query parameter into the link_to? Is the true and what does this look like in my situation? Here is my code.
LINK_TO
<%= link_to edit_account_plan_path, class: "button-big reverse-blue" do %>
HTML
<div class="plan-control">
<%= radio_button_tag :plan_id, plan.id, row.current?, disabled: row.ineligible? %>
</div>
you can pass a query parameter back to your controller something like this:
<%= link_to edit_account_plan_path(feature: 'basic'), class: "button-big reverse-blue" do %>
Then in your controller action routed by edit_account_plan_path, you can set an instance variable which you can use to preselect on your view page:
def edit
#plan = Plan.find_by(feature: params[:feature])
end
Then on your view page:
<div class="plan-control">
<%= radio_button_tag :plan_id, 'Basic', #plan.feature == 'basic' %>
</div>
Related
In my app I want to set a search form with two buttons. Each one of those buttons should send request to separate controller. Something like this:
<%= form_tag(products_path, method: :get) do %>
Search Field <%= text_field_tag :q %>
<br>
<%= submit_tag 'First controller' %>
<%= submit_tag 'Second Controller' %>
<% end %>
Is this even possible? Or rails just impose on developer an "one form - one controller" way?
Use JavaScript to change the form URL based on the button clicked.
<%= form_tag(first_controller_path,id: 'search-form', method: :get) do %>
Search Field <%= text_field_tag :q %>
<br>
<button type='submit' id="form-submit-button">First Controller</button>
<button type='button' id="second-controller-button">Second Controller</button>
<% end %>
<script>
$(function(){
$("#second-controller-button").on("click",function(e){
e.preventDefault();
var form = document.getElementById('search-form');
form.action = '<%= second_controller_path %>' ;
form.submit(); // Or you could also try document.getElementById("form-submit-button").click();
})
});
</script>
HTML5 added some attributes to INPUT and BUTTON elements. One of them is formaction so you can set the action triggered by each button independantly and it overrides the default form action.
<%= form_tag(products_path, method: :get) do %>
Search Field <%= text_field_tag :q %>
<br>
<%= submit_tag 'First controller' #triggers the default action %>
<%= submit_tag 'Second Controller', formaction: another_path %>
<% end %>
https://www.w3schools.com/tags/att_input_formaction.asp
You already defined form_tag with products_path through which the controller method is already defined.
So answering your question, you can't pass send requests to two different controllers with one form.
If you want to pass some status with buttons try adding some attributes to the buttons and differentiate them inside the controller.
I want to make erb template of searchbar that will call search method of its controller. Different controllers got same method, but will have different logic. Is it possible to make form that will work with every controller just by rendering it on their views?
--- UPDATE ---
I have found that kind of solution:
<%= form_tag "#{controller_name}/search", method: 'get' do %>
<div class="input-group">
<%= text_field_tag :sub_string, '', class: 'form-control', placeholder: "Search..." %>
<span class="input-group-btn">
<%= submit_tag 'Search', class: %w(btn btn-primary) %>
</span>
</div>
<% end %>
However when I hit the submit button, it goes to search action of my current controller and it thows an error:
Couldn't find Basis with 'id'=search
it tries to call set_basis private method, even if it is not called by before_action?
Set search form's controllers name with params[:controller].
Use Rails Guide to see how to pass controller and action for a form_tag or form_for.
I think you can
create the template/partial under views/shared folder
then in controller do something like
def search
render :partial => "shared/search"
end
Ok Then add this
in your partial so you can dynamically change your controller for the form
form_tag({controller: controller.controller_name, action: "search_action"}, method: "post", class: "form")
i have a form in my index-view where i create multiple checkboxes. One checkbox for every entry. This looks like this:
index.html.erb
<%= form_for :user, url: usersupdate_path() do |f| %>
<%= render #users %>
<%= f.submit 'test', :class => 'btn btn-primary' %>
<% end %>
_user.html.erb
<%= check_box_tag "checked[#{user.id}]","#{user.id}",true %>
Description:
With the form i want to allow the admin to uncheck users - this users i want to send to the controller and update their attributes.
There are only 2 problems:
1) I have to refresh the site until i can send the form to the controller - i don't know why
2) When i print the array it looks like this:
{"1"=>"1", "2"=>"2", "4"=>"4"}
User 3 was unchecked by me.
What i want is something like this:
{"1"=>"true", "2"=>"true", "3"=>"false", "4"=>"true"}
But how can i send the checked value of the checkbox to the controller?
In my controller i do only this at the moment:
def update
flash[:success] = params[:checked]
redirect_to root_path
end
Thanks
The browser does not serialize an unchecked checkbox when sending form data, so if it is not checked, it never gets sent.
You can generally fix this two ways. Make your action smart enough to see "missing" values as "unchecked", or add a hidden field before each checkbox:
<%= hidden_field_tag "checked[#{user.id}]", "false" %>
<%= check_box_tag "checked[#{user.id}]","#{user.id}", true %>
As for the true-values, the second parameter to check_box_tag is the value you want the checkbox to have, so you can change it to this:
<%= hidden_field_tag "checked[#{user.id}]", "false" %>
<%= check_box_tag "checked[#{user.id}]","true", true %>
And it should do what you want.
Note that if you use FormBuilders they handle this nuance for you.
I need to include an external search form in my main layout view, such that it is displayed with every page render. At the moment, in my layouts/main.html.erb I have:
<div style="float: right";>
<% form_tag url_for("http://search.example.co.uk/search"), {:method => :get} do %>
<%= text_field_tag(:q) %>
<%= select_tag(:category, '<option value="all">All</option> <option value="dogs">Dogs</option> <option value="cats">Cats</option>') %>
<%= hidden_field_tag("site", "Example") %>
<%= hidden_field_tag("btnG", "Search") %>
<%= hidden_field_tag("filter", "0") %>
<%= hidden_field_tag("proxystylesheet", "std_stylesheet") %>
<%= submit_tag "Search" %>
<% end %>
</div>
This currently works, and in the header of all pages rendering the main layout, I have a text field, a drop down and a Search button. On clicking the 'Search' button, the page is directed to http://search.example.co.uk/search with all the parameters.
I need to be able to modify the parameters before the page is redirected. I need to concatenate params[:q] and params[:category] and pass this with the redirection. Is this possible?
Thanks for any help/suggestions.
You should do this by submitting your form to an action which would concatenate all your results and they would in-turn redirect your request to the URL which you have specified along with the parameters that you have processed...
Trying to create a select menu with items from a collection, so that upon selection of an item, and hitting submit, the user is taken to the "Show" action for that item...What I have is something like this:
<% form_tag("subjects/#{#subject.id}/state/:id", :method=>:get) do %>
<%= select_tag('state', options_from_collection_for_select(State.states, 'id', 'name'))%>
<%= submit_tag "go!" %>
<% end %>
What'd I like is for what's selected in the menu to fill in the :id parameter...(this is rails 2.3)
You can send the form to an action that redirect where you want:
<% form_tag("some_controller/redirection", :method=>:get) do %>
<%= select_tag('id', options_from_collection_for_select(State.states, 'id', 'name'))%>
<%= hidden_field_tag :subject_id, #subject.id %>
<%= submit_tag "go!" %>
<% end %>
in SomeController
def redirection
redirect to "subjects/#{#{params[:subject_id]}}/state/#{params[:id]}"
end
The structure of the URL that you are requesting can't be made using only an HTML form. It will require some Javascript:
$('#my_form').submit(function(){
window.location = '/subjects/' + $('#subject_id').val() + '/state/' + $('#state_id').val();
});