I'm trying to build Rails blogger site following a tutorial, but I'm not seeing the expected web page. I've done the following:
1) I've modified the config/routes.rb file to look as follows:
Blogger::Application.routes.draw do
resources :articles
end
2) In db/migrate, there is a "_create_articles.rb" which I've modified to the following:
class CreateArticles < ActiveRecord::Migration
def change
create_table :articles do |t|
t.string :title
t.text :body
t.timestamps
end
end
end
In the console, I've defined several instances of the "Article" class, assigned "title" and "body" attributes, and saved them, which I confirmed by calling "Article.all" in the console:
irb(main):001:0> Article.all
Article Load (1.7ms) SELECT "articles".* FROM "articles"
=> #<ActiveRecord::Relation [#<Article id: 1, title: "Sample Article Title", body: "This is the text for my article, woo hoo!", created_at: "2016-06-17 18:18:33", updated_at: "2016-06-17 18:18:33">, #<Article id: 2, title: "This is the second sample article", body: "Text for the second sample article", created_at: "2016-06-17 18:19:35", updated_at: "2016-06-17 18:19:35">, #<Article id: 3, title: "Third article!", body: "Lorem ipsum", created_at: "2016-06-17 18:20:01", updated_at: "2016-06-17 18:20:01">]>
I've also started the Rails server using the "rails server" command in project directory. However, if I go to localhost:3000 I still see the "Welcome aboard" screen (shown below), whereas I would expect at this point to see an error message "Unknown action - The action 'index' could not be found for ArticlesController".
In fact, I have been at this point before in the tutorial, but I've since closed everything and reopened it, and now I no longer see the website I expect, or any error message. Any ideas what could be amiss?
You need to set a root page in your routes.rb file to get rid of that page in development environment.
Blogger::Application.routes.draw do
resources :articles
root 'articles#index'
end
Related
I have a spec (Rspec 2.13.1 and rails 3.2.19) and am getting the following error
1) ApiMenusController task0041: test saving new menu will create new menu
Failure/Error: post :create, {is_new: "true", location_id: lucques.id, name: "my newer jtjt menu"}
ActiveRecord::UnknownAttributeError:
unknown attribute: attributes_changes
# ./app/models/save_app_event.rb:37:in `before_save'
My AppEvent looks like this:
class AppEvent < ActiveRecord::Base
attr_accessible :loggable_id, :loggable_type, :user_id, :event, :attributes_changes, :location_id, :menu_id
It is called in a before_save
class SaveAppEvent
def self.before_save(model)
Rails.logger.info("within SaveAppEvent with a " + model.class.to_s)
tmp=model.changes.except(:admin_frag).except(:menu_item_frag)
if model.class.to_s=='Menu'
Rails.logger.info("within SaveAppEvent with menu.location_id #{model.location_id}")
tmp_menu_id=model.id
tmp_menu_id=0 if !tmp_menu_id
tmp_location_id=model.location_id
event="a menu was saved with location_id: #{tmp_location_id} "
#Rails.logger.info("within SaveAppEvent with name location_id: #{location_id} and menu_id: #{menu_id}")
else
menu_id=nil
location_id=nil
end
Rails.logger.info("within SaveAppEvent right before creation location_id: #{tmp_location_id} and menu_id: #{tmp_menu_id} and attributes_changes: #{tmp}")
AppEvent.create event: event, menu_id: tmp_menu_id, attributes_changes: tmp, user_id: model.current_user_id, loggable_type: model.class.name, loggable_id: model.id
end
The info is there:
within SaveAppEvent right before creation location_id: 95 and menu_id: 0 and attributes_changes: {"location_id"=>[nil, 95], "name"=>[nil, "my newer jtjt menu"]}
For some reason, in this context, it doesn't think attributes_changes exists. On development, it works fine. Why would test environment see attributes on an object differently?
I start to using pry in a rails console.
When I get a instance of a Rails model, the values are shown without line breaks like this:
pry(#<Class:0x1022f60e0>):1> first
=> #<Article id: 1, name: "What is Music", content: "Music is an art form in which the medium is sound o...", created_at: "2011-08-24 20:35:29", updated_at: "2011-08-24 20:37:22", published_at: "2011-05-13 23:00:00">
from http://railscasts.com/episodes/280-pry-with-rails?view=asciicast
Is there way to show the values with line breaks like this?
Article
id: 1
name: "What is Music"
content: "Music is an art form in which the medium is sound o..."
created_at: "2011-08-24 20:35:29"
updated_at: "2011-08-24 20:37:22"
published_at: "2011-05-13 23:00:00"
You could call .to_yaml on the model instance! It returns a string that's formatted almost exactly like you're requesting it to be.
Here are some examples of to_yaml output:
http://yaml4r.sourceforge.net/doc/page/examples.htm
I would recommend that you install awesome_print.
Add it to your Gemfile:
group :development do
gem 'awesome_print'
end
And install it with bundle install.
Now use ap to print it in the console:
pry(#<Class:0x1022f60e0>):1> ap first
#<Article:0x1022f60e0> {
:id => 1,
:name => "What is Music"
:content => "Music is an art form in which the medium is sound o..."
:created_at => "2011-08-24 20:35:29"
:updated_at => "2011-08-24 20:37:22"
:published_at => "2011-05-13 23:00:00"
}
I think, the below trick will work for you.
arup#linux-wzza:~/Rails/model_prac> rails c
Loading development environment (Rails 4.1.4)
2.1.2 :001 > Comment.first
Comment Load (0.4ms) SELECT "comments".* FROM "comments" ORDER BY "comments"."id" ASC LIMIT 1
=> #<Comment id: 1, value_old: "I am a good Boy.", value_new: "I am a bad Boy.", created_at: "2014-08-02 17:36:14", updated_at: "2014-08-02 18:21:42">
2.1.2 :002 > y Comment.first
Comment Load (0.4ms) SELECT "comments".* FROM "comments" ORDER BY "comments"."id" ASC LIMIT 1
--- !ruby/object:Comment
attributes:
id: 1
value_old: I am a good Boy.
value_new: I am a bad Boy.
created_at: 2014-08-02 17:36:14.249466000 Z
updated_at: 2014-08-02 18:21:42.511522000 Z
=> nil
2.1.2 :003 >
In routes.rb I have:
resources :dtests do
resources :dquestions
end
All activities and routes of type "/ dtests/2/dquestions/3" working properly, but when I try to enter "dtests/2/dquestions", an error:
No route matches {:action=>"show", :controller=>"dquestions", :locale=>:en, :dtest_id=>#<Dquestion id: 1, question_text: "dfs", count_answer: 4, dtest_id: "1", created_at: "2013-06-01 09:32:41", updated_at: "2013-06-01 09:32:41">}
In server log:
Dquestion Load (0.5ms) SELECT "dquestions".* FROM "dquestions" WHERE "dquestions"."dtest_id" = 1
default_url_options is passed options: {}
Rendered dquestions/index.html.erb within layouts/application (39.5ms)
Completed 500 Internal Server Error in 106ms
ActionController::RoutingError
Tell me, please, what's the problem?
Look here:
:dtest_id=>#<Dquestion id: 1, question_text: "dfs", count_answer: 4, dtest_id: "1", created_at: "2013-06-01 09:32:41", updated_at: "2013-06-01 09:32:41">
Somehow you pass the whole object instead of the id. Can't tell why to_param is not automatically triggered.
Anyway just prepend .id to your object in the url helper.
This is one of those ones that makes you think you're going insane...
I have a class Section, and a DraftSection that inherits from it:
(Trimmed for brevity)
class Section
include Mongoid::Document
belongs_to :site
field :name, type: String
end
And
class DraftSection < Section
field :name, type: String, default: "New Section"
end
All simple stuff... console proves (again, trimmed for brevity):
004 > site = Site.first
=> #<Site _id: initech, name: "INITECH">
005 > site.sections.build
=> #<Section _id: 1, site_id: "initech", name: nil>
006 > site.draft_sections.build
=> #<DraftSection _id: 2, site_id: "initech", name: "New Section">
As you can see - the draft section name correctly defaults to "New Section" as it is overridden in the subclass.
Now when I run this spec:
describe "#new" do
it "should return a draft section" do
get 'new', site_id: site.id, format: :json
assigns(:section).should == "Something..."
end
end
Which tests this controller method:
def new
#section = #site.draft_sections.build
respond_with #section
end
Which fails (as expected), but with this:
Failure/Error: assigns(:section).should == "Something..."
expected: "Something..."
got: #<DraftSection _id: 1, site_id: "site-name-4", name: nil> (using ==)
What gives???
Update:
I figured it might be an issue with the different environment settings, so I looked at the mongoid.yml config file and saw this in the options:
# Preload all models in development, needed when models use
# inheritance. (default: false)
preload_models: true
I added it to the test environment settings too, but still no joy :(
Update 2 - the plot thickens...
Thought I'd try loading up the console in the test environment and trying the same as before:
001 > site = Site.first
=> #<Site _id: initech, name: "INITECH">
002 > site.draft_sections.build
=> #<DraftSection _id: 1, site_id: "initech", name: "New Section">
WTF?
Ok, no one's listening, but I'll post the solution here for future reference anyway...
For some reason, some time ago I had a debug session that meant I had left this code in my Spork.each_run block
# Reload all model files when run each spec
# otherwise there might be out-of-date testing
# require 'rspec/rails'
Dir["#{Rails.root}/app/controllers//*.rb"].each do |controller|
load controller
end
Dir["#{Rails.root}/app/models//*.rb"].each do |model|
load model
end
Dir["#{Rails.root}/lib//*.rb"].each do |klass|
load klass
end
This was causing the models to get reloaded on each run of a spec. Not surprisingly, this screwed up the way the classes were set up in memory whilst the specs were running.
Definitely explains why it was such a hard one to debug...
So for future googlers with similar inheritance problems in Rspec only - make sure that there's nothing reloading models anywhere in the test stack.
I'm getting the following error: SystemStackError: stack level too deep when executing the following code in rails3 beta4 under ruby 1.9.2-rc1:
ruby-1.9.2-rc1 > f = Forum.all.first
=> #<Forum id: 1, title: "Forum 1", description: "Description 1", content: "Content 1", parent_id: nil, user_id: 1, forum_type: "forum", created_at: "2010-07-17 04:39:41", updated_at: "2010-07-17 04:39:41", icon_file_name: nil, icon_content_type: nil, icon_file_size: nil, icon_updated_at: nil>
ruby-1.9.2-rc1 > f.children
=> [#<Forum id: 2, title: "Thread 2", description: "Description 2", content: "Content 2", parent_id: 1, user_id: 1, forum_type: "thread", created_at: "2010-07-17 04:40:17", updated_at: "2010-07-17 04:40:17", icon_file_name: nil, icon_content_type: nil, icon_file_size: nil, icon_updated_at: nil>]
ruby-1.9.2-rc1 > f.forum_type = "thread"
=> "thread"
ruby-1.9.2-rc1 > f.save
SystemStackError: stack level too deep
from /Users/emilkampp/.rvm/rubies/ruby-1.9.2-rc1/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!!
ruby-1.9.2-rc1 >
And that is caused by the following code:
# Before and after filters
#
before_update :update_all_nested_objects, :if => :forum_type_changed?
protected
# Checks if the +forum_type+ has been changed
#
def forum_type_changed?
self.forum_type_changed?
end
# Updates all nested upjects if the +forum_type+ has been changed
#
# This will trigger the +update_all_nested_objects+ method on all touched children, thus
# starting a chain-reaction all the way through the forum-tree.
#
# NOTE: This is where the error is triggered, since this is the only non-tested looping code, and my test-
# cases hasn't changed since this was added.
#
def update_all_nested_objects
children.each do |child|
child.forum_type = child_type
child.save
end
end
So, what's going on. I have been checking around a little, but nobody seems to have the same problem. Either it's not the same ruby version, thus the workflow.rb file is different, or the stack level to deep is caused by something else.
Any help will be greatly apreciated!
Best regards
// Emil
You are calling same method in method itself
def forum_type_changed?
self.forum_type_changed? #This will never ending process hence it gives error
end
I think you have same method name and column name that causing problem change your method name then
def check_forum_type_changed?
self.forum_type_changed? #method name and column name are different
end