Errors encountered in 10.3.3 of Michael Hartl's Rails Tutorial - railstutorial.org

I ran into the following errors when running my RSpec tests:
Failures:
1) Authentication authorization as wrong user visiting Users#edit page
Failure/Error: before { visit edit_user_path(wrong_user) }
ActionView::Template::Error:
undefined local variable or method `feed_item' for #<#<Class:0x007f89628593f8>:0x007f895fcdcd38>
# ./app/views/shared/_feed.html.erb:1:in `_app_views_shared__feed_html_erb___2268788413810348528_70113969385200'
# ./app/views/static_pages/home.html.erb:13:in `_app_views_static_pages_home_html_erb__501962788019734978_70114020097480'
# ./spec/requests/authentication_pages_spec.rb:108:in `block (5 levels) in <top (required)>'
2) Micropost pages micropost creation with invalid information should not create a micropost
Failure/Error: before { visit root_path }
ActionView::Template::Error:
undefined local variable or method `feed_item' for #<#<Class:0x007f89628593f8>:0x007f895ff8c3a8>
# ./app/views/shared/_feed.html.erb:1:in `_app_views_shared__feed_html_erb___2268788413810348528_70113969385200'
# ./app/views/static_pages/home.html.erb:13:in `_app_views_static_pages_home_html_erb__501962788019734978_70114020097480'
# ./spec/requests/micropost_pages_spec.rb:11:in `block (3 levels) in <top (required)>'
3) Micropost pages micropost creation with invalid information error messages
Failure/Error: before { visit root_path }
ActionView::Template::Error:
undefined local variable or method `feed_item' for #<#<Class:0x007f89628593f8>:0x007f8962b99798>
# ./app/views/shared/_feed.html.erb:1:in `_app_views_shared__feed_html_erb___2268788413810348528_70113969385200'
# ./app/views/static_pages/home.html.erb:13:in `_app_views_static_pages_home_html_erb__501962788019734978_70114020097480'
# ./spec/requests/micropost_pages_spec.rb:11:in `block (3 levels) in <top (required)>'
4) Micropost pages micropost creation with valid information should create a micropost
Failure/Error: before { visit root_path }
ActionView::Template::Error:
undefined local variable or method `feed_item' for #<#<Class:0x007f89628593f8>:0x007f895de6c3a8>
# ./app/views/shared/_feed.html.erb:1:in `_app_views_shared__feed_html_erb___2268788413810348528_70113969385200'
# ./app/views/static_pages/home.html.erb:13:in `_app_views_static_pages_home_html_erb__501962788019734978_70114020097480'
# ./spec/requests/micropost_pages_spec.rb:11:in `block (3 levels) in <top (required)>'
5) Static pages for signed-in users should render the user's feed
Failure/Error: visit root_path
ActionView::Template::Error:
undefined local variable or method `feed_item' for #<#<Class:0x007f89628593f8>:0x007f895dd16f80>
# ./app/views/shared/_feed.html.erb:1:in `_app_views_shared__feed_html_erb___2268788413810348528_70113969385200'
# ./app/views/static_pages/home.html.erb:13:in `_app_views_static_pages_home_html_erb__501962788019734978_70114020097480'
# ./spec/requests/static_pages_spec
Code:
The code for authentication_pages_spec.rb
require 'spec_helper'
describe "Authentication" do
subject { page }
describe "signin page" do
before { visit signin_path }
it { should have_selector('h1', text: 'Sign in') }
it { should have_selector('title', text: 'Sign in') }
end
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
it { should have_selector('title', text: 'Sign in') }
it { should have_selector('div.alert.alert-error', text: 'Invalid') }
describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_selector('div.alert.alert-error') }
end
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }
it { should have_selector('title', text: user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
it { should have_link('Settings', href: edit_user_path(user)) }
it { should have_link('Users', href: users_path) }
it { should_not have_link('Sign in', href: signin_path) }
describe "followed by signout" do
before { click_link "Sign out" }
it { should have_link('Sign in') }
end
end
end
describe "authorization" do
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }
describe "when attempting to visit a protected page" do
before do
visit edit_user_path(user)
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
describe "after signing in" do
it "should render the desired protected page" do
page.should have_selector('title', text: 'Edit user')
end
end
end
describe "in the Users controller" do
describe "visiting the edit page" do
before { visit edit_user_path(user) }
it { should have_selector('title', text: 'Sign in') }
end
describe "submitting to the update action" do
before { put user_path(user) }
specify { response.should redirect_to(signin_path) }
end
describe "visiting the user index" do
before { visit users_path }
it { should have_selector('title', text: 'Sign in') }
end
end
end
describe "in the Microposts controller" do
describe "submitting to the create action" do
before { post microposts_path }
specify { response.should redirect_to(signin_path) }
end
describe "submitting to the destroy action" do
before { delete micropost_path(FactoryGirl.create(:micropost)) }
specify { response.should redirect_to(signin_path) }
end
end
describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong#example.com") }
before { sign_in user }
describe "visiting Users#edit page" do
before { visit edit_user_path(wrong_user) }
it { should_not have_selector('title', text: full_title('Edit user')) }
end
describe "submitting a PUT request to the Users#update action" do
before { put user_path(wrong_user) }
specify { response.should redirect_to(root_path) }
end
end
describe "as non-admin user" do
let(:user) { FactoryGirl.create(:user) }
let(:non_admin) { FactoryGirl.create(:user) }
before { sign_in non_admin }
describe "submitting a DELETE request to the Users#destroy action" do
before { delete user_path(user) }
specify { response.should redirect_to(root_path) }
end
end
end
end
The code for _feed.html.erb**
<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="content"><%= feed_item.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
</li>
The code for _feed_item.html.erb
<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="content"><%= feed_item.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
<% if current_user?(feed_item.user) %>
<%= link_to "delete", feed_item, method: :delete,
data: { confirm: "You sure?" },
title: feed_item.content %>
<% end %>
</li>
Please let me know if there are additional files I should include. Thanks!

You have issues with your _feed.html.erb and _feed_item.html.erb files: at the moment, you're referencing feed_item without having defined it anywhere.
Refer to the equivalent files in Rails Tutorial: _feed.html.erb, _feed_item.html.erb.

Related

ReferenceError: " postIts.forEach(function(postit)" on list.ejs? postIts is not defined at eval (eval at compile

I need to loop through an object PostIts and display the "Id", " Title" with an ejs "forEach" Loop Am using sails.js "1.2.3" and mongodb on local host, but i get error
ReferenceError : postIts is not defined at eval (eval at compile ?
Here is the code on the PostItsController.js:
module.exports = {
list: function(req, res) {
// res.view('list');
PostIts.find({}).exec(function(err, postIts) {
if (err) {
res.send(500, { error: 'Database Error' });
}
res.view('list', { postIts: postIts });
});
}
};
And here is the code on list.ejs:
<tbody>
<% postIts.forEach(function(postit){ %>
<tr>
<td>
<%= postit.id %>
</td>
<td>
<%= postit.title %>
</td>
<td></td>
</tr>
<% }) %>
</tbody>
I should get the value of the ID and title displayed on the list.ejs page in a table, but instead I get an error that the postIts object is not defined.
First of all your route '/postIts/list': { view: 'list' }, should point to an action (since it has backend logic) not a view, so in your case "/postIts/list": "PostItsController.list", but if you're using actions2 things would be simpler
Secondly you don't need to tell your users that you have a database error error: "Database Error"
Using Actions2
sails generate action post/list
In your config/route.js
'POST /api/v1/post/list': { action: 'post/list' },
In your action
module.exports = {
friendlyName: "List Posts",
description: "List all post in our site",
inputs: {},
exits: {
success: {
description: "The path to your template file",
viewTemplatePath: "list"
}
},
fn: async function(inputs) {
var posts = await Post.find();
// All done.
return { postIts: posts };
}
};
postit works
An Boohoo! it works
https://sailsjs.com/documentation/concepts/actions-and-controllers/routing-to-actions
If you're sure that res.view('list', { postIts: postIts }); is actually sending the correct data you can use _.each(postIts, cb()) ... instead
For some reason the postIts object didnt save the data from the post req I made instead it just recalled what I posted. and I used the '_.each(postIts, function (postit)' and it finally worked.
to me its like a magic happened hahaha but yeah I learned from it.
thanks #Navicstein Rotciv for the quick replies.

SAILS - How to close modal with ajax-form after receiving "notFound" error from action

So, I am using Sailsjs - and I am using a modal form to confirm deletion of an item. What I want to do is to detect a double deletion (someone deleted an item - while it was in someone else's page - and the second person tries to delete it too).
I managed to do that - my action is returning "notFound".
The problem is - how can I make the Modal close when getting "notFound" back from the action instead of success?
Here is an excerpt of my action code:
exits: {
forbidden: {
description: 'The user making this request is not the owner of this thing and therefore cannot delete it.',
responseType: 'forbidden'
},
notFound: {
description: 'The item was not found in the database. Maybe it has already been deleted?',
responseType: 'notFound'
}
},
fn: async function (inputs, exits) {
var thing = await Thing.findOne({
id: inputs.id
});
if (!thing) {
throw 'notFound';
}
if (thing) {
if (thing.owner !== this.req.me.id ) {
throw 'forbidden';
}
}
await Thing.destroy({ id: inputs.id });
return exits.success();
}
and here is an excerpt of my modal using ajax-form component:
<% /* Confirm DeleteThing Modal */%>
<modal v-if="confirmDeleteThingModalOpen" #close="closeDeleteThingModal()">
<ajax-form action="destroyOneThing" :syncing.sync="syncing" :cloud-error.sync="cloudError" :handle-parsing.sync="handleParsingDeleteThingForm" #submitted="submittedDeleteThingForm()">
<div class="modal-header">
<h3 class="modal-title">Are you sure?</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
You can use #rejected on your ajax-form. So somethinglike:
<ajax-form action="destroyOneThing" #rejected="rejectedDeleteOneThing"
Then tell it what you want it to do on your page script
something like:
rejectedDeleteOneThing: function() {
this.confirmDeleteThingModalVisible = false;
}

Why would my Edit Form be view-able but not my New form of the same controller?

If I click on my edit link the link works and takes me to the edit form for the particular gallery I'd like to edit. If I click on the link to the new for the same controller I get the following error:
NoMethodError at /users/2/galleries/new
undefined method `galleries_path' for #<#<Class:0x007f4714fbac68>:0x000000060f32e0>
Did you mean? gallery_path
I've done extensive searching for the solution to this, but I'm not sure what I am doing wrong. This was working fine until I updated the routes to have galleries a nested resource to the user. I was in the process of updating/correcting my links and forms when I couldn't figure out how to get past this issue.
Below are my routes, the galleries/_form, galleries/edit, galleries/_new, parts of the gallery controller.
routes.rb
resources :users, shallow: true do
resources :galleries
resources :images
resources :albums
end
galleries.html.erb
<h1>New Gallery</h1>
<%= render 'form' %>
<%= link_to 'Dashboard', user_path(current_user) %>
galleries/edit.html.erb
<h1>Update Gallery</h1>
<%= render 'form' %>
<%= link_to 'Dashboard', user_path(current_user) %>
galleries_controller.rb
def new
#gallery = current_user.galleries.new
end
def create
#gallery = current_user.galleries.build(gallery_params)
respond_to do |format|
if #gallery.save
format.html { redirect_to #gallery, notice: 'Gallery was successfully created.' }
format.json { render :show, status: :created, location: #gallery }
else
format.html { render :new }
format.json { render json: #gallery.errors, status: :unprocessable_entity }
end
end
end
def show
end
def update
if #gallery.update_attributes(gallery_params)
flash[:success] = "Gallery Updated"
redirect_to #gallery
else
render 'edit'
end
end
private
def set_gallery
#gallery = Gallery.find(params[:id])
end
def gallery_params
params.require(:gallery).permit(:title, images_files: [])
end
If you need more from me let me know. Thanks in advance.
I ended up not using 1 form for edit and new. I made two separate forms, violating DRY principles, but getting my app to work so I could move on.

Facebook Open Graph: How to Post User Message with Custom Action

The Facebook tutorials suggest that you can add a user comment to a custom action in a Facebook App. The example javascript function for posting is:
<script type="text/javascript">
function postCook()
{
FB.api(
'/me/[YOUR_APP_NAMESPACE]:cook',
'post',
{ recipe: 'http://fbwerks.com:8000/zhen/cookie.html' },
function(response) {
if (!response || response.error) {
alert('Error occurred');
} else {
alert('Cook was successful! Action ID: ' + response.id);
}
});
}
</script>
I have an "Endorse" action defined with the object "Local Business". Everything is working. Now I want to give the user the option to add a user message to their endorsement but I can not find any help in Facebook docs on how to implement this in the api (the above code). Any help?
You need to specify the 'message' parameter when publishing the action. For example,
<script type="text/javascript">
function postCook(userMessage)
{
FB.api(
'/me/[YOUR_APP_NAMESPACE]:cook',
'post',
{ recipe: 'http://fbwerks.com:8000/zhen/cookie.html',
message: userMessage },
function(response) {
if (!response || response.error) {
alert('Error occurred');
} else {
alert('Cook was successful! Action ID: ' + response.id);
}
});
}
</script>
will submit userMessage as a user provided message on the action.
The full list of supported parameters for the OpenGraph publishing API is available here: https://developers.facebook.com/docs/technical-guides/opengraph/publish-action/#create

Include sentence in facebook post

Hello
I want to post a sentence in news feed of user together with the link, but how can I include the text along with the link.
This is the link I am using:
<script type="text/javascript">
function fbs_click() {
u = location.href;
t = document.title; window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
return false;
}
</script>
<style>
html .fb_share_link {
padding:2px 0 0 20px;
height:16px;
background:url(http://static.ak.facebook.com/images/share/facebook_share_icon.gif?6:26981)
no-repeat top left;
}
</style>
<a rel="nofollow" href="http://www.facebook.com/share.php?u=http://www.mywebsite.com" onclick="return fbs_click()" target="_blank" class="fb_share_link">Share on Facebook</a>
Thanks in advance, Laziale
Do you have to use the PHP link? Seems like it might be easier with the official javascript client library: https://github.com/facebook/connect-js . Here is sample code:
FB.ui(
{
method: 'stream.publish',
attachment: {
name: 'JSSDK',
caption: 'The Facebook JavaScript SDK',
description: (
'A small JavaScript library that allows you to harness ' +
'the power of Facebook, bringing the user\'s identity, ' +
'social graph and distribution power to your site.'
),
href: 'http://fbrell.com/'
},
action_links: [
{ text: 'fbrell', href: 'http://fbrell.com/' }
]
},
function (response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);