Forum: Ruby on Rails REST with 1.2.3

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bob O (Guest)
on 2008-11-13 19:20
(Received via mailing list)
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..
Jeff C. (Guest)
on 2008-11-13 20:59
(Received via mailing list)
On Nov 13, 11:13 am, Bob O <removed_email_address@domain.invalid> 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 :-)

> 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 topic is locked and can not be replied to.