How do I get a form to submit it's params in the url, such that the rendered page will contain the query (rails 2.3)?
Something like this:
example.com/search?name=john&age=25&city=atlanta
Simple, I know, but I'm not sure how to do it... :)
thanks.
This is not specific to Rails. Just set the method attribute of the form to GET.
as stated by Jimmy: make sure your form-tag looks like this:
<form method="get" .....>
If you're using routing helpers to do this use:
search_path(:name => "John", :age => 25, :city => "atlanta")
Related
I am using Apotomo with Rails and I have some code that looks like this:
root.find_widget(:messages).render :state => :display
which works fine.
I would like to pass some data along with that call. How do I do that?
I imagine something like this:
root.find_widget(:messages).render :state => :display, :my_variable => its_value
and then to be able to access my_variable in the widget's display method.
Please is there a way to do this?
Thanks.
You need to use render_state that comes from Cells, upon which Apotomo is built. It can be used with arguments like this:
root.find_widget(:messages).render_state(:display, my_variable: its_value)
Let's say I need a simple search form:
<form action="<%= search_path %>" methode="GET">
<input type="text" placeholder="where?" name="place_name" />
</form>
I need to generate a url based on the input field so I can show:
/search/some-value
I tried:
match 'search(/:place_name)', to: 'some_controller#some_action', as: 'search'
Any suggestions why it's not working or what I'm doing wrong here?
If you submit a form, the parameters set via inputs within the form will be appended to the URL using query parameters — in your case that would look something like this:
/search?place_name=some-value
That’s the basic form behavior and there’s no easy way around this unless you want to use some Javascript to handle the request (which I would advice against in this case).
To use the default behavior however, your route would need to look like this:
match 'search', to: 'some_controller#some_action', as: 'search'
The parameter place_name will then be available in the controller using:
params[:place_name]
Hope this helps.
The following code supports unicode and additional parameters:
get "search/*place_name" => "store#search"
get "search" => redirect {|_, req|
params = req.query_parameters
query = Rack::Utils.escape params.delete(:place_name){"%"}
"/search/#{query}#{params.to_query.prepend('?') if params.any?}"
}, as: :search
I have a form where I'd like to be able to input a web address, like "google.com", into a form field, and be able to click on this link in the show view of the submitted form.
How can I accomplish this?
show.html.erb
<p>
<strong>Website:</strong>
<%= link_to #video.website, #video.website %>
</p>
Scaffolding will handle it like Jason Swett said. If you are looking to put it in a link in show just do something like this:
<%=link_to #link.name, "http://"+#link.url%>
If that doesn't work you could always do:
<%=#link.name%>
Scaffolding will handle this for you. I recommend going through the Rails Guides, especially this one: http://guides.rubyonrails.org/generators.html
And more specifically, you can generate a model with an attribute called url like this:
rails generate scaffold Thing url:string
And then run your migrations:
rake db:migrate
You should end up with some views in app/views/thing/ and a controller at app/controllers/thing_controller.rb. That should put you on your way.
If you have a model that has an attribute like web_address, you can do:
= text_field_tag, "web_address", ""
(using HAML syntax and tags for form_tag)
And then in your show you can do:
= link_to model.web_address, model.web_address
Which will make a link like: http://www.google.ca
Trying to use the form_tag helper in rails to submit to an SSL address. Currently, my code looks like this:
form_tag(form_action_path) do
# This spits out:
<form action="form_action_path" method="post">
If I try this:
form_tag(form_action_path, :protocol => 'https', :only_path => false)
# It spits out:
<form action="form_action_path" method="post" protocol="https" only_path="false>
That is of course, not a valid or worthwhile result. How can I make the form tag helper render out an https action path?
Thanks.
It turns out that I was using the wrong syntax.
Instead of
form_tag(form_action_path, :protocol => 'https')
I needed
form_tag(form_action_url(:protocol => 'https'))
The difference being, apparently, that form_action_path generates something like "/path/to/action" and form_action_url generates "http://url.com/path/to/action."
You can use something like SSL_Requirement with your create/update actions. It seems SSL_Requirement is older (not that it needs to be updated, it's not terribly complicated), but there may be a newer gem/plugin that people prefer now.
I seem to have a problem that I can't find the solution for myself, I hope someone can help.
I have a form defined like so:
<% form_for #leads do |f| %>
I have a resource called #leads (map.resource :leads)
But when I look in the HTML code of the page it generates, I see as a form action the following
<form action="/lead.%23%3Clead:0x10333e858%3E" class="edit_lead" ... etc
The lead.%23%3Clead:0x10333e858%3E as a form action does work, however rails doesn't know what to do with it after it updates. Does anyone know how I can make this a normal URL so that rails can redirect after the update again?
Thank you very much
Regards, Marco
I think you have to rename your route from
map.resource :leads
to
map.resources :leads
because you have multiple leads (and not only one -> so no "resource", its "resources")
If you are using a singular resource you should not pass the object to the url helper, ie. lead_path not lead_path(#lead).
However it does look like a typo and your route should be map.resources :leads