REST with 1.2.3

Hello all,

First let me say im a n00b. Im coming into the OOP world from the
design world. So far so good. With great resources like this, youre
all making my life less painfull.

Question -

The current project im involved in is in 1.2.3 (not by my choice)

Basically i have a form partial that i want to be able to route the
information to either create or update, but do this like rails 2 would
do.

1 form, 1 submit button, and have it be smart enough to know what to
do.

this is my form code

<% form_for :styles, :url => styles_url(:id => current_client.id) do |
f| %>
<%= render :partial => “styles/form”, :locals => {:f => f} %>
<%= button :action => :submit, :label => ‘Save Styles’, :class =>
‘blue’ %>

i have a map.resources :styles in my routes

My Styles Controller

def create
@client = Client.find(params[:id])
@styles = Styles.new(params[:styles])
if @styles.save
flash[:notice] = ‘Your custom styles have been created.’
redirect_to :controller => “client”, :action => ‘edit’, :id =>
@client.id
else
render :back
end
end

def edit
@styles = Styles.find(params[:id])
end

def update
@client = Client.find(params[:id])
@styles = Styles.find_by_client_id(@client.id)
if @styles.update_attributes(params[:styles])
flash[:notice] = ‘Your custom styles have been updated.’
redirect_to :controller => “client”, :action => ‘edit’, :id =>
@client.id
else
render :back
end
end

any help would be great…

On Nov 13, 11:13 am, Bob O [email protected] wrote:

Hello all,

First let me say im a n00b. Im coming into the OOP world from the
design world. So far so good. With great resources like this, youre
all making my life less painfull.

Welcome!

Question -

The current project im involved in is in 1.2.3 (not by my choice)

That’s unfortunate :slight_smile:

f| %>
I’m rusty on 1.2.3 syntax, but I think you can just specify the method
needed:

form_for :styles, :url => styles_url(:id =>
current_client.id), :method => :put do

so actually, you might want to move this into your form partial,
passing a local variable to indicate whether or not you should
use :method => :put or :method => :post (or you can leave it off
entirely for the :post case).

I can’t tell from your code, but if your partial is using a variable
like @styles, then actually you could just look at @styles.new_record?
to find out if it’s new (i.e. use :post) or old (i.e. use :put).
That’s actually what the 2.0 code does internally.

Hope this helps?

Jeff

www.purpleworkshops.com

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