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 %>
Related
In Rails, I have a custom controller action that needs to accept some parameters from a form:
def update_ordid
# Get the active exchange
#exchange = Exchange.find(params[:id])
# Decide which order ID field to update
active_order_field = params[:ordfld]
# Save the order ID
order_id = params[:ordid]
if active_order_field == 1 then
#exchange.order_id_1 = order_id
else
#exchange.order_id_2 = order_id
end
#active_exchange.save
respond_with(#exchange)
end
Because these parameters aren't actual data fields in the exchange table, I would typically invoke the action by using a link such as:
link_to "Update Order ID", update_ordid_exchange(ordfld: value_from_form, ordid: value_from_form), :method => :post
Because in this case the value of these parameters needs to be populated by user input, I created the following form to pass the data:
<%= form_for(#exchange, url: update_ordid_exchange_path) do |f| %>
<div class="field">
<%= f.label :ordid, "Order ID" %><br>
<%= f.text_field :ordid, class: "form-control" %>
</div>
<% if #isrequestor == true %>
<%f.hidden_field :ordfld, :value => "1" %>
<% else %>
<%f.hidden_field :ordfld, :value => "2" %>
<% end %>
<div class="actions">
<%= f.submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>
When I attempt to render this form, I receive the error: undefined method `ordid' for #
When researching this issue, I found that I could be able to do this by changing the text_field line to:
<%= f.text_field_tag :ordid, class: "form-control" %>
While this resolves the initial error, it throws a new error: undefined method `text_field_tag' for #
Any ideas as to what I'm doing wrong?
You cannot use form_for since your form elements doesn't represent the attributes of a model. use form_tag instead
<%= form_tag(update_ordid_exchange_path, :method => :patch) do%>
<div class="field">
<%= label_tag "Order ID" %><br>
<%= text_field_tag :ordid, class: "form-control" %>
</div>
<% if #isrequestor == true %>
<%= hidden_field_tag :ordfld, "1" %>
<% else %>
<%= hidden_field_tag :ordfld, "2" %>
<% end %>
<div class="actions">
<%= submit_tag "Submit", class: "btn btn-primary" %>
</div>
<%end%>
Documentation here
form_tag vs form_for
hidden_field_tag
I'm a bit new to this myself, but I believe it's because you're calling "text_field_tag" from a form_for instead of form_tag builder object. Try just leaving off the form for object as such:
<%= text_field_tag :ordid, class: "form-control" %>
I have model product. I want to create multiple record in single model (product), but fields not display in view
#controller
#products = Array.new(3){ Product.new }
# view
<%= form_tag create_product_path, :method => :post, :class => "form-horizontal", 'role' => "form" do %>
<% #products.each_with_index do |product, index| %>
<% fields_for "products[#{index}]", product do |f| %>
<%= f.text_field :date %>
<%= f.text_field :name %>
<% end %>
<% end %>
<%= submit_tag "Submit", :class => "btn btn-primer" %>
Look at this screenshoot, fields not appear. Can anyone tell me, why new form method using array not appear?
Don't forget about <%= on fields_for
read about fields_for
rails 2.x - 3.0
<% fields_for "products[#{index}]", product do |f| %>
rails > 3.1.x
<%= fields_for "products[#{index}]", product do |f| %>
I have two forms on one view page. One of these is an update form. The other form is a single button that goes to the same controller action but does not actually update the form. Here is the first form:
<%= form_for [#project, #schedule] do |f| %>
<%= f.fields_for :tasks do |builder| %>
<%= render 'task_fields', :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add task", f, :tasks %>
<p><%= f.submit "Submit" %></p>
<% end %>
And here is the second form:
<%= form_tag project_schedule_path(#project, #schedule), method: "patch" do %>
<div><%= hidden_field_tag :emp_accepts, true %></div>
<%= submit_tag "Accept schedule", class: "btn btn-large btn-primary" %>
<% end %>
The problem is the strong parameters. The strong parameters require :schedule, which only the first form provides. So when I try to use the second form, an error is returned. Here are the strong params:
def schedule_params
params.require(:schedule).permit(:emp_accepts,
tasks_attributes: [:title, :content, :_destroy])
end
you should change the second form to
<%= form_for [#project, #schedule], http: { method: 'patch' } do |f| %>
<div><%= f.hidden_field :emp_accepts, value: true %></div>
<%= f.submit 'Accept schedule', class: 'btn btn-large btn-primary' %>
<% end %>
It looks like you should really use different controller methods for each action. It would make it easy for you to make changes in the future.
Recently started to study rails and faced with the next problem:
I have two tables Inboxes(request) and Equipments. Inboxes has many Equipments through InboxEquipment.
Since the equipment can be many, the searching for the right is difficult. I need to add field quick search or filter values of table Equipments. I think this can be done using Ajax, but I don't know him. Something similar is described in "Episode #240 Search, Sort, Paginate with AJAX", but I don't know how to make to search has been in the create a new record
My current working code:
../controllers/inbox_controller.rb
def new
if signed_in?
#inbox = Inbox.new(params[:inbox])
#partners = #inbox.partners.build(params[:partners])
#equipments = #inbox.equipments.search(params[:search])
end
end
../inboxes/new.html.erb
<% if signed_in? %>
<%= render 'shared/inbox_form' %>
<% end %>
../shared/_inbox_form.html.erb
<%= form_for #inbox, :html => { :multipart => true } do |f| %>
<div class="new_application">
<h2>Входящая заявка</h2>
<%= render 'shared/error_messages', object: f.object %>
<div class="fields_inbox">
<h4><center>Контрагент:</center></h4>
<%= f.select(:partner_id, options_for_select(Partner.all.collect {|x| [x.name, x.id]}, :include_blank => "Выберите контрагента")) %>
<%= f.select(:equipment_ids, options_for_select(Equipment.all.collect { |z| [z.name, z.id] }), {}, {:multiple=>true, :size => 10}) %>
<h4><center>Тип заявки:</center></h4>
<%= f.select(:application_type, ["Новая","Принята","На рассмотрении","Завершена"], :include_blank => "Выберите тип зявки") %>
<center>
<%= f.submit "Добавить", class: "btn btn-large btn-primary" %>
<%= link_to "Назад", :back, :class => "btn btn-large btn-primary" %>
</center>
</div>
</div>
<% end %>
EDIT
Add two images as works now and how to
So now working http://s12.postimg.org/vt91ceqkd/inbox.jpg
So it should work http://s22.postimg.org/z20zb8c9d/inbox_correct.jpg
What do I need to add to my code to make it work as I need to (Example in second image)?
I'm trying to create a filter form that user selects dates and sources. The problem is after clicking submit button, in the new page i see that input values are empty. Is there a way to make form remember its values ? Thanks.
<%= form_tag products_path, :method => 'get' do %>
<%= text_field_tag :from %>
<%= text_field_tag :to %>
<% Source.all.each do |source| %>
<%= check_box_tag "sources[]", source.id %>
<%= source.name %><br />
<% end %>
<%= submit_tag "Submit", :name => nil %>
<% end %>
controller
def index
#from = params[:from] ? params[:from].to_datetime : (Time.now-3.day)
#to = params[:to] ? params[:to].to_datetime : (Time.now)
#sources = params[:sources] ? params[:sources] : 1..6
#products = Product.where(:source_id => #sources, :created_at => #from.beginning_of_day..#to.end_of_day)
end
Can't you use the value and checked options from these tags? Here is an example :
<%= form_tag products_path, :method => 'get' do %>
<%= text_field_tag :from, #from %>
<%= text_field_tag :to, #to %>
<% Source.all.each do |source| %>
<%= check_box_tag "sources[]", source.id, #sources.include?( source.id ) %>
<%= source.name %><br />
<% end %>
<%= submit_tag "Submit", :name => nil %>
<% end %>