# Help: restful and ajax call

**URL:** <https://www.ruby-forum.com/t/help-restful-and-ajax-call/74053>\
**Category:** Rails\
**Created:** [October 29, 2006, 3:41pm UTC](https://www.ruby-forum.com/t/help-restful-and-ajax-call/74053 "2006-10-29T15:41:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 29, 2006, 3:41pm UTC](https://www.ruby-forum.com/t/help-restful-and-ajax-call/74053/1 "2006-10-29T15:41:04Z")

</div>

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

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 29, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/help-restful-and-ajax-call/74053/2 "2006-10-29T16:01:06Z")

</div>

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.

–  
-yossef

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 29, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/help-restful-and-ajax-call/74053/3 "2006-10-29T17:03:15Z")

</div>

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

On 10/29/06, Yossef M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> that update means replace\_html. If you want to append something, use  
> :position.
> 
> –  
> -yossef
> 
> >

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 29, 2006, 11:06pm UTC](https://www.ruby-forum.com/t/help-restful-and-ajax-call/74053/4 "2006-10-29T23:06:00Z")

</div>

On Oct 29, 2006, at 6:40 AM, Dark A. wrote:

> …  
> \<% end %\>
> 
> # here is the div for the  
> update (which works when …keep reading)
> 
> In the controller -  
> def addtfield  
> render(:partial =\> ‘txtfield’)  
> end

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.

\<% form\_for(:whatever, :url =\> whateverss\_path) do |f| %\>  
\<%= text\_filed ‘whatever’, ‘foo’ %\>

\<%= link\_to\_function “[+] add”, “addNewTextField(‘buttons\_info’,  
‘canlocation’, ‘city’)” %\>

 \<%= submit\_tag "Submit" %\> 
\<% end %\>

```
Have fun with it.

```

– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 29, 2006, 5:17pm UTC](https://www.ruby-forum.com/t/help-restful-and-ajax-call/74053/5 "2006-10-29T17:17:16Z")

</div>

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

On 10/29/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > And I don’t know the specifics of your page layout, but keep in mind
> 
> –  
> [Dark ambient - Wikipedia](http://en.wikipedia.org/wiki/Dark_ambient)

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...
