When I click the submit button it does filter the selections according to the selected brand but the dropdown option reset to the first option
`
**<%= form_tag(products_path, :method => "get",
class: 'navbar-form navbar-left') do %>
<div class="input-group">
<%= search_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %>
<div class="input-group-btn">
<%= button_tag "search", :class => 'btn btn-info glyphicon glyphicon-search',:name => nil%>
</div>
<div class="field">
<%= search_field_tag :min, params[:min], placeholder: "min", class: "form-control" %>
</div>
<div class="field">
<%= search_field_tag :max, params[:max], placeholder: "max", class: "form-control" %>
</div>
<% brands_array = Brand.all.map { |brand| [brand.name, brand.id] } %>
<%= select_tag :brand_id, options_for_select(brands_array) %>
</div>
<% end %>**
`
Related
This is my select_tag:
<div class="card-body">
<%= form_tag services_path, :method=> "get", :id => "filter_form" do %>
<div class="form-group">
<%= select_tag :filter, options_for_select([[t('italy'), "it"], [t('world_wide'), "en"]], selected: params[:filter] ? params[:filter] : I18n.locale), class: "form-control" %>
</div>
<div class="form-group">
<%= submit_tag t('research'), name: nil, class: "btn btn-default bg-primary text-light" %>
</div>
<% end %>
</div>
and these are the buttons:
<%= link_to(t('impact_hub'),
external_service_goto_path("impacthub-coworking"),
class: "btn btn-light-primary font-weight-bold m-1",
target: "_blank") %>
<%= link_to(t('my_spot'),
external_service_goto_path("myspot-coworking"),
class: "btn btn-light-primary font-weight-bold m-1",
target: "_blank") %>
I don't know how to show buttons when I select "italy" in select and not show them when I select world_wide
Rails 6.0.3
I am using a form to schedule meetings.
It is being used for both single time meetings that span over several days (with start_time and end_time) and tasks that last only one day.
For singular day meetings, I need the end_time to be set to the same value as start_time, without user selecting it.
I have tried many things in the controller, but can't work it out.
How would you go about this.
Thanks for the help
<div class="border border-grey-light rounded" style ="padding: 10px;">
<%= form_with(model: meeting, local: true, :html => {:id => "flatpickr-form-single"} ) do |form| %>
<div class="mb-6">
<%= form.label :name, class: 'label' %>
<%= form.text_field :name, required: true, class: 'form-control', placeholder: "Task name" %>
</div>
<div class="mb-6">
<%= form.label :body %>
<%= form.rich_text_area :body, class: 'form-control' %>
</div>
<div class="start_time_result mb-6" style ="width: 30vw;">
<%= form.label :start_time, class: 'label' %>
<div class="flex items-center justify-between max-w-md">
<%= form.text_field :start_time, data: { behavior: "flatpickr" }, placeholder: "Date and time select ...", class: "form-control" %>
</div>
</div>
<div class=" field" style ="width: 30vw;">
<%= form.label :end_time, class: 'label' %>
<div class="end_time_result flex items-center justify-between max-w-md" >
<%= form.text_field :end_time, data: { behavior: "flatpickr" }, placeholder: "Date and time select ...", class: "form-control required" %>
</div>
</div>
<%= form.submit class: "btn btn-primary text-base py-1.5 px-5", value: "Confirm" %>
<% end %>
</div>
<script>
const selectElement = document.querySelector('.start_time_result');
selectElement.addEventListener('change', (event) => {
const end_time_result = document.querySelector('.end_time_result');
end_time_result.textContent = `${event.target.value}`;
});
</script>
You can add a before_action in the controller and set the end_time parameter value to the same as the start_time from there.
i have 2 search fields in 2 different views. they both search for establishments and are identical except for styling.
one is in welcome/index.html.erb and works great.
the other is in a partial called _nav.html.erb and is called from application.html.erb and does not work at all.
do i need to pass something into the _nav partial for it to process the search form properly?
welcome/index.html.erb
<div>
<%= form_tag(establishments_path, method: "get") do %>
<div class="form-group">
<%= text_field_tag :search, params[:search],
placeholder: "Enter the name of an eatery", class: "form-control" %>
<div class="actions"><%= submit_tag "Search", :name => nil, class: "btn btn-primary" %></div>
</div>
<% end %>
</div>
_nav.html.erb
<form class="navbar-form navbar-left" role="search">
<%= form_tag(establishments_path, method: "get") do %>
<div class="form-group">
<%= text_field_tag :search, params[:search],
placeholder: "Search", class: "form-control" %></div>
<%= submit_tag "Search", :name => nil, class: "btn btn-primary" %>
<% end %>
</form>
application.html.erb
<%= render 'layouts/nav' %>
EstablishmentsController/index
def index
if params[:search]
#establishments = Establishment.search(params[:search].downcase).order("created_at ASC")
else
#establishments = Establishment.all.limit(25)
end
end
Your form_tag is already wrapped in a form:
<form class="navbar-form navbar-left" role="search">
#^^^^ Here
<%= form_tag(establishments_path, method: "get") do %>
#^^^^ and here again
<div class="form-group">
<%= text_field_tag :search, params[:search],
placeholder: "Search", class: "form-control" %></div>
<%= submit_tag "Search", :name => nil, class: "btn btn-primary" %>
<% end %>
</form>
When you click on the submit tag, it submits the wrong form (the first one).
You should use it like this:
<%= form_tag(establishments_path, method: :get, class: 'navbar-form navbar-left', role: 'search') do %>
<div class="form-group">
<%= text_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %></div>
<%= submit_tag "Search", :name => nil, class: "btn btn-primary" %>
<% end %>
In my form the user has to select a category for his post.
<div class="field form-group">
<%= f.text_field :category, class: "form-control" %>
</div>
(Currently I am using text-input instead of a drop-down list)
Posts belong to categories
#post = #category.posts.build(post_params)
However I can't understand how to get the category value out of that field.
I have tried passing a number, to find_by id, and string to find_by name.
#category = Category.find(params[:category]) #returns no Categoy with nil id
#category = Category.find_by(name: params[:category]) #returns no method error
any help would be appreciated
Edit:
Form code
<%= form_for [#company, #post], :html => { :class => "form-posts"} do |f| %>
<div class="field form-group">
<div class="input-group input-group-lg">
<span class="input-group-addon">$</span>
<%= f.text_field :text, class: "form-control", placeholder: "text", required: true %>
</div>
</div>
<div class="field form-group">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-calendar"></i>
</span>
<%= f.text_field :date,
class: "form-control",
value: #today,
data: {behaviour: "datepicker"},
required: true %>
</div>
</div>
<div class="field form-group">
<%= f.text_field :comment, class: "form-control", placeholder: "Comment (optional)" %>
</div>
<div class="field form-group">
<%= f.text_field :category, class: "form-control" %>
</div>
<div class="actions"><%= f.submit 'Add', class: "btn btn-lg btn-primary" %></div>
<% end %>
Edit 2
controller:
form:
<%= f.text_field :category, class: "form-control" %>
def create
#company = current_user.companies.find(params[:company_id])
#category = #company.categories(params[:category])
#post = #category.posts.build(post_params)
debugger:
{"utf8"=>"✓", "authenticity_token"=>"...", "transaction"=>{"text"=>"lalala", "date"=>"11.11.2013", "comment"=>"", "category"=>"5"}, "commit"=>"Add post", "action"=>"create", "controller"=>"posts", "company_id"=>"2"}
undefined method `transactions'
Take a look at the development log.
Probably it will be something like params[:post][:category]
Get your category like this:
params[:post][:category]
I want to have all the errors appear on top of their respective areas.
I ran an if statement with any? on the first one but I know I am repeating myself and have to do it the Rails way.
Any help?
<%= form_for #movie, :html => {:class => "form-horizontal"} do |f| %>
<div class="control-group">
<% if #movie.errors[:title].any? %>
<div class="alert alert-error"><%= #movie.errors[:title].to_sentence %></div>
<% end %>
<%= f.label :title, :class => "control-label" %>
<div class="controls">
<%= f.text_field :title %>
</div>
</div>
<div class="control-group">
<div class="alert alert-error"><%= #movie.errors[:description].to_sentence %></div>
<%= f.label :description,:class => "control-label" %>
<div class="controls">
<%= f.text_area :description, :class => "span8", :rows => "10" %>
</div>
</div>
<div class="control-group">
<div class="alert alert-error"><%= #movie.errors[:rating].to_sentence %></div>
<%= f.label :rating, :class => "control-label" %>
<div class="controls">
<%= f.select :rating, Movie::RATINGS, prompt: "Pick one" %>
</div>
</div>
<div class="control-group">
<div class="alert alert-error"><%= #movie.errors[:total_gross].to_sentence %></div>
<%= f.label :total_gross, :class => "control-label" %>
<div class="controls">
<%= f.number_field :total_gross %>
</div>
</div>
<div class="control-group">
<div class="alert alert-error"><%= #movie.errors[:released_on].to_sentence %></div>
<%= f.label :released_on, :class => "control-label" %>
<div class="controls">
<%= f.date_select :released_on, :order => [:month, :day, :year], :prompt => { :month => 'Select month',:day => 'Select day', :year => 'Select year' }, :start_year => 1950 %>
</div>
</div>
<div class="control-group" >
<%= f.label :image_file_name, :class => "control-label" %>
<div class="controls">
<%= f.text_field :image_file_name %>
</div>
</div>
<div class="form-actions">
<%= f.submit :class => "btn btn-success btn-large" %> <%= link_to "Cancel", root_path, class: "btn btn-danger btn-large" %>
</div>
<% end %>
Here's a solution that will be more DRY and won't necessitate gems like simple_form.
Create a partial for your control group and replace the field name and form field helper with variables:
# File: _control_group.html.erb
<% show_errors = true if show_errors.nil? %>
<div class="control-group">
<% if #movie.errors[field_name].present? && show_errors %>
<div class="alert alert-error">
<%= #movie.errors[field_name].to_sentence %>
</div>
<% end %>
<%= label_tag field_name, :class => "control-label" %>
<div class="controls">
field_helper
</div>
</div>
Then replace these items in your form with a partial render and pass the appropriate code in through the parameters:
<%= form_for #movie, :html => {:class => "form-horizontal"} do |f| %>
<%= render "control_group", field_name: :title, field_helper: f.text_field(:title) %>
<%= render "control_group", field_name: :description, field_helper: f.text_area(:description, :class => "span8", :rows => "10") %>
<%= render "control_group", field_name: :rating, field_helper: f.select(:rating, Movie::RATINGS, prompt: "Pick one") %>
<%= render "control_group", field_name: :total_gross, field_helper: f.number_field(:total_gross) %>
<%= render "control_group", field_name: :released_on, field_helper: f.date_select(:released_on, :order => [:month, :day, :year], :prompt => { :month => 'Select month',:day => 'Select day', :year => 'Select year' }, :start_year => 1950) %>
<%= render "control_group", field_name: :image_file_name, field_helper: f.text_field(:image_file_name), show_errors: false %>
<div class="form-actions">
<%= f.submit :class => "btn btn-success btn-large" %> <%= link_to "Cancel", root_path, class: "btn btn-danger btn-large" %>
</div>
<% end %>
Ran into a similar problem.
Using simple_form worked for me. See an example here