Recent Posts by James West
|
Nov 18, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / p.507 collection_select example doesn't work for me. Ooops it was me being an arse :-) <= f.collection_select(:catalogue_id, @catalogues, :id, :name)> Is what was needed. Sorry for the messy opening post. The code tags seem to have messed up the dislay somewhat! |
|
Nov 17, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / p.507 collection_select example doesn't work for me. <%=
@users = User.find(:all, :order => "name" )
form.collection_select(:name, @users, :id, :name)
%>
Hi, I have a category that belongs to catalogues and am trying to use this method to pick the catalogue that the category belongs to when creating/editing a category record. I am totally unable to get this method to work in as outlined in the book. I get the selection list populated with the correct data but am getting the following error when submitting the form to create a new record. NoMethodError in Categories#create You have a nil object when you didn’t expect it! My view looks like this. (This is a rendered template(Not a partial) used for both editing and creating category records)
<legend>Enter Catalogue Details</legend>
<p>
<%= f.label :catalogue_name -%>:
<%= f.collection_select(:category_name, @catalogues, :id, :name)%>
</p>
<p>
<%= f.label :category_name -%>:
<%= f.text_field :category_name %>
</p>
<p>
<%= f.label :category_description -%>:
<%= f.text_area :category_description, :rows => 2, :cols => 40 %>
</p>
<p>
<%= f.label :category_image_url -%>:
<%= f.text_field :category_image_url %>
</p>
The relevant parts of my controller look like this
# GET /categories/new
# GET /categories/new.xml
def new
@category = Category.new
@catalogues = Catalogue.get_all_catalogues
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @category }
end
end
# GET /categories/1/edit
def edit
@category = Category.find(params[:id])
@catalogues = Catalogue.get_all_catalogues
end
# POST /categories
# POST /categories.xml
def create
@category = Category.new(params[:category])
respond_to do |format|
if @category.save
flash[:notice] = 'Category was successfully created.'
format.html { redirect_to(@category) }
format.xml { render :xml => @category, :status => :created, :location => @category }
else
format.html { render :action => "new" }
format.xml { render :xml => @category.errors, :status => :unprocessable_entity }
end
end
end
# PUT /categories/1
# PUT /categories/1.xml
def update
@category = Category.find(params[:id])
respond_to do |format|
if @category.update_attributes(params[:category])
flash[:notice] = 'Category was successfully updated.'
format.html { redirect_to(@category) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @category.errors, :status => :unprocessable_entity }
end
end
end
The category model looks like this and all validation passes
class Category < ActiveRecord::Base
belongs_to :catalogues
has_many :products
validates_presence_of :category_name
validates_uniqueness_of :category_name
validates_presence_of :catalogue_id,
:message => "No catalogue has been assigned!"
validates_format_of :category_image_url,
:with => %r{\.(gif|jpg|png)$}i,
:unless => Proc.new{|c| c.category_image_url.blank?},
:message => 'must be a URL for GIF, JPG ' +
'or PNG image.'
end
The catalogue model looks like this
class Catalogue < ActiveRecord::Base
has_many :categories
validates_presence_of :name
validates_uniqueness_of :name
validates_format_of :thumbnail_url,
:with => %r{\.(gif|jpg|png)$}i,
:unless => Proc.new{|c| c.thumbnail_url.blank?},
:message => 'must be a URL for GIF, JPG ' +
'or PNG image.'
def self.get_live_catalogues
@catalogues = self.find_all_by_available_to_sell(true)
end
def self.get_all_catalogues
@catalogues = self.find(:all, :order => "name")
end
end
Is this a bug that needs reporting as errata or is it just me being an a**e and getting something wrong? Thanks James |
|
Nov 8, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Can't Seem to download the PDF When I bought the book I had a few issues with downloading. You should have an option to re-generate the pdf file then try to download again The pdf reader has a save button that allows you to save the file to your HD. HTH |
|
Nov 3, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / #notice css error for new user form Hi, Name: [ ] Now looks like Name I assume it’s an error in the #notice declaration in the depot.css style sheet but does anyone have any clue how to fix it? CSS is a bit of a dark art for me at the moment. Cheers James |
|
Oct 30, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / What is different between "Clearing" a table and "Emptying" a table? Hi, If you look at the code you’ll see that the up method deletes all records then adds some new ones Empty and clear out are just interchangeable terms that mean the same thing. i.e. clear out the car and empty out the car. Same dofference in this instance. HTH James |
|
Oct 29, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Instance Variables for newbies In my hunt to find out how to write classes properly I cam accross this http://rubylearning.com/satishtalim/writing_our_own_class_in_ruby.html. Totally awesome. Alan – It’s well wort a read. |
|
Oct 29, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Instance Variables for newbies Alan. James |
|
Oct 29, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Instance Variables for newbies Alan. I have a feeling that we could probably help each other out quite a lot if we were able to talk. I’m not posting a public contact in this forum so if you have any suggestions then let me know. James |
|
Oct 29, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Instance Variables for newbies Hi Alan. As it happens after my last post I wnet looking for books and the pickaxe book is one that I am considering quite seriously. It’s in BETA at the moment and I am hooked on this BETA thing with this book. Much better to get your info as up to date as possible when starting out ne thinks! I am obviously trying to understand similar issues and I think I have an incling into the bit of insight that you are missing and like all things the complexity of usage may be hiding the simplicity of the language (wood and trees). Given that Rails is a framework and not a language (Ruby is the language) you can learn a lot on how to use Ruby by looking at what Rails does with it in order to provide the tools and functionality that you get with rails. This is inevitably going to provide you with a second language as after all you are using commands that have been created using Ruby and those commands are obviously not going to be Ruby commands else Rails would already exist. Please forgive me here as I am thinking as I type. Given that ALL variables are classes then, all instances of classes are objects (an object is an instance of a class that you use) ergo all instance variables are objects. Now it’s a case of understanding what you can do with those objects and how to pass them around. If I am telling you something you already know then sorry but this is helping me. Question – Are all methods functions? That is to say do all methods have the ability to return a value? I think so! An array is far more efficient than a hash (It sais so in the book) but not as flexible (Epihany moment mentioned earlier – Go and gen up on hashs’ and arrays) OK understnading this then learning rails is going to be just a case of learning the functionality that is available. i.e. getting know the common clases that you use and what methods and properties they have plus the syntax for cucking these variables around. If I now go back and look at the way the products were looped through to place them on rthe screen in the Depot app I will know what collection of objects or instance variables have been generated to wrap up the data from the database. Whether those objexts are prepopulated at time of fetching from the datanse or whther they are populated using an internal array at time of usage I don’t need to know at the moment so I don’t really care. I just need to accept that they are there and can be used. I suspect this will become important later when considering performance issues for bottlenecks. Now I’m going to go away and gen up on scope for classes as I think that a protected method or property is the equivalent to a friend in V.B. I also need to really understand the way that the framework (or Rails) behaves and I suspect that routes may be key to this. So more info is needed there. I often find that no amount of drawing or reading can substitute for talking a problem through. |
|
Oct 28, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Just curious, how far along is the Beta? Can’t wait :-) |
|
Oct 28, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Instance Variables for newbies Alan This language will be my 9th language to use in anger and I’m still struggling. The answers are in this book which is great but I’m having a heck of a job knowing how to find them. The trouble is that I KNOW what it is that I want to do but finding the Rails way to do it is very difficult. Personally I would have picked a different language if I wanted to learn programming but it is the route you have chosen and at least you are being exposed to a true OO environment. Finding the Rails Way would be a good book. Where does everything fit? The Depot app example is not the way an experienced developer would write an app. It uses far too much of the auto generated code etc… But it is a good example of how to get started. It has just taken me a week to find out how to write a class properly (I think) but the mist is lifting slowly but it took some digging to realise that what I actually needed to know was the bit on page 665 section A.4 Classes. Having read that section I find myself needing to know more. It’s the nuts and bolts I am interested in. I’m getting there but I’m not there yet. I suspect that when I have found answers to my questions I will have a better way of doing things but it won’t be the best way. Maybe there should be a forum somewhere for newbies to rails to help out each other. Alan, I think we have the same problem but from different ends of the spectrum and that tells me there is definitely something missing somewhere. Maybe what I should be learning is Ruby first then learning the framework of Rails after but I don’t have time for that. But maybe that’s the answer. This book is really really good but I think that if I had Ruby and HTML knowledge first it would be even better for me and possibly for yourself Alan. I don’t think any book can teach you how to program. That is something you learn from experience. It’s a very creative process and everyone has their own style and quirks, and you build up your own style and the way you program evolves as you move on but learning how a language works is paramount. I guess you have to know what it is you are wanting to achieve before you can achieve it and if you want any pointers then ask. I may not know the rails code but I could certainly help point you in the right direction. If anyone can recommend a good Ruby book I would love to tead it :-) |
|
Oct 26, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Instance Variables for newbies There you go you see. |
|
Oct 26, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Instance Variables for newbies You’re not on your own. I am still struggling with where to put things and how variables get passed around I feel as though a pea soup fog has lifted and been replaced with an early morning mist but I can still only see a few feet in front of me. I really want to see some kind of easy to visualise explanation of how to pass variables around and what to do with them. I’m starting to get there though with a little help from peeps in the rails forums. It’s stupid things like how to get an object out of a shared partial, pass it back to the view, then have the views controller take that object and pass it to a shared class in a lib folder for further processing. I’m going through a step by step walkthrough with a thread running on the rails forum that is helping me to understand what should go where and how it should be used. If you phrase the posts properly then people are really helpful and can understand what it is you are trying to get your head round. There is enough info in the book to find out how to do things but it is presented in a way that makes it very difficult to get the basics as you say. Trying to piece it all together is very difficult especially when you start talking about dealing with information that is not stored in a database. |
|
Oct 22, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Custom form builder P.517/518 doesn't make sense to me The following excerpt from the book dos not make sense to me We could then call this method twice in our class definition, once to create a create_tagged_field(:text_field) create_tagged_field(:text_area) But even this contains duplication. We could use a loop instead. [ :text_field, :text_area ].each do |name| create_tagged_field(name) end We can do even better. The base FormBuilder class defines a collection called
class TaggedBuilder < ActionView::Helpers::FormBuilder
# <p>
Description</label><br/>
# <%= form.text_area 'description' %>
#</p>
def self.create_tagged_field(method_name)
define_method(method_name) do |label, *args|
@template.content_tag("p",
@template.content_tag("label" ,
label.to_s.humanize,
:for => "#{@object_name}_#{label}") +
"<br/>" +
super)
end
end
field_helpers.each do |name|
create_tagged_field(name)
end
end
I think I have completely missed the point about how the specific fields get created. Given that the view code looks like this
<% form_for :product, :url => { :action => :save }, :builder => TaggedBuilder do |form| %>
<%= form.text_field 'title' %>
<%= form.text_area 'description' %>
<%= form.text_field 'image_url' %>
<%= submit_tag %>
<% end %>
This code looks to me as if when I call for example <%= form.text_field 'title' %>there will actually be multiple fields generated, one for each type of field_helper in fact as there seems to be no condition in the loop to make sure that the correct type is generated. Whether I have this wrong or not I think a further explanation in the book would be a really good idea. Thanks |
|
Oct 22, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / partitioning / inherit models I’m not entirely sure that would work. In another environment I would have an abstract base class of person and the employee and studemt classes would inherit from this. I think that doing things that way is do-able but not ideal and would need you to re-think the database design somewhat. i.e. having a person type that can be set to either employee or student in the specific relevant model class. I hope this makes sense and I stand to be corrected. Is there another way? There will undoubtedly be another way that will enable you to closer mapyour database in this way but I really don’t know what that way would be and for me to suggest anything would be a case of the blind leading the blind. It might be worth taking a look at helpers as an option for views and it may be that you could have a module that you could mix in with your controllers in order to share code wthout the need to inherit. I only got that idea coz I’ve just been reading page 686 about modules and it seems like it fits |
|
Oct 16, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Still trying to get my head round what belongs where. Help! I think on reflection that I’m going to use a helper and a partial template. That way I can share the helper in whichever controllers need access to it. Having said this I would still appreciate any advice as to whether or not this is the right path to follow |
|
Oct 16, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Still trying to get my head round what belongs where. Help! Hi, What I would like to do is to write a bit of generic code that puts a file_field on a form. The file_field form obviously needs to be multi part as mentioned in the book I also want a bit of code that does different things with the file_field value if it has been set. So I have scoured the book for answers as to how this could be done and come up with several possibilities. The part of the book that talks about file uploading gives this bit of code as an example for the view
<% form_for(:picture, :url => {:action => 'save'},
:html => { :multipart => true }) do |form| %>
Comment: <%= form.text_field("comment" ) %><br/>
Upload your picture: <%= form.file_field("uploaded_picture" ) %><br/>
<%= submit_tag("Upload file" ) %>
<% end %>
What I would want to do is something like
Upload your picture: <%= form.my_file_upload_field("uploaded_picture" ) %><br/>
And I would want to be able to do this in any view. The first thought that sprang to mind was that what I really wanted then was a helper. This helper would need to define a multi part form as it may be used in views that do not have this type of form defined and and as this helper could be used in any view then it would need to live in the application_helper.rb. also taking into account the fact that the helper is not associated with a model then it should be a form_tag rather than a form_for so I came up with something like this in the application_helper.rb <% form_tag(:something? maybe :my_file_upload_field
:html => { :multipart => true }
) do |form|
<%= form.file_field("uploaded_picture" ) %>
<%end%>
This raises questions of what to do with the “uploaded_picture” attribute as this no longer makes sense. O.K. Accepting then that there are satisfaciry answers to these questions I then need to associate the field with a bit of code that will do something with the value set in the my_file_upload_field. So where to put the bit of code that takes whatever action is needed when the user presses a submit_tag button in whatever view is using this? I would need a controller that deals with this me thinks but now I am starting to get really out of my depth. Page 514 starts talking about custom form builders and I start thinking that this is what I need instead or maybe it’s the same thing? There is also mention of working with non model fields on page 519 so now I start thinking that what I really need is some advice on just what it is that I should be doing in order to achoeve my original task. So any help and pointers would be really appreciated especially when it comes to getting the submit action on whtever form is used to call a function that will behave as I want it to based on parameter arguments. This sort of task in any of my tradition OO language backgrouds is really trivial to do. I would just create a new class based on something like a TControl. Create a couple of new properties, add a method to the class and probably add an on error event handler. The form I would use this class in would set the properties of this class in it’s constructor, attach an error event handler call back routine and then when the user presses an appropriate button call the method on the instantiated object to tell it to take whatever action it needs to. Sounds like a lot of work but in reality it’s half a dozen lines of code. Doing this in rails seems to be really complicated which leads me to believe that I have not chosen the right solution and I really know nothing like enough to start cutting proper rails code. so please help :-) Cheers James |
|
Oct 15, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / table joins... Hi, Just a stab in the dark as I haven’t read that chapter yet but here gows in the hope that it helps. I guess your questions lean towards database design rather than ruby code which is probably why your questions have not been answered. Why is the the category_id set to unique? I assume that the purpose of the table is create a many to many relationship between categories and products. To allow products to live in more than one category but for a join table that could cause problems if the index is not set properly. If you set the index on the category field to be unique then you could not have a priduct live in different categories Take the following example With a site selling jewellery for example you might have a situation where you would want categories of bracelets, earrings for example but you migh also want categories of jewellery with gem stones and jewellery without gem stone. A bracelet with a gem stone would want to appear in both categories but this would not be possible if the category index was unique. It all depends on how you want to design your database. The combination of both keys HAS to be unique otherwise you could go on forever and have your product appearing multiple times in the same category. It really is basic database design as I say. A many to many join table has only the keys for the primary indexes on both tables and this combination is ALWAYS unique One of the individual keys needs to be not unique otherwise there is no point to having the join table. Hope that makes sense. It may help to write down a couple of products and a couple of categories on a piece of paper and see what would happen to the join table and whether or not it would prevent combinations that didn’t make sense if the key was unique. You’ll need to do this anyway when you are designing a join table for your own purposes as you will need to work out what the purpose is. I hope I have not rambled on too much and that all is now a little clearer James |
|
Oct 12, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Is it possible to embed Rails code in a style sheet? Thank you :-) |
|
Oct 8, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Lookout It's Another Noob Question I would just delete the folder and it’s contents then start again. |
|
Oct 5, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Help! How to call a method in a model? It’s OK I’ve got it.
module ApplicationHelper
def title(page_title)
content_for(:title) {get_title_from_database || page_title}
end
private
def get_title_from_database
@design = Design.new
@design.get_title_from_database
end
end
Works a treat. Sorry for the silly question! |
|
Oct 5, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / How to selectively require authentication in the depot app? Hi Tate I think if I were doing this from a noob perspective I would probably use some kind of flag as a parameter to the authorize method.
def authorize(need_to_log_in)
if need_to_log_in?
unless User.find_by_id(session[:user_id])
flash[:notice] = "Please log in"
redirect_to :controller => :admin, :action => :login
end
end
end
I’m not sure if I have the syntax correct or even if this would work but it is probably the way I would approach this problem given my limited rails knowledge |
|
Oct 5, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Help! How to call a method in a model? Sorry for the really dumb question but I am still failing to understand some of the fundamentals of rails. I have scaffolded a new table called design with the intention of placing a some configuration data in a single record such as aplication_title. I have placed code in the store.html.erb to call a method in the application_helper and my <title> </title> now looks like this <%= yield(:title) || "Title not set" %><!-- <label id="code.depot.e.title"/> --> My application_helper.rb now looks like this
module ApplicationHelper
def title(page_title)
content_for(:title) {get_title_from_database || page_title}
end
private
def get_title_from_database
"A title from the database"
end
end
If I just put a string constant into the get_title_from_database method everything works a treat so now here comes the biggy I want to call the get_title method that I have in my designs.rb model. How do I do this without getting some sort of uninitialized constant error? Using Delphi,VB,C# and the likes of, I would have a variable of type designs that I would create an instance of, use the method then destroy the instance. But I just can’t seem to get my head round how rails would do this. My design.rb looks like this
class Design < ActiveRecord::Base
def get_title_from_database
Designs.find(:first)
end
end
I know this needs fleshing out to actually return the value of the application_title field but in theory I should be able to call Design.get_title_from_database but it just does not work. I would like my application_helper to look something like this
module ApplicationHelper
def title(page_title)
content_for(:title) {get_title_from_database || page_title}
end
private
def get_title_from_database
Design.get_title_from_database
end
end
However, this gives me the following error
Showing store/index.html.erb where line #1 raised: undefined method `get_title_from_database’ for #<class:0x48550d0> I guess it boils down to a lack of understanding on my part so any help would be greatly appreciated. |
|
Oct 5, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / Is it possible to embed Rails code in a style sheet? The book talks about setting colours in a style sheet (depot.css) and I was wondering if it would be possible to have these colours set from a database table which would presumably need ruby code embedded into the depot.css to call a method maybe in the application_helper.rb that reads colours from a database if it has not already read them? |
|
Oct 5, 2008
James West
70 posts
|
Topic: Agile Web Development with Rails, 3rd Edition / How to selectively require authentication in the depot app? Hi, “Note that this is going too far. We have just limited access to the store itself to You must have missed that part when you were reading the book! lol! |
70 posts
