Static Pages from Railcast

Hi everyone,

I followed the railcast from Ryan at #117 Semi-Static Pages - RailsCasts and works. But what I would like to do is on my general layouts have the permalink to be generated this is where i get confused with the tutorial ruby on rails http://ruby.railstutorial.org/chapters/

My scaffold is called pages. I though we could fetch the resource by doing has follow

<%= render @pages %> But i am getting and error

ArgumentError in Static_pages#home

Showing /home/jean/rail/wyw/app/views/layouts/_footer.html.erb where line #6 raised:

'nil' is not an ActiveModel-compatible object that returns a valid partial path. Extracted source (around line #6):

3: Copyright Where you Where 2012 4: </small> 5: 6: <%= render @pages %> 7: <nav> 8: <ul> 9: <li><%= link_to "About", '#' %></li> Trace of template inclusion: app/views/layouts/application.html.erb

Rails.root: /home/jean/rail/wyw

Application Trace | Framework Trace | Full Trace

Then the tutorial say i have to add a _page.html.erb which i added into the folder of pages, but render@pages doesnt see it. Not sure what to do!!

Thanks

Hi everyone,

I followed the railcast from Ryan at #117 Semi-Static Pages - RailsCasts and works. But what I would like to do is on my general layouts have the permalink to be generated this is where i get confused with the tutorial ruby on rails http://ruby.railstutorial.org/chapters/

My scaffold is called pages. I though we could fetch the resource by doing has follow

<%= render @pages %> But i am getting and error

ArgumentError in Static_pages#home

Showing /home/jean/rail/wyw/app/views/layouts/_footer.html.erb where line #6 raised:

'nil' is not an ActiveModel-compatible object that returns a valid partial path. Extracted source (around line #6):

3: Copyright Where you Where 2012 4: </small> 5: 6: <%= render @pages %>

Same again, @pages is nil. Try to look at the error and see if you can understand what it means.

Colin

Colin Law wrote in post #1069081:

Extracted source (around line #6):

3: Copyright Where you Where 2012 4: </small> 5: 6: <%= render @pages %>

Same again, @pages is nil. Try to look at the error and see if you can understand what it means.

Colin

Well now what I have done his has follow

<%= render @page.name %>

And get the error has follow: The partial name (About Us) is not a valid Ruby identifier; make sure your partial name starts with a letter or underscore, and is followed by any combinations of letters, numbers, or underscores.

So to me this should be the string About Us and placing has html. Did i miss something??? I do know that the page.name = "About Us" But why not just displaying it has a html string. What step did i confused.

Have a look at the Rails Guide on Layouts and Rendering. Section 2 describes how to use render, but read and understand the rest of it.

I forget whether you are a beginner. If so then work right through some tutorials such as railstutorial.org to understand the basics of Rails, also look at the other Rails Guides.

Colin

Colin Law wrote in post #1069081:

Extracted source (around line #6):

3: Copyright Where you Where 2012 4: </small> 5: 6: <%= render @pages %>

Same again, @pages is nil. Try to look at the error and see if you can understand what it means.

Colin

Well now what I have done his has follow

<%= render @page.name %>

And get the error has follow: The partial name (About Us) is not a valid Ruby identifier; make sure your partial name starts with a letter or underscore, and is followed by any combinations of letters, numbers, or underscores.

So to me this should be the string About Us and placing has html. Did i miss something??? I do know that the page.name = "About Us" But why not just displaying it has a html string. What step did i confused.

Have a look at the Rails Guide on Layouts and Rendering. Section 2 describes how to use render, but read and understand the rest of it.

I forget whether you are a beginner. If so then work right through some tutorials such as railstutorial.org to understand the basics of Rails, also look at the other Rails Guides.

Looking back I see that I have already advised that. How are you getting on with the tutorials.

Colin

Colin Law wrote in post #1069088:

Colin

Yes I am new to Ruby on Rails. I have read and try several tutorial in the last 2 weeks. But this is what I am trying to do.

I have a create a file Static_Pages/homes which his just a static pages.

It has the following structure

<!DOCTYPE html> <html>   <head>     <title><%= full_title(yield(:title)) %></title>     <%= content_for(full_meta_description(yield(:meta_description))).html_safe %>     <%= full_meta_keyword(yield(:meta_keyword)) %>     <%= full_meta_author(yield(:meta_author)) %>

    <%= stylesheet_link_tag "application", media: "all" %>     <%= javascript_include_tag "application" %>     <%= render 'layouts/shim' %>   </head>   <body>     <%= render 'layouts/header' %>     <div class="container">       <%= yield %>     </div>     <%= render 'layouts/footer' %>   </body> </html>

_footer.html would then have page links to my resources pages which contains static links such About Us, Company News, etc... Based on this tutorial http://ruby.railstutorial.org/chapters/user-microposts#sec:manipulating_microposts section 10.22 you must use render to allow to insert a file which you then create which can be use if there is an association. However there is no association, About Us and Company News aren't related to a user or somesort.

Any solution that ruby can have if i dont have a model association between those two field?

Colin Law wrote in post #1069088:

Colin

Yes I am new to Ruby on Rails. I have read and try several tutorial in the last 2 weeks.

Reading several tutorials is not sufficient. Work right through railstutorial.org, entering the code, running it, and doing the exercises. Make sure you understand absolutely every line of code.

Colin

Colin Law wrote in post #1069096:

Reading several tutorials is not sufficient. Work right through railstutorial.org, entering the code, running it, and doing the exercises. Make sure you understand absolutely every line of code.

Colin

That my problem, I don't understand! I have try to make the tutorial but i don't get it. Using example 10.22 it says to insert a micropost into a user field you must do has follow

<%= render @resources %> into the location where you want the micropost to appear or the content

You then create a file called _micropost.html.erb in the folder micropost/_micropost.html.erb

And voila, to me this is how i see it, I do see that you might have to define in the controller users @micropost.user to allow the association to works. But in my program this particular model doesn't have an association. Is there something I miss, does Rails allow a way to go around this or no. Cause if no, then I must change language

Colin Law wrote in post #1069096:

Reading several tutorials is not sufficient. Work right through railstutorial.org, entering the code, running it, and doing the exercises. Make sure you understand absolutely every line of code.

Colin

That my problem, I don't understand! I have try to make the tutorial but i don't get it. Using example 10.22 it says to insert a micropost into a user field you must do has follow

<%= render @resources %> into the location where you want the micropost to appear or the content

I can't find that in the tutorial. Which section and listing number is it in.

Colin

Colin Law wrote in post #1069113:

i don't get it. Using example 10.22 it says to insert a micropost into a user field you must do has follow

<%= render @resources %> into the location where you want the micropost to appear or the content

I can't find that in the tutorial. Which section and listing number is it in.

Colin

its actually microposts i was refering resources to any resource or model that exists has broader concept.

But it still his <%= render @microposts %> then you must create a file with all the information that you want to show in the folder microposts/_microposts.html.erb

Once this is done then you need to tell in the User controllers

def show     @user = User.find(params[:id])     @microposts = @user.microposts.paginate(page: params[:page])   end

But what if my model doesn't have a relation between the two! Then i can't use it?

Are you just trying to render a static partial?

Colin

Colin Law wrote in post #1069118:

Colin Law wrote in post #1069118:

def show     @user = User.find(params[:id])     @microposts = @user.microposts.paginate(page: params[:page])   end

But what if my model doesn't have a relation between the two! Then i can't use it?

Are you just trying to render a static partial?

Colin

Yes

How far through the tutorial have you worked?

Colin

Colin Law wrote in post #1069121:

The reason I asked was that listing 10.30 shows how to render a fixed page. Also the other advise I gave to study the rails guide on rendering would do that. There really is no shortcut, you need to learn stuff. The best way to start is by carrying on through the tutorial and studying the rails guides.

Colin

Colin Law wrote in post #1069130:

How far through the tutorial have you worked?

Colin

Well the fartest i was was 9, but I retry today, i am going to scratch it again, I did skip to best fit my program. But it remainds the main issues is that I can't simply render a resource into an other page. Is it at least possible?

The reason I asked was that listing 10.30 shows how to render a fixed page. Also the other advise I gave to study the rails guide on rendering would do that. There really is no shortcut, you need to learn stuff. The best way to start is by carrying on through the tutorial and studying the rails guides.

Colin

I understand well your telling me its possible which his good I however re did everything from the tutorial and on the test i am getting error

Here the command bundle exec rspec spec/requests/static_pages_spec.rb

The result F

Failures:

  1) Static pages Home page should have the content 'Sample App'      Failure/Error: visit '/static_pages/home'      NoMethodError:        undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xa1695cc>      # ./spec/requests/static_pages_spec.rb:7:in `block (3 levels) in <top (required)>'

Finished in 0.04033 seconds 1 example, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:6 # Static pages Home page should have the content 'Sample App'

Why would visit not be defined? Thanks for your help, not sure why its gives me this

Colin Law wrote in post #1069130:

How far through the tutorial have you worked?

Colin

Well the fartest i was was 9, but I retry today, i am going to scratch it again, I did skip to best fit my program. But it remainds the main issues is that I can't simply render a resource into an other page. Is it at least possible?

The reason I asked was that listing 10.30 shows how to render a fixed page. Also the other advise I gave to study the rails guide on rendering would do that. There really is no shortcut, you need to learn stuff. The best way to start is by carrying on through the tutorial and studying the rails guides.

Colin

I understand well your telling me its possible which his good I however re did everything from the tutorial and on the test i am getting error

Here the command bundle exec rspec spec/requests/static_pages_spec.rb

The result F

Failures:

  1) Static pages Home page should have the content 'Sample App'      Failure/Error: visit '/static_pages/home'      NoMethodError:        undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xa1695cc>      # ./spec/requests/static_pages_spec.rb:7:in `block (3 levels) in <top (required)>'

Finished in 0.04033 seconds 1 example, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:6 # Static pages Home page should have the content 'Sample App'

Why would visit not be defined? Thanks for your help, not sure why its gives me this

My best guess would be that you have an error in the test code. If you cannot see it then post static_pages_spec.rb here. Copy/paste it from your actual code so we can check for typos.

Colin

Colin Law wrote in post #1069168:

getting error      Failure/Error: visit '/static_pages/home'

rspec ./spec/requests/static_pages_spec.rb:6 # Static pages Home page should have the content 'Sample App'

Why would visit not be defined? Thanks for your help, not sure why its gives me this

My best guess would be that you have an error in the test code. If you cannot see it then post static_pages_spec.rb here. Copy/paste it from your actual code so we can check for typos.

Colin

Lol here what I have

spec/requests/static_pages_spec.rb describe "Static pages" do

  describe "Home page" do     it "should have the content 'Sample App'" do             visit '/static_pages/home'             page.should have_content('Sample App')         end     end end

app/views/layouts/application.html.erb <!DOCTYPE html> <html>   <head>     <title><%= full_title(yield(:title)) %></title>     <<%= full_meta_description(yield(:meta_description)) %> />     <<%= full_meta_keyword(yield(:meta_keyword)) %> />     <<%= full_meta_author(yield(:meta_author)) %> />

    <%= stylesheet_link_tag "application", media: "all" %>     <%= javascript_include_tag "application" %>     <%= csrf_meta_tags %>     <%= render 'layouts/shim' %>   </head>   <body>     <%= render 'layouts/header' %>     <div class="container">       <% flash.each do |key, value| %>         <div class="alert alert-<%= key %>"><%= value %></div>       <% end %>       <%= yield %>       <%= render 'layouts/footer' %>       <%= debug(params) if Rails.env.development? %>     </div>   </body> </html>

app/views/static_pages/home.html.erb Sample App <div class="left_column"> </div> <div class="right_column">   <%= link_to "Sign Up", 'signup_path' %> </div>

And do as follow to run the test bundle exec rspec spec/requests/static_pages_spec.rb

Colin Law wrote in post #1069168:

getting error      Failure/Error: visit '/static_pages/home'

rspec ./spec/requests/static_pages_spec.rb:6 # Static pages Home page should have the content 'Sample App'

Why would visit not be defined? Thanks for your help, not sure why its gives me this

My best guess would be that you have an error in the test code. If you cannot see it then post static_pages_spec.rb here. Copy/paste it from your actual code so we can check for typos.

Colin

Lol here what I have

spec/requests/static_pages_spec.rb describe "Static pages" do

  describe "Home page" do     it "should have the content 'Sample App'" do             visit '/static_pages/home'             page.should have_content('Sample App')         end     end end

That is not quite the same as that in Listing 3.9 on http://ruby.railstutorial.org/chapters/static-pages#top You seem to have a bit missing at the beginning. Whether that is the cause of the problem I don't know.

Colin

Other people have reported this error:

NoMethodError:       undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xa1695cc>     # ./spec/requests/static_pages_spec.rb:7:in `block (3 levels) in <top (required)>'

google: rspec visit

In particular see:

https://github.com/rspec/rspec-rails/issues/360

You might want to post your Gemfile.