After I thought this was fixed I found out that it wasn’t.
What i want to do is give the user the ability to add additional text_fields to their form.
I’ve created the calls and forms but let me show what’s going on:
In the form:
<%= link_to_remote “[+]”,
:update => ‘xtratitle’,
:url => {:action => “addtfield”} %>
…
<% end %>
# here is the div for the update (which works when …keep reading)
In the controller -
def addtfield
render(:partial => 'txtfield')
end
So here is the where the problem seems to be:
In the controller I have a
before_filter :find_candidate
private
def find_candidate
@candidate_id = params[:candidate_id]
redirect_to candidate_url unless @candidate_id
@candidate = Candidate.find(@candidate_id)
When it’s enabled - and I try the link_to_remote I get this error:
ActiveRecord::RecordNotFound
in CanpositionsController#addtfield
Couldn’t find Candidate without an ID
If I disable the before_filter (which is obviously not the right solution)
Then it gets me in the create or update code for not having the @candidate_id so db operations can’t take place.
I thought that since I was only adding a field into the form the form should handle this candidate_id , not sure.
Any suggestions ?
Stuart
–
http://en.wikipedia.org/wiki/Dark_ambient
Stuart,
Adding a field is a separate action, so you're going to have to let
your controller know what you're doing. Add :candidate_id =>
@candidate.id in your :url hash, change your before_filter to not
include the addtfield action, use the session or flash, do something.
And I don't know the specifics of your page layout, but keep in mind
that update means replace_html. If you want to append something, use
:position.
Thank you Yossef.
It is working now , however I believe adding the candidate_id to the link_to_remote hash has any effect
url => {:action => “addtfield”, :candidate_id => @
candidate.id}
I only say that cause I added a condition in the action based on the candidate_id and it had no effect.
Unless I need to add more braces , which I haven’t tried yet.
Stuart
I also have another related question to this -
Having a weird behaviour with my action :
def addtfield
user_title_counts = Cantitle.count(:all, :conditions => [“user_id = ?”, current_user.id])
if user_title_counts == 3
flash[:notice]= "Maximum 3 allowed"
else
render(:partial => 'txtfield')
end
end
Problem is that it’s not sending a flash notice but it’s throwing an error looking for addtfield.rhtml ?
Stuart
Hey Stuart-
Ok so you don't need an ajax call for this it is overkill. All you really need is some javascript to add a new form element. So here is a js function that will insert a new text_field when you click a link.
<script type='text/javascript'>
function addNewTextField(id, obj, meth, val){
new Insertion.Before(id, '<input type="text" name="'+obj+'['+meth+']" value="'+val+'" />')
}
</script>
<% form_for(:whatever, :url => whateverss_path) do |f| %>
<%= text_filed 'whatever', 'foo' %>
<%= link_to_function "[+] add", "addNewTextField('buttons_info', 'canlocation', 'city')" %>
<div class="buttons_info">
<%= submit_tag "Submit" %>
</div>
<% end %>
Have fun with it.
-- Ezra Zygmuntowicz-- Lead Rails Evangelist
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)