views:

32

answers:

2

I am trying to follow the rules and stay RESTful, but I am running into some problems.

I set my routes so I am sure it is calling the right action, when I run rake routes this comes up:

PUT /admin/settings {:controller=>"admin", :action=>"save_settings"}

So I know my route is correct, and in my *views/admin/settings.html.erb" I have the following:

<% form_for(:settings, :html => { :method => :put },:builder => MyFormBuilder) do |f| %>

And everything seems to render correctly (since rails dances around a PUT):

<form action="/admin/settings" method="post">
<input name="_method" type="hidden" value="put" />

But when I actually click on the submit button, nothing happens. For testing, I simply do a flash:

# PUT admin/settings
def save_settings
  flash[:notice] = 'Settings Saved'
  render :action => 'settings'
end

And nothing flashes. I think I covered my bases here, but I must be forgetting something. please help out a RoR noob :)

+2  A: 

You need to render the settings page again after setting the flash otherwise the page will not be redrawn as you have not given the browser any new content.

A "standard" restful controllers update action looks something like this

  def update
    @device = Device.find(params[:id])
    if @device.update_attributes(params[:device])
      flash[:notice] = "Successfully updated device."
      redirect_to @device
    else
      render :action => 'edit'
    end
  end

So you should either add a render to your save_settings method or redirect back to itself.

Steve Weet
I had a render in there, I just took it out for the post (edited above) - I am still just getting a postback to the page and nothing else?
naspinski
+1  A: 

You either do what Steve has said or if your are adamant about mentioning the request type in the form builder, then you should use correct syntax.

Try this

`<% form_for :post, @post, :url => post_path(@post), :html => { :method => :put, :class => "edit_post", :id => "edit_post_45" } do |f| %>`

This is taken from rails' documentation. First argument is the name of object (settings in your case/model class name in most cases), second for the instance variable in which form data will be stored (the name of variable you have used in your "edit" action, in url you can either use url helpers or you can manually specify controller and action, and your "put" verb will go into html options hash.

Follow this and see if it is working.

Jagira