RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

From what I can tell, creating a theme in Spree is very similar to customizing the application like I showed here. The only difference is that you're working on an engine instead of the application. Check out the blue theme and the Rails Dog Radio theme for good examples.

Avatar

It is a bit slow in development mode, see the performance section in the readme here for how to improve it. Also you may want to try Active Reload.

Avatar

Did you get this to work in the end? I'm having the exact same problem...

Avatar

I really like this screencast but am having problems implementing this because I use haml, besides telling me to switch to erb, is there any advice you can provide about implementation of this with haml. I have googled and there seems to be some suggestions that there are problems with haml, specifically with respect to this issue here https://github.com/nex3/haml/issues/450.

Avatar

I agree this is so interesting and helpful, it would be great to see another one, thank you!

Avatar

I hope to see the in depth how to create an theme for Spree. I guess it would result both, useful for the a particular developing task, but it could really help the spree community to boost on creating various themes for the engine.

However, it was excellent as always.

Thanks Ryan.

Avatar

i cant get sorting to work for text field (not string one)

in model | ruby
searchable do 
  ...
  text :name
end
in controller | ruby
Sunspot.search(Article) do
  ...
  order_by :name, :asc
end

i have an exception:
Sunspot::UnrecognizedFieldError (No field configured for Article with name 'name')

if i reindex with string :name - it become sortable but i loosing possibility to nGram search on that field

did anyone have same issue and got solution to it?

Avatar

There is a bug report with rails 3.1 spree 0.70.1, nokogiri 1.5.0 and ruby-1.9.3-p0 https://github.com/tenderlove/nokogiri/issues/561

And I have this bug ;-)

But for the slowness, you can use RailsDevTweaks

Avatar

Great episode, I would like to see more episodes about rails internals.

Avatar

Thank you.

But did you have some problems with rails 3.1 ? the system seems to be very slow in development mode.

Avatar

Same here. The error happens when i include ActionView::TestCase::Behavior. I'm using rails 3.1.1. The setup action is called inside the Behavior module itself. So, how do i get access to views in RSpec?

Avatar

Great idea! Thanks a lot Ryan!!! Before I put header global variable in a controller (((

For layout:

ruby
...
<title>My Site - <%= yield_or_default(:title, "Default title") %></title>
...

and view:

ruby
...
<% title "My page title" %>
...

It doesn't work on rails 3.1.1

ruby
module ApplicationHelper
  def title(page_title)
    content_for (:title) { page_title }
  end

  def yield_or_default(section, default = "")
    content_for?(section) ? yield(section) : default
  end
end

Solve:

ruby
module ApplicationHelper
  def title(page_title)
    content_for (:title) { page_title }
  end

  def yield_or_default(section, default = "")
    content_for?(section) ? content_for(section) : default
  end
end
Avatar

...and looking at the 'cast again, I'm further conflicted. I'd be inclined to put something like avatar_name in its own partial (or even helper), but neither is satisfactory from an OO point of view, as I mentioned already. At the same time, I wouldn't want to put that much HTML generation in the model, I think, and Cells, while very promising, seems to be trying to solve a different problem (though I could well be wrong). Hmm.

Avatar

Doesn't that logic go in the model?

Avatar

How is putting something in the decorator any better than putting it right in the model class? The Presenter pattern is designed to abstract display logic from one (or several) model objects whose data model isn't much like the view. That's not what's going on here, as far as I can tell. Looks rather like you're reinventing helpers.

Now, there may be some call for doing that: as you point out, helpers are among the least satisfactory parts of Rails from an OO design perspective. But I tend to think this is not the right solution to that problem.

OTOH, I usually trust you, so I'm interested in hearing more about your rationale here.

Avatar

Figured it out, I was passing f instead of builder...

Avatar

After editing all files and run rails s

There will be an error on Ubuntu:

autodetect': Could not find a JavaScript runtime.

After i installed node.js apt-get install nodejs it works.

Greetings

Avatar

10x Matthiew,

I'll try to be more specific. I need to execute some JS snippet on he client side (actually it's executing google analytics code), but I want to run it from a controller on my app (the controller action redirects automatically without rendering html, don't ask why.. :-)

So, I'm not sure whether ExecJS + therubyracer can do that

Thanks.

Avatar

I seem to be getting a "sort_faqs_url" not found error when trying to access my controller sort action. Everything matches the above code perfectly

Avatar

For some reason :child_index isn't working for me in rails 3.0.10. My code is also modified to allow reordering. I am also using formtastic 1.2.4 with haml, and have tried switching the semantic_fields_for to fields_for and have also tried with or without the new_object as an array as a previous commenter noted. Here is the code:

application_helper.rb
def link_to_add_fields(name, f, association)
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.semantic_fields_for(association, [new_object], :index => "new_#{association}") do |builder|
      render(association.to_s.singularize + "_fields", :f => f, :poll_answer => new_object)
    end
    link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')", :class => 'add')
  end
poll_answer_fields.html.haml
.field
  = f.semantic_fields_for :poll_answers, poll_answer do |builder|
    = builder.input :text
    = builder.hidden_field :position
    = builder.hidden_field :_destroy
    = link_to_function "Delete", "remove_fields(this)", :class => 'remove'
    %br
Avatar

When there are naming conflicts, the first path that appears in the config.assets.paths array is the file that is chosen. This can be avoided by using the asset_path() helper and specifying the directory.

Avatar

+1 for follow up episode on PayPal integration!

Great screen cast Ryan, quality is always!

Avatar

My bad. I didn't know that Rails3 session cookies are tamper proof... Wow, that makes my life really easy!

Avatar

Hello Ryan. Thank you for great tutorials! :)

I didn't quite get this comment though. Isn't it what session hijacking is about?

I may be misunderstanding your comment, so please correct me if my basic understanding about the security design is wrong.

  1. If some malicious people gets user_id, he can create a request with session[:user_id] to by-pass login. So, it is always better to use SSL to avoid session cookies to be sniffed out.

  2. To enhance the security further in highly sensitive system, it would be better to use a session id that changes its value according to the login time (unlike user_id)

Avatar

Would have loved to see this working across different lists (moving items from one to the other). I already have something working for this, although I don't think it is very efficient.

Avatar

Usually, your client side will be a web browser that can execute JS code without any third-party library, so ExecJS is not needed in this case (and wouldn't work, since ExecJS needs a Ruby interpreter, that is not available in any standard web browser).

Avatar

Hi, I've ran into an interesting problem which I can't seem to find a solution for. Help is really appreciated.

http://stackoverflow.com/questions/8116314/rails-3-1-paperclip-error-with-custom-processors-and-cropping

Avatar

Hi,

Probably a newbie question, but I want to be sure...Can I use ExecJS to execute JS code on the client side?
Thanks!

Avatar

You mention in your video that Airbrake is a paid service, but there is actually a free version as well: https://airbrakeapp.com/account/new/Free

Avatar

Hi all, i have a problem i followed the tutorial and when I try to sign up through Facebook i get this error:
{
"error": {
"message": "Invalid redirect_uri: Given URL is not allowed by the Application configuration.",
"type": "OAuthException"
}
}

Avatar

I agree. You are doing a great job Ryan!

Avatar

thanks a bunch, can't wait for vagrant + chef screencast

Avatar

Thanks for the kind words.. And yeah, that's the basic idea, but it becomes pretty heavy on the server side and I've intentionally kept that to a minimum (see complaint about formtastic and paperclip above).

You can just adjust the configuration to point to a different route/path when loading the history panel, so it's as simple as writing a view that you want to see. =)

Avatar

So, yeah.. it does have these dependencies, but I will likely drop formtastic -- as it was only used as a convenience for the example snippet options form. I've been meaning to for a while now.

Avatar

I'm actively maintaining the project, and I'm interested in what you mean here.

It may be easier and not get lost if you submit ideas on the github project https://github.com/jejacks0n/mercury -- with some more details.

Avatar

Thanks for advice...

Change should be in app/layouts/mercury.html.erb
saveStyle: null, // 'form', or 'json' (default json)

Changing null to 'form' did solve my problem.

Just to tell because I have been looking in mercury.js for quite a long time :-).

Avatar

I am not sure I am clear on how you would update the view w/ the progress of a job, or the results of a job? Having to refresh is obviously not an ideal implementation, bu it seems like you did that in this screencast as well as the BeadStalkd + Stalker one... Am I missing something ? Do you have any suggestions on how I could update the view automagically when the job is complete?

Thanks!

-John

Avatar

Maybe gem 'turn', :require => false -- it should pretty printe test output

Avatar

I too had an issue with this, you have to do some magic converting the param to a correct JSON format, but the easier solution (at least for Rails) is to use the form method to send the data, instead of the default json method.

Avatar

Simple, add a conditional statement to the line where you include mercury.js. Without this file Mercury never loads, so something like this would solve your issue:

Ruby
<%= javascript_include_tag('mercury') if current_user %>
Avatar

Actually you are right, mercury-rails does have a few conventions (using Formtastic and Paperclip), but Mercury (the editor) doesn't use any of these dependencies, it's only the Rails backend that uses it. But you are free to use any backend you want. The forms for the modals can be customized so you can do without both Formtastic and Simpleform if you want, and I myself use Carrierwave with Mercury without ever lookign at Paperclip.

So I don't see how it is annoying that Mercury includes a few simple examples/conventions for you to get started without almost any coding on the server side.

Avatar

Thank you for your casts, always appreciated!
Any advice about implementing it with a category description and category photo? Any comment is welcome!

Avatar

Hey everyone,

I've implemented a way to change the password (not reset the password) see here.

However I have trouble hooking up rspec tests for this bit of code... If anyone can help me out...

Avatar

Here is how:

ruby
def update_password(current_password, new_password, new_password_confirmation)
  if new_password == new_password_confirmation
    if !User.authenticate(self.username, current_password).nil?
      return self.change_password!(new_password)
    else
      return false
    end
  else
    return false
  end
end
Avatar

I have only just found my love for testing. test unit suits me much better, I tried using rspec for years but we never gelled. Maybe try switching test suites?

Avatar

From now , i'll be the regular visitor of your web. Thanks , great info.

http://www.nsdlpan.co.in

Avatar

Mercury-rails has a dependencies on Formtastic and Paperclip, which I find personally annoying. I tend to use Simpleform and Carrierwave myself, so I'm working on a fork that will remove these dependencies and employ a concept of providers for image-handling. Not sure what to do about Formtastic. It's almost syntactically compatible with Simpleform, but it's use is quite limited. I think a way to delegate options form building to your application would be better, then you'd be free to handle it however you like. All in all, Mercury is awesome, and I'm using it myself. Kudos to Jeremy for sharing it.

Avatar

Hmmm, some good points made in these comments. And I have to say I'm leaning towards Sergey Kuleshov's comment. This is a very cool screencast, but I'm not sure I'd go this far in a production project.

I like that you're showing us Coffeescript usage too Ryan, it's lowering the barrier for me, but I've got to say, I still prefer straight Javascript. I still find Coffeescript very hard to read. Ultimately, I think I'd love to be able to write my Javascript in Ruby.

The similarity of Ruby's new hash syntax is bringing the two closer, and this is something I adopted only this week. Yeah, I know... slow on the uptake.