RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Thanks Ryan. As usually your great tutorial helped me shave off those precious hours!

Avatar

Oh, and one other thought, what about adding a new symptom/problem that doesn't exist yet? I think you did a railscast on adding a has_many like that, but how about a many-to-many?

Avatar

Hi,

can somebody help me to test this:
page[:review_name].value = "this is cool"
 with assert_select_rjs, or any other method will be appreciated.

Thanks

Avatar

How to build a form without a model linked to it? It can be done with this gem?

Avatar

One thing I'd love to see is what if your symptoms model had been a full fledged model with additional attributes that couldn't be placed on either of the joined models? Say it had a "cured_on" attribute for when that particular problem was cured. How would you handle that?

Avatar

Love it! I was just wondering about many to many relationships. I just have one question: what about when the list is too large to put in a list box or check boxes?

For example, I have a large database of names and phone numbers, and people can share phone numbers and also have many different phone numbers. Some sort of autocomplete and ajax addition would be interesting.

Avatar

Formtastic is great, but if you don't want your forms so tightly linked to your models, check out the Informant plugin/gem:

http://github.com/alexreisner/informant

Avatar

And I was just looking for Formtastic tutorials when I stumbled upon this. Great as usual, Ryan!

Avatar

@Frederic, thanks for mentioning the typo, fixed now.

@Todd, Formtastic is not tied to the URL, so you are free to use a permalink instead of an id in there and it won't conflict.

@cbmeeks, planning to solve the spam problem soon, thanks!

Avatar

Awesome as usual but do you think you could put a better spam filter on your site?

There are tons of spam posts in your comments.

Keep up the great work!

Avatar

I followed step by step as you explained in the tutorial. Its working for me till the form in crop view. I can choose the dimensions I want, but when click submit it don't crop the image. It gives the same only the small version(default thumbnail).

I get an error in the terminal,which is

[paperclip] An error was received while processing: #<Paperclip::PaperclipError:
 There was an error processing the thumbnail for paperclip-reprocess,4716,0>
Please help me with this issue.
Thanks

Avatar

Hello Ryan!
I could not find a better way to start my weeks than watching your railscasts!
Congratulations!
Luciano

Avatar

Great episode. In episode 63, you discussed permalinks. If you're using some name as opposed to the id number for seo, is it possible to use formtastic and how would you do that?

Avatar

Hi ryan. Once again grt screencast. I love it and going to use it on me next small rails project.

Avatar

Really good episode!! I love Monday mornings with a new Railscasts episode ;)

Avatar

One small typo in the models/symptom.rb file: the symptom belongs to an :animal, not an ":animial" :-)

Thanks a lot Ryan for all of your screencasts.

Avatar

Very nice one, Ryan. Was waiting a week for this one :) Keep up the good work

Avatar

Nice one Ryan! I recommend you all to checkout the Formtastic I18n features, they are very flexible. Not only useful for internationalization, but also usability. Peep the README. =)

Avatar

great! exactly what I need for my current project.

thanks Ryan!

Avatar

Thanks Ryan, been waiting for this!

Avatar

@Rick DeNatale:

Gemcutter is now default. =)

Avatar

To everyone having the same problem I had, that is, at no time the table for sessions was created in the db and I kept having this [not so clear] error: "no credentials provided" when trying to login. You should just create the table. Or at least this did the trick for me:

/db/migrate/migration2.rb

class CreateSessions < ActiveRecord::Migration
  def self.up
    create_table :sessions do |t|
      t.string :session_id, :null => false
      t.text :data
      t.timestamps
    end

    add_index :sessions, :session_id
    add_index :sessions, :updated_at
  end

  def self.down
    drop_table :sessions
  end
end

ps: it might sound lame, but everyone should get the chance to start from the scratch...

Avatar

I just tried out the formtastic textmate bundle, and notice that the ftgem snippet still generates a source option for the gem as github.

I sent a message to the author of the bundle via git about that. If there's time, you might want to mention this in the next railscast.

Avatar

I followed you guide and its amazing, thank you very much.
But i found some problems when trying to loggin in.
If I choose an openid that doesnt exists, it fails for "Template is missing" in the user_sessions/create.erb.
And if I try to login with a wrong username/password, it no longer shows the error message about it, just reloads the view.
Any idea how could I fix this?

Avatar

I'm trying to alter the code so that an "comment" is automatically associated with the "user" that created it. I was able to associate an article with its creator, but when I tried the same process for comments, I get a nil error on the user when I access comment.user.username.
 
----------------Steps--------------
1)To the migration script I added: "add_column :comments, :user_id, :integer"

2) To comments.rb I added: "belongs_to :user"

3) To user.rb I added: "has_many :comments, :dependent => :destroy"

4) To comments_controller.rb I added: "@comment.user = current_user" to the "new" method
4) To app/views/comments/_form.html.erb I added: "<%= f.hidden_field :user_id %>"

5) To app/views/articles/show.html.erb I add: "<%=h comment.user.username %>" to access the username of the poster.
----------------------------------

However I get an error from step #5

Clearly comment.user is not being set, when I create a new comment but I don't understand what I am doing wrong because these are the same steps that were used to automatically to a assign a comment to its article, and an article to the user?

Avatar

actually, this IS useful for multipage forms

even if a instance variable is not shared among requests, you can progressively put/retrieve attributes in the session, and step by step updating a "step" attribute (a virtual attribute of the model), then firing validations only for the correct steps

I noticed that apart save(false) you can check the validity of the attributes with model_instance.valid?

Avatar

Nice episode, but this spam is really annoying :(
Add some captha please

Avatar

I got the same problem as Markus

Missing template products/_product.erb in view path app/views

so I created the partial _product.erb as the following

<% form_for @product do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p><%= f.submit "Submit" %></p>
<% end %>

.. and now I get the error below

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

When I display in the debugger for the view search/show the content of @search.products I got the correct entries in the array. It seems like the partial cannot link it to the variable @product.

I am using Rails 2.3.4 and I tried also to pass the parameters :collection and/or :as etc. when calling the partial but I got always the same error.

I also tried to call the partial products/index which expects a collection @products and still the same error.

Please help me I am new to Rails and I am really stuck with this variable passing between the view of one controller (searches/show) and the partial of another controller (products/_product.erb.)

Cheers,
Houcine

Avatar

is there any way to make footers work when printing multi-page table?

Avatar

Hi

How can I determine if a job is finished ? Ideally there would be a way for my rails app to - upon request, determine if a specific job is complete. It seems to me that in order to do that you would need to get some unique id upon creating a delayed job, return that to the user - who could then pass that id to the app and ask 'is "this" (aka insert delayed job id here) job done yet ?"

Is this possible and if so - how ?

Thanks

Avatar

thanks for this cast, Ryan, it made my day a few weeks ago.

How would you do this with Ajax, though?

Avatar

thank. your article is succesful

Avatar

For anyone who has yet to refactor their AJAX and run into Javascript conflicts,
you can use:

jQuery.noConflict();

At the head of your file and then use

jQuery instead of $ for your commands and it won't conflict with other AJAX libraries.

Avatar

Nice cast!

@nick:

Validation error messages is attached to the fields instead all in one huge block, and you can render general form errors using errors[:base] - pure Rails.

FYI: I'm a heavy Formtastic user and created a gem that handles client-side (and fallback on AJAX when client-side not possible) validations of forms by extracting needed validation info from your models. Formtastic markup in mind, so it's compatible out of the box. Actually compatible with any markup if you want. Check it out:

http://github.com/grimen/validatious-on-rails

Unobtrusive and JS framework agnostic. =)

Avatar

Great podcast, Ryan. I was wondering why this wouldn't work if the Product model has a has_one association to the Category model and the Category model has a belongs_to to the Product.

I've tested both ways and It works the way you have it, but was just curious if you knew why this other way would not work.

Avatar

Hey Ryan,
Great job as usual. Please make sure to cover Formtastic in at least 3 or 4 episodes. It is that good.

Avatar

Great comparison between traditional scaffold and formtastic.
I would like to see how validation works in formtastic.

Avatar

I was thinking about something very much like this last night. The difference is that I was thinking of something that would loop through all existing models in a project and generate the forms. This looks great on its own, but also looks like a fantastic starting point for a generate-new-forms plugin!

Avatar

Just wondering...

Is it possible to have a text field associated with each checkbox? What if the form creates an entry in the join table, but also should set a property in the jointable/join object itself? As an example, there is a free form text field for notes on the choice made by marking the checkbox.

Avatar

Shreyans,

Post your question on www.stackoverflow.com. You'll probably get an answer more quickly than here :)

Avatar

If you're running Rails 2.3.x and the latest version of subdomain-fu, make sure you set

SubdomainFu.override_only_path = true

in your initializer or else passing :subdomain into URLs won't work.

Avatar

I know it is not good to ask this question here, but I can't stop myself from doing this coz I need this function immediately.

Does anyone know how to send formatted output to printer using Ruby?
Or
Is there a way to send a formatted pdf document to a printer using Ruby?

I need suggestions. Please help.
Ryan Bates rocks....

Avatar

Cute but perhaps a bit gratuitous. I prefer to use form builders wrapped in a helper method. Cheers.

Avatar

I noticed that if you use a select that joins the users table and the comments table, and create a comments_count attribute for accessing the number of comments for each user, there is a problem:

If that user has no comments, they will not be selected. Is there an obvious way around this? I imagine the select method just shouldn't be used if you want to find users who have not commented....

Avatar

If not covered in part two, maybe a part three covering the usage of Searchlogic and Formtastic in tandem?

Thanks for the work.

Avatar

@Tair you can do a min and max price search by using a range:

:with => {:price => 10..100}

Avatar

Ryan,

I hang out monday afternoons (aus time) waiting for your rails casts and it is never a dissapointment... awesome work on this one... i can't wait to use it for our projects.