If I have a form taking a collection of names, a date, and another boolean via virtual attributes how can I submit them to a new page and action. That has nothing to do with CRUD. It will simply do some processing then spit out the values on a new page.
personsSelection.erb
<%= render :partial => 'myForm' %>
_myForm.html
<%= simple_form_for(#person) do |f| %>
<%= f.input :names, :collection => People.all, as => :check_boxes %>
<%= f.input :DateTime %>
<%= f.check_box :paid %>
<%= f.submit %>
person_controller.erb
def personReport
#Some Random Processing
end
personReport.html.erb
#Display the personReport processing data
Again, I'm trying to submit the form to process via personReport action then display to a new page called personReport.html.erb
In your routes
match "/person/personreport" => "person#personReport", :as => personreport
In your form
<%= simple_form_for(#person, :url => personreport_path) do |f| %>
This will send the data to your personReport action where you can process the data then by default that action will render personReport.html.erb
Related
Im trying to design a shopping cart. i.e a customer shopping online adds a product to their trolley.
I want to go straight to create action from my new action without going to new.html.erb with pre-set values in my params
Here is what I have so far:
#trolley_id += 1
redirect_to :controller => 'trolleys', :action => 'create', :id => #trolley_id, :something => 'else', method: :post
This redirects me to my index action
To do this with javascript templates, it would look like this:
view
= form_form Trolley.new, remote: true do
-# form goes here
The remote true will submit it as javascript, which will try to render a javascript template.
Your create action can either render :create or let Rails render your template automatically. Since it came in as a javascript request, Rails will render the template with format js.
trolleys/create.js.erb
var html = "<%= j render 'trolley_row', trolley: #trolley %>
$('table.trolleys body').append(html);
I managed to resolve my problem. I created a form in my Product_controller#show that will go straight to my Trolley_controller#create and create an entry in my Trolleys table
<%= simple_form_for [#product, #trolley] do |f| %>
<%= f.input :quantity, collection: 1..12, prompt: "select quantity" %>
<%= f.input :product_id, :as => :hidden %>
<%= f.input :user_id, :as => :hidden %>
<%= f.button :submit, "Add to Basket" %>
<% end %>
I have a url like this http://example.com/myController?key=12345
In my controller myController, i can access the key value with params[:key], but when i submit a form, i want to get that same key in the create method, but the params[:key] is null.
How can i access params[:key] in my post action create?
You might indeed add a hidden field.
If you find yourself adding many of those because you need to have the value available in more and more actions, consider setting it within the session as (e.g.) session[:current_key]
You can pass the info along by a hidden_field_tag. So your code would be like:
<%= form_for #model do |f| %>
<%= hidden_field_tag 'key', params[:key]
# other form code
<%= f.submit %>
<% end %>
You can add a hidden field to your form and set its value to params[:key]. Then it will be available in the params you get from the form.
If you're using Simple Form:
<%= simple_form_for #model do |f| %>
...
<%= f.input :field_name, :as => :hidden, :input_html => { :value => params[:key] } %>
<%= f.button :submit %>
<% end %>
Add a new hidden field to pass via params, when you submit the form your action is changed or called again params hash is recreated; what you can do is pass the value via hidden field in the form as follow,
<%= simple_form_for #model do |f| %>
# your remaining form fields
<%= f.input :field_name, as: :hidden, :input_html: { value: params[:key] } %>
<%= f.button :submit %>
<% end %>
I have a Rails 3.2 ajax form that creates a new Room for a Hotel. The new Room ajax form works correctly on the Room index page, but once I embed the new Room ajax form on a Hotel edit page, the form is submitted normally without using Ajax.
To create the new Room form, I use the following link on the Hotel edit.html.erb page:
<%#= link_to 'Add a Room', new_room_path(:hotel_id => #hotel.id), :remote => true, :class => 'new_room' %>
This loads the following form partial on to that same page:
<%= form_for #room, :remote => true, :html => { :class => "remote-form" } do |f| %>
<%= f.text_field :number %>
<%= f.text_field :size %>
<% if(params.has_key?(:'hotel_id')) %>
<% #hotel_id = params[:hotel_id] %>
<%= f.hidden_field :hotel_id, :value => #hotel_id %>
<% else %>
<%= f.collection_select(:hotel_id, Hotel.all, :id, :name) %>
<% end %>
<%= f.submit "Add this room", :class => 'room_create' %>
<%= link_to 'Cancel', '#', :class => "room_cancel" %>
<% end %>
And finally, I have the following in my create.js.erb (inside the rooms folder):
alert('Test creating a room');
var content = $('<%= escape_javascript(render(#room)) %>');
$("#room_list tbody").append(content);
The create.js.erb is not executed and the form is submitted regularly (non-ajax) and I finally arrive on the room show.html.erb page.
Why is the form be working correctly on the Units index page, but not on the associated Hotel edit page?
Even when you set :remote => true, Rails generates a form tag. Nested form tags are not supported by browsers and will result in unpredictable behavior.
You should rethink the views architecture here. Probably you can have the forms for the rooms outside of the form for the hotel, or maybe you can use fields_for and accepts_nested_attributes_for to edit children objects.
Here's a full example on how to use nested attributes: Nested Attributes Examples.
You cannot nest a form inside a form in HTML. When you click any submit button on a form, even if it's inside another form, only the outermost form will be properly submitted.
You can either use nested attributes to add the attributes for the room directly to the form, so that when the overall form is submitted so are all the rooms... or use a div and a link, instead of a form and a submit button.
I've got a Rails app that has two main views: an overview and a sequenced view. The user can enter data in either view. I use the same form helper for both views, but I want the redirect_to from the create action to respect the user's context. So I pass a hidden_field_tag called 'track'.
I can't seem to access the value of 'track' inside my controller.
Here's the form:
<%= form_for([#mission, #mission.stickies.build]) do |f| %>
<div class="field">
<%= f.text_field :name, :size => 60 %>
<%= f.hidden_field :kind, :value => kind %>
<%= hidden_field_tag :track, :value => track %>
<class="actions">
<br><%= f.submit "Add to " + kind.pluralize.capitalize %>
</div>
<% end %>
And here's where I call it in one of the views:
<%= render :partial => "stickies/form" , :locals => { :kind => "driver", :track => 'main' } %>
Here's the parameters dump (from a different call):
{"utf8"=>"✓",
"authenticity_token"=>"N3IXwNQosOfxw1ZcpfFPOLPKzHbvNyaBhAiP3ftT9GY=",
"sticky"=>{"name"=>"Tesssksjd argghh.",
"kind"=>"success"},
"track"=>"{:value=>\"sequence\"}",
"commit"=>"Add to Successes",
"mission_id"=>"32"}
And here's the relevant code in my create controller:
if params[:track][:value] == "main" then
redirect_to mission_path(#mission) + '#' + #sticky.kind.pluralize
elsif params[:track][:value] == "sequence" then
redirect_to mission_stickies_path(#mission, :kind => #sticky.kind)
end
I can't seem to find the syntax, or comparator, or whatever I need to access the value represented by "track"=>"{:value=>\"sequence\"}".
Any help or pointers would be greatly appreciated. I'm new to Rails and Ruby, this is my first app.
Don't write it with the :value => track, rather do:
<%= hidden_field_tag :track, track %>
and access it with params[:track]
I have a form field in a view that is separate from my initial form. I want the person using the application to be able to edit a single field without having to pull up the entire form. My code is as follows
<%= form_for :user, :url => {:controller => 'users', :action => 'update' } do |f| %>
<%= f.text_field :barcode %>
<%= submit_tag 'Register' %>
<% end %>
When trying to submit the changes to the specified form field I receive an error on the create method I believe. It redirects me to the user controller with the proper id but gives me the following error.
Unknown action
The action '1' could not be found for UsersController
I have tried changing the method from update to create, but then it brings up the blank form, I just want to be able to edit the specified field without having to re-create the form and get the error. Any ideas?
You are not passing the user object to the form.
Try also using the path helper generated by the routes:
<%= form_for #user, :url => user_path(#user) do |f| %>
<%= form_for(#user), :url => url_for(:controller => 'users', :action => 'update') do |f| %>
<%= f.text_field :barcode %>
<%= f.submit, 'Register' %>
<% end %>
It should work now...