i have this form :
<h1> Global data management </h1> </br> </br>
<h2>Enter the conditions and click "find" option to search for users based on conditions. </br> Or click the "List" option to list all the data. </h2>
</br></br></br>
<%= form_for(:find_field, url: find_field_path , method: :get) do |f| %>
<div class="row">
<div class="span8 offset1">
<div class = "row">
<div class = "span4">
<div class="field">
<%= f.label :data_type_choice, "Data type" %>
<%= f.select :data_type_choice, [["all","all"],["2D","2D"],["3D","3D"],["2D3C","2D3C"],["3D3C","3D3C"]] , :name => nil%>
</div>
</br></br>
<h3> COMPLETED WITHIN </h3>:</br>
<div class="field">
<%= f.label :from_date_choice , "From date " %>
<%= f.date_select :from_date_choice , :name => nil%>
</div>
</div>
<div class = "span4">
<div class="field">
<%= f.label :basin_choice, "Basin" %>
<%= f.select :basin_choice, [["all","all"],["Cauvery","Cauvery"],["KG-PG","KG-PG"],["others","others"]] , :name => nil %>
</div>
</br></br>
<h3> </h3>
<div class="field">
<%= f.label :to_date_choice , "To date " %>
<%= f.date_select :to_date_choice , :name => nil %>
</div>
</div>
</div>
</br></br>
<%= f.submit " Search ", class: "btn btn-large btn-primary" %>
</div>
</div>
<% end %>
everything works perfectly, all the parameters are passed correctly etc, but in the view where im trying to display the result, i get a HUGE url like so:
http://localhost:3000/global/data/find?utf8=%E2%9C%93&find_field%5Bdata_type_choice%5D=2D&find_field%5Bfrom_date_choice%281i%29%5D=2012&find_field%5Bfrom_date_choice%282i%29%5D=7&find_field%5Bfrom_date_choice%283i%29%5D=9&find_field%5Bbasin_choice%5D=KG-PG&find_field%5Bto_date_choice%281i%29%5D=2012&find_field%5Bto_date_choice%282i%29%5D=7&find_field%5Bto_date_choice%283i%29%5D=9&commit=++Search++
ive tried using the ":name => nil" as shown in this railscast
why is this happening and how can i reduce the url size to just "http://localhost:3000/global/data/find" ?
EDIT:
this is my find method in fields_controller:
def find
#data_type_choice = params[:find_field][:data_type_choice]
#basin_choice = params[:find_field][:basin_choice]
#from_date_choice = params[:find_field][:from_date_choice]
#to_date_choice = params[:find_field][:to_date_choice]
end
i also want to display the data entered in the form through find.html.erb like so:
<h1><%= #data_type_choice %> dsfgdfsg</h1>
if i make it a post request, the erb is not being rendered! what can i do?
thanks :)
Just change your method from GET to POST and change the routes accordingly.
Try
<%= form_for(:find_field, url: find_field_path , method: :post) do |f| %>
Your routing file should match the request as POST.
Related
I want to be able to performa range search with ransack. I was hoping I could do this with a slider that has two thumbs. I'm thinking that the _in predicate would be perfect for a situation like this. However, I cannot find an example on how to appropriately configure this. Here would be some example code:
CONTROLLER `users_controller.rb:
def index
#page_title = 'Users'
#page_icon = 'store'
#q = User.search(params[:search]).ransack(params[:q])
#retailers = #q.result.order("#{sort_column} #{sort_direction}").paginate(page: params[:page], per_page: 20)
end
VIEW users/index.html.erb:
<%= search_form_for #q, url: users_path, class: 'form' do |f| %>
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<%= label_tag :search %>
<%= text_field_tag :search, params[:search], class: "form-control" %>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<%= f.label :first_name_eq, "First Name" %>
<%= f.select :first_name_eq, {include_blank: true}, {class: "chosen-select"} %>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<%= f.label :team_eq, "Team" %>
<%= f.select :team_eq, Team.options_for_select, {include_blank: true}, {class: "chosen-select"} %>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<%= f.label :score_in, "Score Range" %>
<%= f.search_field :score_in, {include_blank: true} %>
</div>
</div>
</div>
<div>
<%= f.submit 'Apply Filters', class: 'btn btn-outline-dark btn-md' %>
<%= link_to 'Reset Filters', users_path(q: {reset: true}), class: 'btn btn-outline-danger btn-md' %>
</div>
<% end %>
I guess where I'm lost is how to start or implement a slider w/ two thumbs that would appropriately pass the parameters when submitted. Or even if I just create a two thumbed slider from a js library, how to connect it then to ransack, or at least pass the values of the two thumbs to ransacks params. Another thing that might be helpful is if someone has an example, both in controller/view, of what the _in predicate should look like.
<%= form_tag("/posts/create") do %>
<div class="form">
<div class="form-body">
<% #post.errors.full_messages.each do |message| %>
<div class="form-error">
<%= message %>
</div>
<% end %>
<div class="continent">
<label>continent<br>
<%= #post.continent %>
<%= select :continent,
[["Africa","AF"],["North America","NA"],["Oceania","OC"],["Asia","AS"],["Europe","EU"],["South America","SA"]],
:prompt => "Select" %>
</label>
</div>
<div class="country">
<label>country<br>
<%= #post.country %>
<%= select :country,
[["Japan","JP"],["China","CH"]],
:prompt => "Select" %>
</label>
</div>
<div class="title">
<label>title<br>
<textarea name="title"><%= #post.title %></textarea>
</label>
</div>
<input type="submit" value="POST">
</div>
</div>
<% end %>
I want to set a select tag for choosing user's continent and country in the form
But It doesn't work well
I've tried some way to solve this problem.
And I just got the shape which is like "Select tag" but it's only included "prompt"
Please give me some advice.
FormOptionsHelper`s select takes these args
select(object, method, choices = nil, options = {}, html_options = {}, &block)
Try explicit putting options like this:
select :post, :country, [["Japan","JP"],["China","CH"]], { :prompt => "Select" }, { other_html_options }
If you are in a form context make this
<%= form_tag("/posts/create") do |form| %>
<%= form.select :country, [["Japan","JP"],["China","CH"]], { :prompt => "Select" }, { other_html_options } %>
<% end %>
First start off by creating the correct routes:
resources :posts
In Rails you create a resource by sending a POST request to the collection path ('/posts').
Then create the form with form_for(#post) or form_with(model: #post) (Rails 5.1+) and bind the model instance to the form builder.
<%= form_for(#post) do |f| %>
<div class="form">
<div class="form-body">
<% f.object.errors.full_messages.each do |message| %>
<div class="form-error">
<%= message %>
</div>
<% end %>
<div class="continent">
<%= f.label :continent do %>
<%= f.select :continent,
[["Africa","AF"],["North America","NA"],["Oceania","OC"],["Asia","AS"],["Europe","EU"],["South America","SA"]],
prompt: "Select" %>
<% end %>
</div>
<div class="country">
<%= f.label :country do %>
<%= f.select :country,
[["Japan","JP"],["China","CH"]],
:prompt => "Select" %>
<% end %>
</div>
<div class="title">
<%= f.label :title do %>
<%= f.text_area_field :title %>
</div>
<%= f.submit %>
</div>
</div>
<% end %>
This lets you reuse the same form for updating the resource. Also when you use the input helpers rails will properly name the inputs so that you can whitelist them with:
params.require(:post).permit(:continent, :country, :title)
Ruby 2.1.5 on Rails 4.2.0:
I have two directories. One directory is a rails-generate scaffold named 'inqueries'. The other directory is named 'welcome', which only houses a landing paged named index.html.erb. The inquery form works & submits fine as long as I'm using the view from the actual 'inquery' scaffold/directory.
I am able to render the inquery _form on my index.html.erb landing page using :
<%= render partial: "inqueries/form", locals: {inquery: #Inquery} %>
However, this JUST renders the form. When I hit the submit button, no errors, flash messages, or inquery is submitted. It is complete non-action, including on my rails terminal.
How can I properly make the form work on my landing page?
Here is my welcome_controller.rb file. This controller handles the landing page where I am trying to render the inquery scaffold's form:
class WelcomeController < ApplicationController
def index
#inquery = Inquery.new
render layout: false
end
end
This is my new rails scaffold-generated method in the inqueries_controller.rb:
def new
#inquery = Inquery.new
respond_with(#inquery)
end
Sorry, Here is the Inquery _form itself:
<%= form_for(#inquery) do |f| %>
<div class="form-group col-lg-4">
<%= f.label t('.name') %><br>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="form-group col-lg-4">
<%= f.label t('.email') %><br>
<%= f.text_field :email, class: "form-control" %>
</div>
<div class="form-group col-lg-4">
<%= f.label t('.phone') %><br>
<%= f.text_field :phone, class: "form-control" %>
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<%= f.label t('.message') %><br>
<%= f.text_area :message, rows: 8, class: "form-control"%>
</div>
<%= f.submit t('.submit'), class: "btn btn-primary" %>
<% end %>
EDIT 2: I saw there were a couple of suggestions with the logic. I have simplified the partial just to see if I could get it to work and it still does not submit properly outside of its directory. I am new to rails, do I need to render a new action in the second directory that I am calling it into?
Your partial has 2 forms. The one which submits to inqueries#create is an empty form with no submit button, the other which has no action contains the text fields and the submit action.
The form_for tag will create the html tags, you dont need to specify them again.
Tip - Switch to haml. You won't ever look back at erb :)
This should work (not tested) -
<%= form_for(#inquery) do |f| %>
<div id="error_explanation">
<% if #inquery.errors.any? %>
<h2>
<%= pluralize(#inquery.errors.count, "error") %> prohibited this inquery from being saved:
</h2>
<ul>
<% #inquery.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
</div>
<br />
<div class="col-lg-12">
<div class="form-group col-lg-4">
<%= f.label t('.name') %><br>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="form-group col-lg-4">
<%= f.label t('.email') %><br>
<%= f.text_field :email, class: "form-control" %>
</div>
<div class="form-group col-lg-4">
<%= f.label t('.phone') %><br>
<%= f.text_field :phone, class: "form-control" %>
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<%= f.label t('.message') %><br>
<%= f.text_area :message, rows: 8, class: "form-control"%>
</div>
<div class="form-group col-lg-12">
<input type="hidden" name="save" value="contact">
<%= f.submit t('.submit'), class: "btn btn-primary" %>
</div>
</div>
<% end %>
Figured it out. Foolish error,
My welcome page is a bootstrap theme that I hadn't fully gone over. I had imported some unnecessary javascript files that I think were blocking the form.
I have a form that posts something to a feed. I would like an user to post to the feed if they are signed in, but if they are not and they hit submit, I would like them to see the login modal. Here is my current form without this logic. It's a basic form_for - nothing special.
<%= form_for(#sub_opp) do |f| %>
<div class="field">
<%= f.label :sport, :class=>'form_label' %>
<%= f.select :sport, ['Basketball', 'Beach Volleyball', 'Flag Football'] %>
</div>
<div class="field" id = "datetime_select">
<%= f.label :Sub_Date, :class=>'form_label' %>
<%= f.datetime_select :sub_time, :ampm => true, :minute_step => 15 %>
</div>
<div class="actions">
<%= f.submit 'POST', :class=>"btn btn-success btn-large" %>
</div>
<% end %>
Conceptually, I would like to incorporate logic in along these lines when one clicks the submit button.
<% if current_user.present? %>
Submit the form
Otherwise
<a href="#myModal" data-toggle="modal">
Can you do this with a form_for? Thanks
I figured it out with a simple if / then statement. Surprised i did not think of this earlier. Thanks.
<% if current_user.present? %>
<div class="field">
<%= f.hidden_field :poster_user_id, :value => current_user.id %>
</div>
<div class="actions">
<%= f.submit 'POST', :class=>"btn btn-success btn-large" %>
</div>
<%else%>
<div class="actions">
POST
</div>
<%end%>
I have a view where I need to show a list of pages (I have a model called Page) and then i need the id of the selected page
I have this code in the view
مهمة جديدة
: <%= form_for :task, :url => {:action=>"create", :controller=>"tasks"}, :html => {:class :
=> "nifty_form"} do |f| %>
<% if #task.errors.any? %>
<div id="error_explanation">
<h2>: عذرا لم نستطع استكمال طلبك</h2>
<ul>
<% #task.errors.full_messages.each do |msg| %>
<li style="color:red;"><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "اسم المهمة" %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label "وصف المهمة" %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<button><%= link_to 'العودة الى قائمة المهام', project_tasks_path %></button>
<%= f.submit "مهمة جديدة"%>
</div>
<%= select_tag(:page, options_for_select(['a',1])) %>
<% end %>
#pageslist is a 2d array of the form [['pagename', 1], ..]
The question is how can i access the page_id of the selected page in the controller ?
I tried params[:p_id] but it is not working any help please?
This is what I have in the controller:
#task = Project.find(params[:project_id]).tasks.new(params[:task])
#task.update_attributes({:page_id => params[:p_id]})
Assuming that #pagelist is in a format as you described, then in your view:
<%= select_tag(:page, options_for_select( #pagelist )) %>
You had missed it in your example, and provided only 1d array for options_for_select.
With such prepared view your create action will receive page id in:
params[:page]
it's because you had passed :page as a first argument for select_tag.