I'm having issues getting product.pictures.update_attributes() to work sans-javascript.
I have a radio button next to each image on the product editor page, but I am only able to get it to work if i explicitly send a value of 1 or 0 for "is_default" flag for each picture.
I'd like to know if there is a means of doing this w/o requiring javascript.
Ryan: Thanks for another GREAT 'cast. You inspire me to write great code.
Please help to understand: I thought database calls were meant to be in the controller. I understand moving logic to the model, but I thought it was more appropriate to keep calls out of the view and in the controller. Am I wrong here or is best to keep calls in the controller unless the fragment is cached?
After having been working on something like this episode, rails seems unable to render my javascript and I need to update my routes and add the following line:
@Peter D, that's a really good question. I'm not entirely sure why a sweeper behaves differently than an observer. It even has the observe line. My guess is it has something to do with the scope in which it is called. It needs access to the request object which is available to the controller.
@Ben, you can find the snippet here: http://pastie.caboo.se/144239
Maybe someday I'll release a TM bundle with all my snippets, but right now they aren't organized well enough.
As mentioned above, the simple snippet will turn "Franklin Delano Roosevelt" into ["Franklin", "Delano Roosevelt"]. Here's a snippet which takes the last non-whitespace as the last name:<pre><code>
def clean(n, re = /\s+|[^[:alpha:]\-]/)
return n.gsub(re, ' ').strip
end
# Returns [first_name, last_name] (or '' for first name if there isn't one).
# Leading/trailing spaces and non-alpha non-dashes ignored.
def first_last_from_name(n)
parts = clean(n).split(' ')
[parts.slice(0..-2).join(' '), parts.last]
end</code></pre>
However, as someone who can't check in at the automatic kiosks in airports because the credit card thinks my last name is "IV", I <a href="http://vizsage.com/blog/2008/01/parsing-names-with-honorifics.html">wrote a version that works with honorifics</a> (like 'Esq.' or 'Jr.'): http://vizsage.com/blog/2008/01/parsing-names-with-honorifics.html
I noticed that if my application is set to use a layout (in my case 'xhtml') I will run into this error message when attempting to see the rss format:
Missing layout layouts/xhtml.rss.erb in view path
Is there a clean DRY way to tell rails to not use layouts on certain formats? The only way I could think of was to go back to the old not very DRY respond_to stuff. Or just creating a one line yeilding layout but that seemed more like a work around.
Hi, I've been googling about a week to solve a ruby-openid problem. And found a working example on Rama McIntosh's blog. http://myutil.com/2007/12/29/openid-2-0-2-with-rails-2-0-2
At first, I thought rails Ticket #10604 patch worked. But it was more complicated than that. Since Rails, ruby-openid, and open_id_authentication plugin versions were not consistent. And Rama McIntosh went through all. You can find Dr. Nick’s example application fully ported to rails 2.0.2.
Here is my development environment.
------------------------------------------------------------
LG LW25 advanced laptop IntelCore2 T5600, Mem 2G
XP Pro version2002 SP2
Cygwin + CygPutty
Ruby ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-cygwin]
Rails 2.0.2
ruby-openid (2.0.3)
rake (0.8.1)
mongrel (1.1.3)
mongrel_cluster (1.0.5)
ruby-yadis (0.3.4)
app db : sqlite3
------------------------------------------------------------
Besides his README, some extra typing was needed to run the app.
1. svn co http://drnicwilliams.com/svn/openid/demos/apps/openidauth_multiopenid/trunk openidauth_multiopenid
2. Do not erase some svn directories because he made some rake tasks related with svn.
3. rake freeze TWICE!
4. mkdir log
5. require_gem => gem
openidauth_multiopenid/vendor/gems/ruby-openid-1.1.4/lib/openid/service.rb:7
/home/yunsoo/auth_openID/openidauth_multiopenid/vendor/gems/ruby-openid-1.1.4/lib/openid/discovery.rb:8:
6 rake db:create
7. gem install mocha
8. rake test
9. config/environment.rb
config.action_controller.session = { :session_key => "_openidauth_multiopenid_session", :secret => "mocramagic" }
Just a quick question. When you use the shortcut for displaying partials:
render :partial => @products
does it make any assumptions about where the partial is stored? I have a feeling it assumes the partial is stored in the controller with the same name.
Thanks. I purchased peepcode stuff too so advertising works... My question on this has to do with copying the yml file to the config/database_example.yml file. Isn't it still the the project directory that you commit? I looks like this step just changes the name in the same dir?
I'm really running into road block after road block trying to figure out how to get associated validation working properly. At this point all I can get is an error message that says 'Model is invalid' rather than the preferred child-model's attribute error message.
We really need a screen cast on how to get this kind of validation working.
Thank you for the great bite sized tutorials. I've been trying to figure out subversion for awhile. To all who want to practice this with a free remote subversion service, check out: http://www.beanstalkapp.com/ You can setup of a free account that comes with 1 repository. I followed this tut using beanstalk. Worked great!
Scrap that - folks make sure you've got rails (including environment.rb) updated to latest and got your javascripts (as mentioned above updated to latest) using the command:
rake rails:update:javascripts
Similar to Peter I'm having a few problems implementing this.
With implementation identical to yours navigating to
http://0.0.0.0:3000/javascripts/dynamic_show_requirements.js
renders:
"Missing template ....requirements.rhtml"
(nb. filename is "...requirements.js.erb")
Any quick tips as to how to get this moving?
Rails v 2.0.2 and Ruby v1.8.6
This may be a really dumb question but since the sweeper observes the State model, why is it necessary to also declare the cache_sweeper in the controller?
Ryan, your 'casts always get me revved up for a long day of refactoring. You don't talk down to your audience and you don't have a huge rub-ego, and that keeps me coming back for more. I even bought a couple Peepcodes! Well done sir, and keep them coming.
I'd like to ask sandro.d#s question agian in a slightly different form
I'd like the 'view' and 'edit'/'new' to look the same. Same layout and laels & screen position. So I'd like to use the same ... well, its not really a _form now is it? Or is it? And how would you address it in 'view.rhtml.erb'?
Hi,
I followed this using Rails 2.0.2 and Ruby 1.8.6. I had to use:
"generate authenticated user sessions" to get the SessionsController.
Then it works fine and no changes of authenticated_system.rb needed. With
"generate authenticated user session" I got stuck with a Name Error:
uninitialized constant SessionsController
Thanks for putting this up. Dynamic selects menus without buggy js plugins is exactly what I've been looking for. I'm having some trouble implementing though.
myhost/javascripts/dynamic_states.js renders normally. The script seems to load in the 'new' action given the html source of my view, though I noticed your helper method doesn't spit errors when given non-existent javascripts. The select id tags in dynamic_states line up with the ones in my the view. Still no action upon selecting country though. Any idea what I might be missing? I assume this works with remote_form_for?
Ryan, what about some validation examples? For example, I'm validating for :company_id given one of the companies from the drop-down are selected, however, I don't want to run the validation if new_company_name is present. This doesn't seem to work:
I follow the tutorial and everything worked great. My only problem is that with what I am I add a new "task"(picture in my case) it overwrites the previous picture. If I add more than one picture, it overwrites previous pictures and adds the new ones. Any ideas on how to fix this? it works great if I don't wrap the file_field in an if statement, but I really don't want it to show up before a picture that has already been submitted....just want the ability to remove. Thanks
I haven't seen it asked yet and I've done some google-ing and haven't come up with a result either...
I want a authenticated? / logged_in? method. Because in my views I want the edit | delete links visible for the authenticated user but obviously not the general public...
I followed this using Rails 2.0.2 and found I had to alter line 67 of authenticated_system.rb from:
redirect_to new_session
to
redirect_to :controller => 'session', :action => 'new'
... in order to avoid the "undefined local variable or method `new_session'" error.
I need to write the feed to a file and others will pick that file ( this file is public).
One way to do is to do page caching. I could also use curl but I would like to store the output of builder in string and then store the file right there rather than using curl or anything.
How can I get the output of the builder in a string.
Great cast as always.
1) PLease can you tell me where the best place to find documentation on this feature is (rails api)
2) I'm not quite sure how this would work since it's likely that the partial for an index will need to be different for a partial from the show. The index view is likely to be much briefer than show view.
Any thought about how to handle security related issue such as mass assignment? Is there a clean way to combine your approach with using attr_accessible?
Does anyone have any insight into how to adapt this example for has_many :through associations? The build aspect of this example breaks it -- I've poste dabout it here -- http://railsforum.com/viewtopic.php?id=14580
I'm having issues getting product.pictures.update_attributes() to work sans-javascript.
I have a radio button next to each image on the product editor page, but I am only able to get it to work if i explicitly send a value of 1 or 0 for "is_default" flag for each picture.
I'd like to know if there is a means of doing this w/o requiring javascript.
You hit another one out of the park with this one. Simple, concise, easy to follow.
I Like.
Another great screencast.
I still have a question, though. Is caching something I should test? How would I do this?
Ryan: Thanks for another GREAT 'cast. You inspire me to write great code.
Please help to understand: I thought database calls were meant to be in the controller. I understand moving logic to the model, but I thought it was more appropriate to keep calls out of the view and in the controller. Am I wrong here or is best to keep calls in the controller unless the fragment is cached?
Thanks again for your great screen casts.
Bryce
@heip, that would work too. I still prefer to place the find in the view, but it's subjective.
@Michel, it's working for me. Is it still down for you?
Link is down for me:
http://media.railscasts.com/videos/090_fragment_caching.mov
Sorry, it's not a great article but a great rails screencast. :)
After having been working on something like this episode, rails seems unable to render my javascript and I need to update my routes and add the following line:
<code> map.connect ':controller/:action.:format'</code>
Great article though...
What about:
controller:
unless read_fragment("recent_products")
@product = Product.find_recent
end
Does anyone know if these instructions work with the new version of the ruby-openid gem (version 2.0.3)?
This subject is one of the most important if you plan to have a dynamic and responsive web site.
Most of programmers come to this point late in the development process.
As usual Ryan is right on the spot, so thanks once again.
@Peter D, that's a really good question. I'm not entirely sure why a sweeper behaves differently than an observer. It even has the observe line. My guess is it has something to do with the scope in which it is called. It needs access to the request object which is available to the controller.
@Ben, you can find the snippet here: http://pastie.caboo.se/144239
Maybe someday I'll release a TM bundle with all my snippets, but right now they aren't organized well enough.
Oof -- I tried pasting in code and it came out unreadable. Sorry.
For a method that will extensibly handle multiple names and names with honorifics like 'Esq.' or 'Jr.', please see:
http://vizsage.com/blog/2008/01/parsing-names-with-honorifics.html
As mentioned above, the simple snippet will turn "Franklin Delano Roosevelt" into ["Franklin", "Delano Roosevelt"]. Here's a snippet which takes the last non-whitespace as the last name:<pre><code>
def clean(n, re = /\s+|[^[:alpha:]\-]/)
return n.gsub(re, ' ').strip
end
# Returns [first_name, last_name] (or '' for first name if there isn't one).
# Leading/trailing spaces and non-alpha non-dashes ignored.
def first_last_from_name(n)
parts = clean(n).split(' ')
[parts.slice(0..-2).join(' '), parts.last]
end</code></pre>
However, as someone who can't check in at the automatic kiosks in airports because the credit card thinks my last name is "IV", I <a href="http://vizsage.com/blog/2008/01/parsing-names-with-honorifics.html">wrote a version that works with honorifics</a> (like 'Esq.' or 'Jr.'): http://vizsage.com/blog/2008/01/parsing-names-with-honorifics.html
Please post the sweeper snippet!
Sorry, just noticed my question had already been asked. Must have mistyped the ctrl+f in firefox searching for layout ;)
I noticed that if my application is set to use a layout (in my case 'xhtml') I will run into this error message when attempting to see the rss format:
Missing layout layouts/xhtml.rss.erb in view path
Is there a clean DRY way to tell rails to not use layouts on certain formats? The only way I could think of was to go back to the old not very DRY respond_to stuff. Or just creating a one line yeilding layout but that seemed more like a work around.
If I have 100 pages how do I display pages 1 through 5 then .... and the last 5, 95 through 100?
Here's a HAML version: http://pastie.textmate.org/143587
Taking out "countrySelected();" in the amended javascript makes this work for my app on rails 2.0.2.
I would like to filter the states list by the pre-selected country though. What might I be missing?
For all the folks with "is not allowed as an instance variable name"... make sure you've got the ":object => Xxx.new" in this bit:
page.insert_html :bottom, :tasks, :partial => 'task', :object => Task.new
That was my problem, maybe its yours.
Hi, I've been googling about a week to solve a ruby-openid problem. And found a working example on Rama McIntosh's blog. http://myutil.com/2007/12/29/openid-2-0-2-with-rails-2-0-2
At first, I thought rails Ticket #10604 patch worked. But it was more complicated than that. Since Rails, ruby-openid, and open_id_authentication plugin versions were not consistent. And Rama McIntosh went through all. You can find Dr. Nick’s example application fully ported to rails 2.0.2.
Here is my development environment.
------------------------------------------------------------
LG LW25 advanced laptop IntelCore2 T5600, Mem 2G
XP Pro version2002 SP2
Cygwin + CygPutty
Ruby ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-cygwin]
Rails 2.0.2
ruby-openid (2.0.3)
rake (0.8.1)
mongrel (1.1.3)
mongrel_cluster (1.0.5)
ruby-yadis (0.3.4)
app db : sqlite3
------------------------------------------------------------
Besides his README, some extra typing was needed to run the app.
1. svn co http://drnicwilliams.com/svn/openid/demos/apps/openidauth_multiopenid/trunk openidauth_multiopenid
2. Do not erase some svn directories because he made some rake tasks related with svn.
3. rake freeze TWICE!
4. mkdir log
5. require_gem => gem
openidauth_multiopenid/vendor/gems/ruby-openid-1.1.4/lib/openid/service.rb:7
/home/yunsoo/auth_openID/openidauth_multiopenid/vendor/gems/ruby-openid-1.1.4/lib/openid/discovery.rb:8:
6 rake db:create
7. gem install mocha
8. rake test
9. config/environment.rb
config.action_controller.session = { :session_key => "_openidauth_multiopenid_session", :secret => "mocramagic" }
Replace to other key, secret like this,
config.action_controller.session = {
:session_key => '_foo_session',
:secret => '4747ba80asdfqwertwertwee5y4365345ertf gadrtvrwebterwvd653b65tyt93c45cb53242eb43'
}
10. mongrel_rails start!!
Peter D and Garrett Heaver:
I don't necesseraly wants to do the sweeping job in every controller. It's a matter of features and needs.
So if you want to enable the sweeping jog, you have to tell it in the given controller using cache_sweeper.
Peter D, I was wondering the same thing? Are we missing something?
Just a quick question. When you use the shortcut for displaying partials:
render :partial => @products
does it make any assumptions about where the partial is stored? I have a feeling it assumes the partial is stored in the controller with the same name.
To illustrate, imagine a directory like such:
views/products/_product.html.erb
views/users/_product.html.erb
When rendering by doing:
render :partial => @product
the shortcut automatically gets the partial from views/products, even though the render may be in the users controller.
However, this problem does not occur when doing it the long way:
render :partial => 'product', :object => @product
Any comments?
Hey Ryan, these railcasts are a great help to me. Is there any chance that you could do one with a search entry form that is not part of the index?
Ryan,
Thanks. I purchased peepcode stuff too so advertising works... My question on this has to do with copying the yml file to the config/database_example.yml file. Isn't it still the the project directory that you commit? I looks like this step just changes the name in the same dir?
I'm really running into road block after road block trying to figure out how to get associated validation working properly. At this point all I can get is an error message that says 'Model is invalid' rather than the preferred child-model's attribute error message.
We really need a screen cast on how to get this kind of validation working.
Thank you for the great bite sized tutorials. I've been trying to figure out subversion for awhile. To all who want to practice this with a free remote subversion service, check out: http://www.beanstalkapp.com/ You can setup of a free account that comes with 1 repository. I followed this tut using beanstalk. Worked great!
Scrap that - folks make sure you've got rails (including environment.rb) updated to latest and got your javascripts (as mentioned above updated to latest) using the command:
rake rails:update:javascripts
Thanks Ryan!
Similar to Peter I'm having a few problems implementing this.
With implementation identical to yours navigating to
http://0.0.0.0:3000/javascripts/dynamic_show_requirements.js
renders:
"Missing template ....requirements.rhtml"
(nb. filename is "...requirements.js.erb")
Any quick tips as to how to get this moving?
Rails v 2.0.2 and Ruby v1.8.6
Thanks for the podcast! :)
This may be a really dumb question but since the sweeper observes the State model, why is it necessary to also declare the cache_sweeper in the controller?
Ryan, your 'casts always get me revved up for a long day of refactoring. You don't talk down to your audience and you don't have a huge rub-ego, and that keeps me coming back for more. I even bought a couple Peepcodes! Well done sir, and keep them coming.
I'd like to ask sandro.d#s question agian in a slightly different form
I'd like the 'view' and 'edit'/'new' to look the same. Same layout and laels & screen position. So I'd like to use the same ... well, its not really a _form now is it? Or is it? And how would you address it in 'view.rhtml.erb'?
Hi,
I followed this using Rails 2.0.2 and Ruby 1.8.6. I had to use:
"generate authenticated user sessions" to get the SessionsController.
Then it works fine and no changes of authenticated_system.rb needed. With
"generate authenticated user session" I got stuck with a Name Error:
uninitialized constant SessionsController
Thanks for putting this up. Dynamic selects menus without buggy js plugins is exactly what I've been looking for. I'm having some trouble implementing though.
myhost/javascripts/dynamic_states.js renders normally. The script seems to load in the 'new' action given the html source of my view, though I noticed your helper method doesn't spit errors when given non-existent javascripts. The select id tags in dynamic_states line up with the ones in my the view. Still no action upon selecting country though. Any idea what I might be missing? I assume this works with remote_form_for?
This episode is not showing in the "Full" podcast, its at episode 88. However, the iPod feed is current.
maybe you should release a TM Bundle with all your nifty snippets in :-)
Ryan, what about some validation examples? For example, I'm validating for :company_id given one of the companies from the drop-down are selected, however, I don't want to run the validation if new_company_name is present. This doesn't seem to work:
validates_presence_of :company_id, :unless => new_company_name?
Any ideas why?
Cheers, GREAT tutorials mate, keep it up! : )
Hi Ryan,
Not sure why, but i'm having trouble with this line:
svn import . svn://localhost/blab -m "initial import" --username
I'll put every username / password (perm) i can think of under the sun and all i get is:
svn: Can't connect to host 'localhost': Connection refused
dam! I'm running a fresh install of rail in leopard with mongel & mysql (no apache).
any ideas?
I follow the tutorial and everything worked great. My only problem is that with what I am I add a new "task"(picture in my case) it overwrites the previous picture. If I add more than one picture, it overwrites previous pictures and adds the new ones. Any ideas on how to fix this? it works great if I don't wrap the file_field in an if statement, but I really don't want it to show up before a picture that has already been submitted....just want the ability to remove. Thanks
<div class="picture">
<% fields_for "housing[picture_attributes][]", picture do |pic_form| %>
<p>
<% if picture.new_record?%>
Upload A picture:
<%= pic_form.file_field :uploaded_data , :index => nil %>
<%= link_to_function "remove", "$(this).up('.picture').remove()" %>
<%end%>
<% unless picture.new_record? %>
<img src="<%=picture.public_filename(:thumb)%>" />
<%= link_to_function "remove", "mark_pic_for_destroy(this)" %>
<%= pic_form.hidden_field :id , :index => nil %>
<%= pic_form.hidden_field :should_destroy , :index => nil, :class => 'should_destroy' %>
<%end%>
</p>
<% end %>
I haven't seen it asked yet and I've done some google-ing and haven't come up with a result either...
I want a authenticated? / logged_in? method. Because in my views I want the edit | delete links visible for the authenticated user but obviously not the general public...
Does anyone have any ideas? Thanks in advance.
I followed this using Rails 2.0.2 and found I had to alter line 67 of authenticated_system.rb from:
redirect_to new_session
to
redirect_to :controller => 'session', :action => 'new'
... in order to avoid the "undefined local variable or method `new_session'" error.
This was great. Just wondering if there is a way to do this without using REST?
My app is not as clean as this one at this point and I am lost when it comes to getting the "collection" of data passed to the controller method.
I have a question about your archive path.
why and how do you use the semicolon between episodes and archive in the path?
rc.com/episodes*;*archive
I need to write the feed to a file and others will pick that file ( this file is public).
One way to do is to do page caching. I could also use curl but I would like to store the output of builder in string and then store the file right there rather than using curl or anything.
How can I get the output of the builder in a string.
Great cast as always.
1) PLease can you tell me where the best place to find documentation on this feature is (rails api)
2) I'm not quite sure how this would work since it's likely that the partial for an index will need to be different for a partial from the show. The index view is likely to be much briefer than show view.
Thanks for any tips
Anthony
Any thought about how to handle security related issue such as mass assignment? Is there a clean way to combine your approach with using attr_accessible?
Thank you Ryan, that's one more neat screencast which came exactly when I needed it most.
Unfortunately there is at least one major issue:
What if you want to edit the stuff later? The child item won't be select, you have to choose again :(
Does anyone have any insight into how to adapt this example for has_many :through associations? The build aspect of this example breaks it -- I've poste dabout it here -- http://railsforum.com/viewtopic.php?id=14580
any help would be awesome!