How do I use a form to capture data for a variable that is no a
database field? In the case, I would like to search my database based
on the data that the user inputs via a form. Any assistance will be
greatly appreciated.
Regards,
Paul Thompson
How do I use a form to capture data for a variable that is no a
database field? In the case, I would like to search my database based
on the data that the user inputs via a form. Any assistance will be
greatly appreciated.
Regards,
Paul Thompson
First use form_tag rather than form_for (which is designed for model
based forms). The instead of something like f.text_field you would use
text_field_tag.
Then just pull the parameter out of params by name
params[:name_of_field] and put that into your variable.
On Aug 7, 3:13 pm, “Paul Jonathan T.” [email protected]
2008/8/8 Robert W. [email protected]:
First use form_tag rather than form_for (which is designed for model
based forms). The instead of something like f.text_field you would use
text_field_tag.Then just pull the parameter out of params by name
params[:name_of_field] and put that into your variable.
Hi Robert,
thanks for the guidance, I am pretty green when it comes to rails so
one more question if you don’t mind? How do I kick this off? I have
this in my views folder.
<% form_tag :action => ‘update’, :id => @supplier do %>
<%= render :partial => ‘test’ %>
<%= submit_tag ‘Save’ %>
<% end %>
And partial _test.
Obviously “<% form_tag :action => ‘update’, :id => @supplier do %>”
needs to be changed as there is now no supplier database. Can you
please set me straight?
Thanks,
Paul Thompson.
Enter Data: |
<%= text_field_tag, 'Field1' %> |
Ok. To expand on that a bit say you wanted to add a search field to
find suppliers.
map.resources :suppliers, :collection => { :search => :get }
def search
@suppliers = Supplier.find_by_name(params[:search])
respond_to … # the standard respond_to stuff here
end
<% form_tag(search_suppliers_path) do %>
<%= text_field_tag :search
<%= submit_tag ‘Search’ %>
<% end %>
This is untested code off the top of my head, but check the Rails API
docs for form_tag and test_field_tag for more details on usage. Also
this is only one of many solutions. It all depends on your needs, but
this is the simplest example I can think of.
Also if you don’t subscribe to the Railscasts podcast. Check it out.
It has very good information to help get you started.
On Aug 7, 3:53 pm, “Paul Jonathan T.” [email protected]
Ahh. I see. The answer is still lurking in those previous responses.
You can still use form_field_tag inside of a form_for block.
So if you had something like:
<% form_for @suppliers do |form| %>
<%= form.text_field :name %>
<%= text_field_tag :extra %> #using a text_field_tag here because
it’s not tied to a model
<%= hidden_field_tag :hidden_extra, “Some hidden value” %> #or even
use a hidden_field_tag
<% end %>
Then in the controller access :extra by params[:extra] or
params[:hidden_extra]
On Aug 7, 6:20 pm, “Paul Jonathan T.” [email protected]
2008/8/8 Robert W. [email protected]:
def search
<% end %>This is untested code off the top of my head, but check the Rails API
docs for form_tag and test_field_tag for more details on usage. Also
this is only one of many solutions. It all depends on your needs, but
this is the simplest example I can think of.Also if you don’t subscribe to the Railscasts podcast. Check it out.
It has very good information to help get you started. http://railscasts.com/
Hi Robert,
I would really, really love to be able to benefit from Railscasts
podcast. However I am totally deaf and so I am unable to use what must
be a marvellous resource.
I think that I have not explained properly what I want to do as the
answer that you have provided seem very complex for what is a small
thing. At the end of the day, all I want to do is accept a name from
an input field in a view. then store it in a session variable so that
I can use it to access various tables in a database when the user
decides what reports they want. Sorry if I have led you up the garden
path.
Thanks,
Paul Thompson
Hi Robert,
I just want to say thank you, very very much. Your last piece of
advise did the trick perfectly
Sorry that it has taken me so long to get back to you but I was side
tracked by other issues. I am just one guy looking after IT for
company of fifty employees and I do everything. So sometimes I have to
put one thing aside and concentrate on another.
Thank you once again for the trouble that you have taken. It
appreciated.
Regards,
Paul Thompson.
2008/8/9 Robert W. [email protected]:
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs