Hi, and thank you for reading this.
First of all let me explain that i am a developer learning Ruby and
RubyOnRails for future projects, so i am basically a noob that is in
need of some help.
I created a little RoR project (im using netbeans 6.1 as IDE, but that
should make no difference), that is a very simple product management
web app.
I created it using scaffolding, and CRUD works just fine when called
from the views that the scaffold created. I have some other
controller, the âmainâ controller with a âwelcomeâ view. This
controller is not tied to any model since it only exists has the
welcome page for the site.
My problem is that i wanted to call the ânewâ view from âwelcomeâ view
using render :partial=>âproducts/newâ, but when ever i call it from
there it gives me an error.
I would like that this partial view would work just like any other
view.
How can i do this?
sample code is bellow
products.rb
class Product < ActiveRecord::Base
validates_uniqueness_of :name
end
products_controller.rb
[code=]class ProductsController < ApplicationController
GET /products
GET /products.xml
def index
@products = Product.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @products }
end
end
GET /products/1
GET /products/1.xml
def show
@product = Product.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @product }
end
end
GET /products/new
GET /products/new.xml
def new
@product = Product.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @product }
end
end
GET /products/1/edit
def edit
@product = Product.find(params[:id])
end
POST /products
POST /products.xml
def create
@product = Product.new(params[:product])
respond_to do |format|
if @product.save
flash[:notice] = 'Product was successfully created.'
format.html { redirect_to(@product) }
format.xml { render :xml => @product, :status
=> :created, :location => @product }
else
format.html { render :action => ânewâ }
format.xml { render :xml => @product.errors, :status
=> :unprocessable_entity }
end
end
end
PUT /products/1
PUT /products/1.xml
def update
@product = Product.find(params[:id])
respond_to do |format|
if @product.update_attributes(params[:product])
flash[:notice] = 'Product was successfully updated.'
format.html { redirect_to(@product) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @product.errors, :status
=> :unprocessable_entity }
end
end
end
DELETE /products/1
DELETE /products/1.xml
def destroy
@product = Product.find(params[:id])
@product.destroy
respond_to do |format|
format.html { redirect_to(products_url) }
format.xml { head :ok }
end
end
end
[/code]
new.html.erb
[code=]
New product
<% form_for(@product) do |f| %>
<%= f.error_messages %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :price %>
<%= f.text_field :price %>
<%= f.label :type %>
<%= f.text_field :type %>
<%= f.submit "Create" %>
<% end %><%= link_to âBackâ, products_path %>
[/code]
show.html.erb
[code=]
Name:
<%=h @product.name %>
Price: <%=h @product.price %>
Type: <%=h @product.type %>
<%= link_to âEditâ, edit_product_path(@product) %> |
<%= link_to âBackâ, products_path %>
[/code]
index.html.erb
[code=]
Listing products
Name | Price | Type | |||
---|---|---|---|---|---|
<%=h product.name %> | <%=h product.price %> | <%=h product.type %> | <%= link_to 'Show', product %> | <%= link_to 'Edit', edit_product_path(product) %> | <%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to âNew productâ, new_product_path %>
[/code]
edit.html.erb
[code=]
Editing product
<% form_for(@product) do |f| %>
<%= f.error_messages %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :price %>
<%= f.text_field :price %>
<%= f.label :type %>
<%= f.text_field :type %>
<%= f.submit "Update" %>
<% end %><%= link_to âShowâ, @product %> |
<%= link_to âBackâ, products_path %>
[/code]
the partial view _new.html.erb (its a copy of new.html.erb)
[code=]
New product
<% form_for(@product) do |f| %>
<%= f.error_messages %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :price %>
<%= f.text_field :price %>
<%= f.label :type %>
<%= f.text_field :type %>
<%= f.submit "Create" %>
<% end %><%= link_to âBackâ, products_path %>
[/code]
main_controller.rb
class MainController < ApplicationController
def welcome
end
end
welcome.html.erb
[code=]
Main#welcome
Find me in app/views/main/welcome.html.erb
<%= render :partial=>âproducts/newâ %>
[/code]
So my question is, shouldnât the partial view that is being called
from the welcome view, work just like it was being called from
products/new view?
I am aware that this is probably not the best of examples, but imagine
that i have a welcome page, and on that same view i would like to
allow users to search and create new products. And show all the error
messages on the welcome page.
How is it done?
Thank you once more for your time!
CrazyMenConnected