rendering simple html from view/controller

5 views
Skip to first unread message

adedip

unread,
Mar 11, 2010, 9:32:33 AM3/11/10
to Ruby on Rails: Talk
I start apologies for the silly question..but:
how do I show the content of a view in a controller by url I mean why,
with the starting rails conf and I simple scaffold done, i can't do
root/:controller/:action and see the action.html.erb content?
it warns me writing
"Couldn't find Product with ID=new2"
where new2 is an empty method in the controller and with a
new2.html.erb file existing in the view/controller_name folder

I had my guess..but I'd like to better understand how to do this.. I
mean..is this about the routes.rb configuration? or there's a way to
fill the method, don't know some particular use of respond_to ?!

I know it's a really stupid question..but still can't figure out how
this work.
thanks since now :)

andrea

Punit Rathore

unread,
Mar 11, 2010, 4:41:15 PM3/11/10
to rubyonra...@googlegroups.com
Andrea,

You need to provide us with more information. From the info you have
given I cannot figure out what is going on.

> "Couldn't find Product with ID=new2"

You say that you have an empty method, but it is trying to find a
Product with ID=new2. I think that it cannot find the correct route from
the routes.rb file.

Actually you should paste all the code snippets from your controller,
view and routes.rb file.

Punit


--
Posted via http://www.ruby-forum.com/.

adedip

unread,
Mar 11, 2010, 5:56:34 PM3/11/10
to Ruby on Rails: Talk
Ok..so that's what I mean:

I need to create a new view where I want to insert for istance (and in
this exact case) a form that I use to upload a file and then process
the file.
So in first place I create a view "new2.html.erb" in my view folder
associated to a controller let's say "references" with a form in it.
so now i have in the "view/references" folder this new file

how can I access this view? can I without creating a method? I guess I
don't because when I type http://localhost:3000/references/new2 i get
that error:
"Couldn't find Reference with ID=new2"
(but it render the page with the form if I had any kind of id after,
i.e. http://localhost:3000/references/new2/1 )

the question is: why for istance the "index" page or the "new"
generated by the scaffold can render the page (and the form in case of
new) without passing any ID and I can't now? what's the rule
underneath?

my new2.html.erb page contain this:
--
<h1>New reference csv</h1>
<% form_for :reference, @references, :method => :get, :url =>
{:action => 'new'} do %>

<p>Filename: <%= text_field_tag "file_tag" %> </p>

<p> <%= submit_tag("Carica csv") %></p>
<% end %>
<%= link_to 'Back', references_path %>
--
(I've redefined the new method to parse a csv file)

and the routes.rb is the default one given by rails with the only
restful defined like this:
--
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
--
I've tried to add something like:
map.connect ':controller/:action.:format'

but it does not do the trick .. :D

I'd like to understand how this work in general more than solve this
particular problem :)

thank u all :)
andrea

Punit Rathore

unread,
Mar 12, 2010, 3:02:53 AM3/12/10
to rubyonra...@googlegroups.com
Andrea,


> how can I access this view? can I without creating a method? I guess I
> don't because when I type http://localhost:3000/references/new2 i get
> that error:
> "Couldn't find Reference with ID=new2"
> (but it render the page with the form if I had any kind of id after,
> i.e. http://localhost:3000/references/new2/1 )


You routes.rb file would also have this line.
map.resources :references

What this does is that it creates RESTful urls for references. You
should read up on restful routes.


Since the routes.rb files is read top-down, the RESTful url for
references gets more priority over map.connect
':controller/:action/:id'

So maybe you should add a named route for your new2 method(and make sure
its before the map.resources line in your routes.rb) or another
controller to handle uploading of files.

adedip

unread,
Mar 12, 2010, 8:40:10 AM3/12/10
to Ruby on Rails: Talk
right!
thx for reply..
I'm going to read a basic restful/routes guide
I guess it's something dealing with GET or POST method
thx :)

On 12 Mar, 09:02, Punit Rathore <li...@ruby-forum.com> wrote:
> Andrea,
>
> > how can I access this view? can I without creating a method? I guess I

> > don't because when I typehttp://localhost:3000/references/new2i get

adedip

unread,
Mar 12, 2010, 8:40:25 AM3/12/10
to Ruby on Rails: Talk
I meant GET or POST request ;)

On 12 Mar, 09:02, Punit Rathore <li...@ruby-forum.com> wrote:

> Andrea,
>
> > how can I access this view? can I without creating a method? I guess I

> > don't because when I typehttp://localhost:3000/references/new2i get

Colin Law

unread,
Mar 12, 2010, 8:44:22 AM3/12/10
to rubyonra...@googlegroups.com
On 12 March 2010 13:40, adedip <ade...@gmail.com> wrote:
> right!
> thx for reply..
> I'm going to read a basic restful/routes guide

In case you have not seem there there are excellent guides at
http://guides.rubyonrails.org/ including one on routing. I would
suggest starting with Getting Started however which will explain the
basic principles of rails, then work through the others.

Colin

RavionRails

unread,
Mar 12, 2010, 11:34:50 PM3/12/10
to Ruby on Rails: Talk
just add
map.resources :referenced , :collection => { 'new2' => 'get' }

On Mar 12, 6:44 pm, Colin Law <clan...@googlemail.com> wrote:
> On 12 March 2010 13:40, adedip <ade...@gmail.com> wrote:
>
> > right!
> > thx for reply..
> > I'm going to read a basic restful/routes guide
>

> In case you have not seem there there are excellent guides athttp://guides.rubyonrails.org/including one on routing.  I would

adedip

unread,
Mar 15, 2010, 9:16:56 AM3/15/10
to Ruby on Rails: Talk
Hi guys..I did try almost everything..but still cant' get any result..

Starting from a simple "scaffold" creating a class called "reference"
I have updated the controller with this 2 methods:
--
def load

end

def upload
require 'fastercsv'
filename = params[:file_tag]
FasterCSV.foreach("public/trac/#{filename}", {:col_sep => ";"} )
do |row|
Reference.create(:idarticolo => row[0], :nomefilefoto => row[1],
:cancellato => row[2] )
end

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @reference }
end
end
--

the I've added a view file in the "app/view/reference" folder called
"load.html.erb"
with this code:

--
<h1>New reference</h1>


<% form_for :reference, @references, :method => :get, :url =>

{:action => 'upload'} do %>

<p>Filename: <%= text_field_tag "file_tag" %> </p>

<p> <%= submit_tag("Load csv") %></p>


<% end %>
<%= link_to 'Back', references_path %>
--

then I've updated the routes.rb file with this line (do I have to
replace the already in "map.resources :references" or do i add this
second new line?):
--
map.resources :references, :collection => {"load" => "get"}
--

going to url: http://localhost:3000/references/load I get this error
message:
--
ActiveRecord::RecordNotFound in ReferencesController#show

Couldn't find Reference with ID=load

RAILS_ROOT: /Users/adedip/Lavoro/antonioli1
Application Trace | Framework Trace | Full Trace

/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/
base.rb:1586:in `find_one'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/
base.rb:1569:in `find_from_ids'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.5/lib/active_record/
base.rb:616:in `find'
/Users/adedip/Lavoro/antonioli1/app/controllers/
references_controller.rb:25:in `show'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/
base.rb:1331:in `send'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/
base.rb:1331:in `perform_action_without_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/
filters.rb:617:in `call_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/
filters.rb:610:in `perform_action_without_benchmark'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/
benchmarking.rb:68:in `perform_action_without_rescue'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/
core_ext/benchmark.rb:17:in `ms'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/
core_ext/benchmark.rb:17:in `ms'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/
benchmarking.rb:68:in `perform_action_without_rescue'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/
rescue.rb:160:in `perform_action_without_flash'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/
flash.rb:146:in `perform_action'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/
base.rb:532:in `send'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/
base.rb:532:in `process_without_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/
filters.rb:606:in `process'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/
base.rb:391:in `process'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/
base.rb:386:in `call'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.5/lib/action_controller/
routing/route_set.rb:437:in `call'
--

If I add any number at the end of the url it works perfectly (i.e.
http://localhost:3000/references/load/1 )

what am I doing wrong?
can someone explain me the process to get this work? thanks in
advance :)

Andrea

On 13 Mar, 05:34, RavionRails <ravindrasing...@gmail.com> wrote:
> just add
> map.resources :referenced , :collection => { 'new2' => 'get' }
>
> On Mar 12, 6:44 pm, Colin Law <clan...@googlemail.com> wrote:
>
> > On 12 March 2010 13:40, adedip <ade...@gmail.com> wrote:
>
> > > right!
> > > thx for reply..
> > > I'm going to read a basic restful/routes guide
>

> > In case you have not seem there there are excellent guides athttp://guides.rubyonrails.org/includingone on routing.  I would

adedip

unread,
Mar 24, 2010, 6:57:59 AM3/24/10
to Ruby on Rails: Talk
I guess is something dealing with this:
attr_accessor :name

isn't it?

> going to url:http://localhost:3000/references/loadI get this error

> > > On 12 March 2010 13:40,adedip<ade...@gmail.com> wrote:
>
> > > > right!
> > > > thx for reply..
> > > > I'm going to read a basic restful/routes guide
>

> > > In case you have not seem there there are excellent guides athttp://guides.rubyonrails.org/includingoneon routing.  I would

Punit Rathore

unread,
Mar 24, 2010, 7:09:49 AM3/24/10
to rubyonra...@googlegroups.com
>then I've updated the routes.rb file with this line (do I have to
>replace the already in "map.resources :references" or do i add this
>second new line?):
>--
> map.resources :references, :collection => {"load" => "get"}
>--


You have to replace the line.

Reply all
Reply to author
Forward
0 new messages