views:

537

answers:

2

I have my code working so that I have a table of businesses. There's a pencil icon you can click on the edit the business information. The edit information comes up in a partial inside of a modal pop up box. The only problem is that once they make the changes they want and click update, it sends them to the 'show' page for that business. What I want to happen is have the pop up box close and have it update the information. This is my update function in my controller.

 def update
@business = Business.find(params[:id])

respond_to do |format|
  if @business.update_attributes(params[:business])
    flash[:notice] = 'Business was successfully updated.'
    format.html { redirect_to(business_url(@business)) }
    format.js
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @business.errors, :status => :unprocessable_entity }
  end
end

end

I tried following railscast 43 and i created an .rjs file but I couldn't get that to work at all. My update was still taking me to the show page. Any help would be appreciated.

EDIT: Added some more code.

<% form_for(@business) do |f| %>
<%= f.error_messages %>

<p>
  <%= f.label :name %><br />
  <%= f.text_field :name %>
</p>
...
      <%= f.label :business_category %><br />
      <%= f.select :business_category_id, @business_categories_map, :selected => @business.business_category_id %>
    </p>
    <p>
      <%= f.label :description %><br />
      <%= f.text_area :description %>
    </p>
    <p>
      <%= f.submit 'Update' %>
    </p>
  <% end %>

This is my form inside of my edit page which is being called through the index in a pop up by:

<div id="popupEdit<%=h business.id %>" class="popupContact">
      <a class="popupClose<%=h business.id %>" id="popupClose">x</a>
      <% if business.business_category_id %>
        <% @business = business  %>
        <%= render "business/edit" %>
      <% end %>
    </div>
A: 

It's hard to say without seeing your form code (hint hint! :-), but it could be that you're using form_for (which will submit a POST as per normal html forms) instead of remote_form_for (which will send an AJAX request).

John Hyland
A: 

Gotta say, get your code to work without javascript first ... also, don't use RJS at all. Use unobtrusive javascript instead.

As Mr. Hyland says, we need to see your view code also before we can help any further.

Omar Qureshi