Partial templates question

hi, i have a question about partial templates.

there is a table - Topics, it has one field - Title.

there is a text_field form with button, and a partial template under the
form. they are in the same template. the partial can display Titles
which is already in the Topics table.

user enter a string in the form, click the button. the string will save
as Title in Topics table. the partial will display all the data from the
Topics table, including the string which user just entered.

i have been thinking all day, but can’t find a solution. can someone
help, give me a solution. thanks very much!

Bob Song wrote:

hi, i have a question about partial templates.

there is a table - Topics, it has one field - Title.

there is a text_field form with button, and a partial template under the
form. they are in the same template. the partial can display Titles
which is already in the Topics table.

user enter a string in the form, click the button. the string will save
as Title in Topics table. the partial will display all the data from the
Topics table, including the string which user just entered.

i have been thinking all day, but can’t find a solution. can someone
help, give me a solution. thanks very much!

I am doing pretty much this exact same thing. What I have done is to
wrap my partials inside of divs with an id value
‘name_of_div_where_partial_is’. I used the form_remote_tag helper to add
to the list without reloading the whole page. Then you can do one of two
things:

  1. In the method that is called when you hit the submit button you save
    the new Title and then quary to get the old titles and the new title.
    Then, you should have the partial already set-up so that variable is
    being used to dsiplay the title in that partial. Add the :update option
    to the form_remote_tag and it should update without reloading the whole
    page:

<%= form_remote_tag :update => ‘name_of_div_where_partial_is’, . . . %>

or,

  1. In the method that gets called when you hit the submit button you can
    use the render method with the :update attribute. So something like:

render :update do |page|
page.replace_html ‘name_of_div_where_partial_is’, :partial =>
‘name_of_partial’
end

What this does it is says to redisplay the partial in the div
overwriting the html currently there.

I use the second way to update a group of two lists that I have on a
single page, where I remove one item from a list that then automatically
gets added to the second list of removed stuff. Hope this helps

thanks Shandy N.! it really useful!
following is my code

Controller:
def index
@topic = Topic.new(params[:topic])
if request.post?
@user.save
end

@topics = Topic.find(:all)

end

View:
<% remote_form_for :topic, :update => ‘name_of_div_where_partial_is’ do
|f| %>
<%= f.text_field :title %>
<%= submit_tag “Add” %>
<% end %>

<%= render(:partial => "show", :collection => @topics) %>

Partial:
<%= show.title%>


_____________________________________________

one more question. if i would like to highlight the new line. how can i
do?

i created a highlight.rjs file, which including highlight javascript
code. the highlight code needs to know which is the new entered object.
so i transfered the new object’s id to it.

View:
<%= javascript_include_tag :defaults %>
<% remote_form_for :user, :url => {:action => :highlight, :id => @user},
:update => ‘name_of_div_where_partial_is’ do |f| %>
<%= f.text_field :name %>
<%= submit_tag “Add” %>
<% end %>

<%= render(:partial => "show", :collection => @users) %>

Controller:
def index
@user = User.new(params[:user])
if request.post?
@user.save
end
@users = User.find(:all)
end

def highlight
@user = User.find(params[:id])
end

loading the index page, it works well. but after i enter a new name,
click submit, partial shows “Couldn’t find User without an ID”. the user
ID didn’t transfer to the highlight method