开发者

rails 3 and ajax: set action how?

开发者 https://www.devze.com 2023-02-19 14:31 出处:网络
I have a create.js.erb, I want make this: if the user click to create button, the site load index partial.

I have a create.js.erb, I want make this: if the user click to create button, the site load index partial.

$('.content').html("<%= escape_javascript(render(:partial => "events/index")) %>");

If I use this, I get error @objectname.each is wrong, I think it's empty, because in the cr开发者_开发技巧eate.js.erb file I only call the partial, and I think I must call index method somehow. It's right?

    def create
    @event = Event.create(params[:event])
    respond_to do |format|
      if @event.save
        format.html { redirect_to(events_url) }
        format.js
      else
        format.html { render :action => "new" }
        format.js
      end
    end
end

the error message:

ActionView::Template::Error (undefined method `each' for #<Event:0x00000102182830>):
    1: - @event.each do |events|
    2:   %h3.title= events.name


You need to provide the partial with the events it should be rendering. Right now you're just providing it with the single event, @event. Populate @events if the action is a success then update events/_index to call @events.each instead of @event.each.

def create
  @event = Event.create(params[:event])
  respond_to do |format|
    if @event.save
      format.html { redirect_to(events_url) }
      format.js {
        @events = Event.all
      }
    else
      format.html { render :action => "new" }
      format.js
    end
  end
end
0

精彩评论

暂无评论...
验证码 换一张
取 消