What's the best way to stop repeating this line in my contro

I don’t think the before_filter will work if you need
to have the action reflect the List.create step. What
about
def list_add
if List.create(params[:list])
index
render(:partial => “listform”)
end
?

Cheers,
B Gates

I am trying to follow the “DRY” principle and came
across a problem.
I am working on a small ajax task manager, and right
now in the index
page which renders the list, I have (for example, in
the Lists
controller):

def index
@lists = List.find(:all)
end

Now within the controller there’s also a list_add
action which is
called via ajax…

def list_add
if List.create(params[:list])
@lists = List.find(:all)
render(:partial => “listform”)
end


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

On 8/17/06, B Gates [email protected] wrote:

You’re right; I hadn’t looked that carefully at your list_add function.
However, if what you’re trying to do is use ajax to add an item to a
task
list on a page, then you shouldn’t need to repopulate the your @lists
collection.

Instead your listform partial should contain the view for a single
record.
The index view would iterate through record set and show the partial for
each one.

So what you should do is:

def index
@lists = List.find(:all)
end

def list_add
if List.create(params[:list])
render(:partial => “listform”)
end
end

Then in your index view, you have something like this:

    <% for @list in @lists %>
            <%= render(:partial => 'listform') %>
    <% end %>

In _listform.rthml you then have the code that displays a single item
(which
you refer to as @list; ie, @list.title, etc.)

In fact, you can take it a step further and use RJS. Get rid of the
render
line in your list_add method. In the same folder as your index.rhtml,
create
a file called list_add.rjs. Your list_add method will automatically look
for
such a file and execute it if found. Add the following line to your
list_add.rjs file:

page.insert_html :bottom, ‘id-of-your-task-div-or-table’, :partial =>
‘listform’

This will add a new instance of your listform partial at the end of your
list.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs