Frustrated with rails has_many problem

hello there all,

i am writing a website in rails, i love it, but i am banging my head here .

ok, i have a table called sites ( means customer sites )
each site has_many :site_notes
so, site_note belongs_to :site

i used a scaffold to generate the site notes files. site notes work kinda
like comments to a blog.
a customer has sites and for each site, i want him to be able to post notes
about what what his machines are doing.

so, my link to the sites controller looks like this.
<%= link_to "site notes", :action => "list",
                              :id => @site,
                              :controller => 'site_notes' %>

and in the site_notes_controller.rb i have this
def list
    @site = Site.find(params[:id])
    @site_notes = SiteNote.find(:all, :conditions => ["site_id", @site.id])
    @site_note_pages, @site_notes = paginate :site_notes, :per_page => 10
  end

everything seems to work, it lists the right notes, i can edit and delete
them etc..
but when i try to add a new one, it tells me that it cannot find a Site
without an id.
so, i guess i need to know how to put something either in the model or
controller, (or view, whatever works)
that will automatically add site_id = @site.id when i create a new note

thanks
sk

You need to do that by hand, sorry.

···

On 11/24/06, shawn bright <nephish@gmail.com> wrote:

hello there all,

i am writing a website in rails, i love it, but i am banging my head here
.

ok, i have a table called sites ( means customer sites )
each site has_many :site_notes
so, site_note belongs_to :site

i used a scaffold to generate the site notes files. site notes work kinda
like comments to a blog.
a customer has sites and for each site, i want him to be able to post
notes
about what what his machines are doing.

so, my link to the sites controller looks like this.
<%= link_to "site notes", :action => "list",
                              :id => @site,
                              :controller => 'site_notes' %>

and in the site_notes_controller.rb i have this
def list
    @site = Site.find(params[:id])
    @site_notes = SiteNote.find(:all, :conditions => ["site_id", @site.id
])
    @site_note_pages, @site_notes = paginate :site_notes, :per_page => 10
  end

everything seems to work, it lists the right notes, i can edit and delete
them etc..
but when i try to add a new one, it tells me that it cannot find a Site
without an id.
so, i guess i need to know how to put something either in the model or
controller, (or view, whatever works)
that will automatically add site_id = @site.id when i create a new note

thanks
sk

--
Chris Carter
concentrationstudios.com
brynmawrcs.com

ok, how ?

···

On 11/24/06, Chris Carter <cdcarter@gmail.com> wrote:

You need to do that by hand, sorry.

On 11/24/06, shawn bright <nephish@gmail.com> wrote:
>
> hello there all,
>
> i am writing a website in rails, i love it, but i am banging my head
here
> .
>
> ok, i have a table called sites ( means customer sites )
> each site has_many :site_notes
> so, site_note belongs_to :site
>
> i used a scaffold to generate the site notes files. site notes work
kinda
> like comments to a blog.
> a customer has sites and for each site, i want him to be able to post
> notes
> about what what his machines are doing.
>
> so, my link to the sites controller looks like this.
> <%= link_to "site notes", :action => "list",
> :id => @site,
> :controller => 'site_notes' %>
>
> and in the site_notes_controller.rb i have this
> def list
> @site = Site.find(params[:id])
> @site_notes = SiteNote.find(:all, :conditions => ["site_id", @
site.id
> ])
> @site_note_pages, @site_notes = paginate :site_notes, :per_page =>
10
> end
>
> everything seems to work, it lists the right notes, i can edit and
delete
> them etc..
> but when i try to add a new one, it tells me that it cannot find a Site
> without an id.
> so, i guess i need to know how to put something either in the model or
> controller, (or view, whatever works)
> that will automatically add site_id = @site.id when i create a new note
>
> thanks
> sk
>

--
Chris Carter
concentrationstudios.com
brynmawrcs.com

so, in my situation, would i need to keep passing the Site.id (or, for the
site_notes table, site_id) as a variable from action to action in the
controller?

thanks
sk

···

On 11/24/06, shawn bright <nephish@gmail.com> wrote:

ok, how ?

On 11/24/06, Chris Carter <cdcarter@gmail.com> wrote:
>
> You need to do that by hand, sorry.
>
> On 11/24/06, shawn bright <nephish@gmail.com> wrote:
> >
> > hello there all,
> >
> > i am writing a website in rails, i love it, but i am banging my head
> here
> > .
> >
> > ok, i have a table called sites ( means customer sites )
> > each site has_many :site_notes
> > so, site_note belongs_to :site
> >
> > i used a scaffold to generate the site notes files. site notes work
> kinda
> > like comments to a blog.
> > a customer has sites and for each site, i want him to be able to post
> > notes
> > about what what his machines are doing.
> >
> > so, my link to the sites controller looks like this.
> > <%= link_to "site notes", :action => "list",
> > :id => @site,
> > :controller => 'site_notes' %>
> >
> > and in the site_notes_controller.rb i have this
> > def list
> > @site = Site.find(params[:id])
> > @site_notes = SiteNote.find(:all, :conditions => ["site_id", @
> site.id
> > ])
> > @site_note_pages, @site_notes = paginate :site_notes, :per_page =>
> 10
> > end
> >
> > everything seems to work, it lists the right notes, i can edit and
> delete
> > them etc..
> > but when i try to add a new one, it tells me that it cannot find a
Site
> > without an id.
> > so, i guess i need to know how to put something either in the model or
> > controller, (or view, whatever works)
> > that will automatically add site_id = @site.id when i create a new
note
> >
> > thanks
> > sk
> >
>
> --
> Chris Carter
> concentrationstudios.com
> brynmawrcs.com
>

you can do this:

<%= link_to "site notes", :action => "list",
                             :id => @site.id, # here will using @site.id
                             :controller => 'site_notes' %>

and in the site_notes_controller.rb i have this

def list
   @site = Site.find(params[:id])
   @site_notes = @site.site_notes # if you use has_many association you can
just write this
   @site_note_pages, @site_notes = paginate :site_notes, :per_page => 10
end

···

2006/11/25, shawn bright <nephish@gmail.com>:

On 11/24/06, shawn bright <nephish@gmail.com> wrote:
>
> ok, how ?
>
> On 11/24/06, Chris Carter <cdcarter@gmail.com> wrote:
> >
> > You need to do that by hand, sorry.
> >
> > On 11/24/06, shawn bright <nephish@gmail.com> wrote:
> > >
> > > hello there all,
> > >
> > > i am writing a website in rails, i love it, but i am banging my head
> > here
> > > .
> > >
> > > ok, i have a table called sites ( means customer sites )
> > > each site has_many :site_notes
> > > so, site_note belongs_to :site
> > >
> > > i used a scaffold to generate the site notes files. site notes work
> > kinda
> > > like comments to a blog.
> > > a customer has sites and for each site, i want him to be able to
post
> > > notes
> > > about what what his machines are doing.
> > >
> > > so, my link to the sites controller looks like this.
> > > <%= link_to "site notes", :action => "list",
> > > :id => @site,
> > > :controller => 'site_notes' %>
> > >
> > > and in the site_notes_controller.rb i have this
> > > def list
> > > @site = Site.find(params[:id])
> > > @site_notes = SiteNote.find(:all, :conditions => ["site_id", @
> > site.id
> > > ])
> > > @site_note_pages, @site_notes = paginate :site_notes, :per_page
=>
> > 10
> > > end
> > >
> > > everything seems to work, it lists the right notes, i can edit and
> > delete
> > > them etc..
> > > but when i try to add a new one, it tells me that it cannot find a
> Site
> > > without an id.
> > > so, i guess i need to know how to put something either in the model
or
> > > controller, (or view, whatever works)
> > > that will automatically add site_id = @site.id when i create a new
> note
> > >
> > > thanks
> > > sk
> > >
> >
> > --
> > Chris Carter
> > concentrationstudios.com
> > brynmawrcs.com
> >

so, in my situation, would i need to keep passing the Site.id (or, for the
site_notes table, site_id) as a variable from action to action in the
controller?

thanks
sk

you can do this:

<%= link_to "site notes", :action => "list",
                             :id => @site.id, # here will using @site.id
                             :controller => 'site_notes' %>

and in the site_notes_controller.rb i have this

def list
   @site = Site.find(params[:id])
   @site_notes = @site.site_notes # if you use has_many association you
can
just write this
   @site_note_pages, @site_notes = paginate :site_notes, :per_page => 10
end

>
> >
> > ok, how ?
> >
> > >
> > > You need to do that by hand, sorry.
> > >
> > > >
> > > > hello there all,
> > > >
> > > > i am writing a website in rails, i love it, but i am banging my
head
> > > here
> > > > .
> > > >
> > > > ok, i have a table called sites ( means customer sites )
> > > > each site has_many :site_notes
> > > > so, site_note belongs_to :site
> > > >
> > > > i used a scaffold to generate the site notes files. site notes
work
> > > kinda
> > > > like comments to a blog.
> > > > a customer has sites and for each site, i want him to be able to
> post
> > > > notes
> > > > about what what his machines are doing.
> > > >
> > > > so, my link to the sites controller looks like this.
> > > > <%= link_to "site notes", :action => "list",
> > > > :id => @site,
> > > > :controller => 'site_notes' %>
> > > >
> > > > and in the site_notes_controller.rb i have this
> > > > def list
> > > > @site = Site.find(params[:id])
> > > > @site_notes = SiteNote.find(:all, :conditions => ["site_id", @
> > > site.id
> > > > ])
> > > > @site_note_pages, @site_notes = paginate :site_notes,
:per_page
> =>
> > > 10
> > > > end
> > > >
> > > > everything seems to work, it lists the right notes, i can edit and
> > > delete
> > > > them etc..
> > > > but when i try to add a new one, it tells me that it cannot find a
> > Site
> > > > without an id.
> > > > so, i guess i need to know how to put something either in the
model
> or
> > > > controller, (or view, whatever works)
> > > > that will automatically add site_id = @site.id when i create a new
> > note
> > > >
> > > > thanks
> > > > sk
> > > >
> > >
> > > --
> > > Chris Carter
> > > concentrationstudios.com
> > > brynmawrcs.com
> > >
>
> so, in my situation, would i need to keep passing the Site.id (or, for
the
> site_notes table, site_id) as a variable from action to action in the
> controller?
>
> thanks
> sk
>

thanks, that helped a lot, i am able to move around the controller passing

@site to each next action, and putting it in the form as a hidden field.
Thanks for the tip there

sk

···

On 11/24/06, ao tianlong <aotianlong@gmail.com> wrote:

2006/11/25, shawn bright <nephish@gmail.com>:
> On 11/24/06, shawn bright <nephish@gmail.com> wrote:
> > On 11/24/06, Chris Carter <cdcarter@gmail.com> wrote:
> > > On 11/24/06, shawn bright <nephish@gmail.com> wrote: