CSS style disappear when using RJS and page.replace

Hi! I’m using RJS to process a form in a partial (_newtask.rhtml) and
replace a partial (_tasks.rhtml) in the same view (index.rhtml):

  • _newtask.rhtml-view *
    <%= form_remote_tag :update => “tasks”, :url => { :action => ‘create’ }
    %>

  • task_controller.rb *
    def index
    @task = Task.new
    @projects = Project.find(:all)
    @tasks = Task.find(:all)
    @timeframes = Timeframe.find(:all)
    @tasks_today = Task.find(:all, :conditions => “timeframe_id = 1”)
    @tasks_thisweek = Task.find(:all, :conditions => “timeframe_id = 2”)
    @tasks_nextweek = Task.find(:all, :conditions => “timeframe_id = 3”)
    @tasks_later = Task.find(:all, :conditions => “timeframe_id = 4”)
    end

def create
@newtask = Task.new(params[:task])
@newtask.save
@tasks_today = Task.find(:all, :conditions => “timeframe_id = 1”)
@tasks_thisweek = Task.find(:all, :conditions => “timeframe_id = 2”)
@tasks_nextweek = Task.find(:all, :conditions => “timeframe_id = 3”)
@tasks_later = Task.find(:all, :conditions => “timeframe_id = 4”)
end

  • create.rjs-template *
    page[:tasks].replace :partial => “tasks”

When submitting the form and the _tasks-partial updates, the CSS-styling
has disappeared. What have I missed?

Also, is there a way that I can leave out the @task_today etc. from the
create-method. Doesn’t seem very DRY :wink:

Thanks!

Why would you need

@tasks_today = Task.find(:all, :conditions => “timeframe_id = 1”)
@tasks_thisweek = Task.find(:all, :conditions => “timeframe_id = 2”)
@tasks_nextweek = Task.find(:all, :conditions => “timeframe_id = 3”)
@tasks_later = Task.find(:all, :conditions => “timeframe_id = 4”)

when creating?

On 10/8/07, Gus Vs [email protected] wrote:

@task = Task.new
@newtask = Task.new(params[:task])

When submitting the form and the _tasks-partial updates, the CSS-styling
has disappeared. What have I missed?

Also, is there a way that I can leave out the @task_today etc. from the
create-method. Doesn’t seem very DRY :wink:

Thanks!


Ramon T.

Why would you need

@tasks_today = Task.find(:all, :conditions => “timeframe_id = 1”)
@tasks_thisweek = Task.find(:all, :conditions => “timeframe_id = 2”)
@tasks_nextweek = Task.find(:all, :conditions => “timeframe_id = 3”)
@tasks_later = Task.find(:all, :conditions => “timeframe_id = 4”)

when creating?

It doesn’t seem very DRY does it :slight_smile: But if I leave them out I get “You
have a nil object when you didn’t expect it!” error in _tasks.rhtml.
This probably relates to why the CSS doesn’t load, but I don’t know
how…

Ah, you might wanna paste your partial too (_tasks). :slight_smile:

On 10/8/07, Gustav S. [email protected] wrote:

It doesn’t seem very DRY does it :slight_smile: But if I leave them out I get “You
have a nil object when you didn’t expect it!” error in _tasks.rhtml.
This probably relates to why the CSS doesn’t load, but I don’t know
how…


Ramon T.

Honestly I can’t explain why your styles are lost… but it might help
if you paste your partial as well, since we know what your rjs does.

On 10/8/07, Gustav S. [email protected] wrote:

I use the RJS to render the partial, is there some reference that I need
to do in the RJS that I’m missing?


Ramon T.

Ramon T. wrote:

Ah, you might wanna paste your partial too (_tasks). :slight_smile:

I use the RJS to render the partial, is there some reference that I need
to do in the RJS that I’m missing?

Here it goes, many thanks for any input!

index.rhtml

<%= render :partial => "newtask" %>
<%= render :partial => "tasks" %>

_newtask.rhtml
<%= form_remote_tag :update => “tasks”,
:url => { :action => ‘create’ } %>

<%= text_field :task, :description %>

<%= collection_select(:task, :project_id,
@projects, :id, :name) %>

<%= collection_select(:task, :timeframe_id,
@timeframes, :id, :name) %>

<%= submit_tag ‘Create!’, :id => ‘submit’ %>

<%= end_form_tag %>

_tasks.rhtml

Today

    <% @tasks_today.each do |task| %>
  • <%= check_box :task, :completed, :onchange => "$('task_#{task.id}').toggleClassName('completed');" %>
    <%= task.description %>
    <%= link_to_remote 'Delete', :url => { :action => 'destroy', :id => task }, :update => :tasks %>
    <%= task.project.name %>
  • <% end %>

Found it! Some sort of conflict: I needed to remove ":update => “tasks”
from form_remote_tag. Also I moved the div-declaration into the
partials. Thanks!

Gosh, you got me. I don’t know why your CSS wouldn’t work with this.
My only guess is that the CSS doesn’t apply to this because the div
names are different…? :eek:

On 10/8/07, Gustav S. [email protected] wrote:

Here it goes, many thanks for any input!


Ramon T.