Using form input without updating model Ruby on Rails - ruby-on-rails

I would like to put a form on one of my pages, but don't want to use form_for to update a model. I am basically using this like a filtering/searching system, where the user inputs something, and the page changes based on what the user input. I know this is a pretty simple problem, but I'm also a little new to Rails.
Note: I have the ability to filter the results if I can just get the input value. I just need access to the input value in my Controller.

Just use form_for with a symbol as argument rather than an instance variable.
You can access the form data in your controller by referencing your params, just like you normally would. Let's say you have a form kinda like this:
<%= form_for :search do |f| %>
<%= f.text_field :query %>
<%= f.submit %>
<% end %>
You'll then get the contents of the search form by calling params[:search][:query] in your controller.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for

you need meta_search
<%= form_for #search, :url => articles_path, :html => {:method => :get} do |f| %>
<%= f.label :title_contains %>
<%= f.text_field :title_contains %><br />
<%= f.label :comments_created_at_greater_than, 'With comments after' %>
<%= f.datetime_select :comments_created_at_greater_than, :include_blank => true %><br />
<!-- etc... -->
<%= f.submit %>
<% end %>

Related

Hardcode partial input value in a Rails form_for field

I have a Rails form where the user is able to make a Top5 list. While they are able to label the list, all lists start with the phrase "Top 5 Greatest." Of course, it's easy to use Regex and append this phrase to the front of a list in the controller, but then I'm stuck doing it for both the create and update actions, so it's not very dry. Is there a way to hardcode a partial value in a rails form_for that will then be combined with the rest of the form input and sent to the controller?
This is currently what the form looks like (minus the form_for partial):
<br><%= f.label "Top Five Greatest:" %>
<%= f.text_field :title %><br>
<%= f.hidden_field :user_id, value: user.id %>
<br><%= f.label "#1" %>
<%= f.text_field :number1 %>
<br><%= f.label "#2" %>
<%= f.text_field :number2 %>
<br><%= f.label "#3" %>
<%= f.text_field :number3 %>
I'd really appreciate any insight.
Override setter method:
# put this in your model:
def title=(value)
super("Top 5 Greatest #{value}")
end

Adding a send email option in a form for a Rails model

After my users fill out a form, I want them to have the option of sending or not sending an email.
I can do this easily in the controller by doing something like:
send_email if params[:entry]
but I'm not sure how to introduce this param under my form_for, since it isn't part of the model.
How can I get this param to show up in the view and be available upon submit?
Use the #check_box_tag form helper in the form_for block
It can be something as simple as this:
<%= form_for #notice do |f| %>
<%= f.label :text, 'Notice Text' %>
<%= f.text_area :text %><br />
<%= label_tag 'entry', 'Send Email?' %>
<%= check_box_tag 'entry' %>
<%= f.submit %>
<% end %>

Order confirmation page in rails

I've been trying to create an order confirmation page for my rails app, and am not quite sure how to go about it in a restful way.
There were a few answers on this question that got me halfway there, but the problem was that I wasn't quite sure how to set up the form in the rails view so that it would take the user to a confirmation page with all their details instead of a create action.
Right now my view is simple:
<% form_for :order do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :first_name %><br />
<%= f.text_field :first_name, :size => 15 %>
</p>
<p>
<%= f.label :last_name %><br />
<%= f.text_field :last_name, :size => 15 %>
</p>
(Be sure to enter your name as it appears on your card)
<p>
<%= f.label :card_type %><br />
<%= f.select :card_type, [["Visa", "visa"], ["MasterCard", "master"], ["Discover", "discover"], ["American Express", "american_express"]] %>
</p>
<p>
<%= f.label :card_number %><br />
<%= f.text_field :card_number %>
</p>
<p>
<%= f.label :card_verification, "Card Verification Value (CVV)" %><br />
<%= f.text_field :card_verification, :size => 3 %>
</p>
<p>
<%= f.label :card_expires_on %><br />
<%= f.date_select :card_expires_on, :discard_day => true, :start_year => Date.today.year, :end_year => (Date.today.year+10), :add_month_numbers => true %>
</p>
<p><%= f.submit "Submit" %></p>
What things should I be doing to direct the user to a confirmation page that shows all the order details?
Thanks!
Kenji
There were a few answers on this
question that got me halfway there,
but the problem was that I wasn't
quite sure how to set up the form in
the rails view so that it would take
the user to a confirmation page with
all their details instead of a create
action.
Directing a form to a non standard page is pretty simple.
Add a url option form_for.
Such that
<% form_for :order do |f| %>
becomes
<% form_for :order :url => {:action => "confirm"} do |f| %>
You'll need to crate the confirm action in your routes, but that only involves this:
map.resources :orders, :collection => {:confirm => :get}
All you need now is a basic controller action and a view:
def confirm
#order = Order.new(params[:order])
unless #order.valid?
render :action => :new
else
end
end
Your view should look almost identical to the show view, with the addition of a form submitting #order to the create action.
Why don't you pull the confirmation via ajax for example, pull the result and put it as an overlay div, upon confirmation submit the original values in the form.
If you still need to do it your way then check wizardly, it's exactly designed for such uses.
I would like to update the answer for more elegant Rails 4 or up.
I hope it will help newbies like me. Ruby is awesome! :)
routes.rb
resources :orders do
collection do
post 'confirm'
end
end
orders_controller.rb
def confirm
#order = Order.new(order_params) # GET THE POST parameters
render :new if #order.invalid? # Return if false
end
form.html.erb
<%= form_for #order, url: {action: 'confirm'} do |f| %>

Specifying html attributes with form_for helper methods

Is it possible to specify html attributes while using the form_for helper methods?
For example:
<% form_for #user do |f| %>
<%= f.label :username%>
<%= f.text_field :username %>
<%= f.submit "Signn Up" %>
<% end %>
How would I go about specifying the class for the label? Is it possible, or do I have to resort to label()?
On mostly helpers, the last arg is a hash of html options for the element.
<%= f.label :username, "Username", :class => "class" %>

RESTful way to use form_for?

I am attempting to use form_for to implement a search form that works with a table-less Search model I created. The search form keeps triggering the 'index' action. I assume I should use 'new' to create the form and 'create' the process the search query. Looking at the log, my POST is getting changed into a GET. Here's my code:
/searches/new.html.erb:
<% form_for :searches, #search, :url => searches_path, :html => {:method => :post} do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :keywords %><br />
<%= f.text_field :keywords %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
What's the standard way for triggering the 'create' action with form_for?
Are you using the RESTful map.resources :searches ?
If so, shouldn't your :url be set to new_search_path ?
form_for is used with models. For a simple search form, I reccommend doing something like this:
<% form_tag posts_path, :method => :get do |f| %>
<%= f.text_field :query %>
<% end %>
You'll get /posts?query=wtf.

Resources