NoMethodError in MicropostsController#index - ruby-on-rails

I cannot figure this out-
NoMethodError in MicropostsController#index
and
I have searched the web and cannot seem to make the right change. I am doing Michael Hartl's Rails Tutorial and I am in ch 2 where we do microposts.
Here's index.html.erb
Listing Microposts
<table>
<thead>
<tr>
<th>Content</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #microposts.each do |micropost| %>
<tr>
<td><%= micropost.content %></td>
<td><%= micropost.user_id %></td>
<td><%= link_to 'Show', micropost %></td>
<td><%= link_to 'Edit', edit_micropost_path(micropost) %></td>
<td><%= link_to 'Destroy', micropost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Micropost', new_micropost_path %>
and
Microposts Controller
Class MicropostsController < ApplicationController
def index
end
def show
end
def new
end
def edit
end
def create
end
def update
end
def destroy
end
end

You are getting this error because there is no microposts instance variable in index action, in index action you should set #microposts like this:
def index
#microposts = Micropost.all
end

Related

Getting Started with Rails section 5.8 - SyntaxError in ArticlesController#index

I'm doing the tutorial for RoR and am getting an exception:
SyntaxError in ArticlesController#index
"....rails/blog/app/views/articles/index.html.erb:18: syntax error,
unexpected keyword_ensure, expecting end-of-input ensure ^"
It points to line 18 of an 16 line file. index.html.erb:
<h1>Listing articles</h1>
<table>
<tr>
<th>Title</th>
<th>Text</th>
</tr>
<% #articles.each.do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
</tr>
<% end %>
</table>
The articles_controller.rb file:
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
def new
end
def create
#article = Article.new(article_params)
#article.save
redirect_to #article
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
Not sure where my error is. I'm using LightTable to edit the work.
In Ruby do is a keyword that denotes a block. Not a method.
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
</tr>
<% end %>
Additionally you are not properly checking if the article is valid in your create method:
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render :new # renders /app/views/articles/new.html.erb
end
end
You have a typo on your code, it should be <% #articles.each do |article| %>
<h1>Listing articles</h1>
<table>
<tr>
<th>Title</th>
<th>Text</th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
</tr>
<% end %>
</table>

ActiveRecord::RecordNotFound in StoriesController#edit

I have correctly defined the params in my controller. It also says No route matches {:action=>"edit", :controller=>"stories", :id=>nil} missing required keys: [:id]'.
However didn't I define it to find the 'id' in the edit method below in my controller? Also, I don't understand why my destroy method isn't working either :/
class StoriesController < ApplicationController
before_action only: [:destroy, :show, :edit, :update]
def index
#stories = Story.order('created_at DESC')
end
def new
#story = current_user.stories.build
end
def create
#story = current_user.stories.build(story_params)
if #story.save
flash[:success] = "Your beautiful story has been added!"
redirect_to root_path
else
render 'new'
end
end
def edit
#story = Story.find(params[:id])
end
def update
if #story.update.attributes(story_params)
flash[:success] = "More knowledge, more wisdom"
redirect_to root_path
else
render 'edit'
end
end
def destroy
if #story.destroy
flash[:success] = "I think you should have more confidence in your storytelling"
else
flash[:error] = "Can't delete this story, sorry"
end
end
def show
#stories = Story.all
end
private
def story_params
params.require(:story).permit(:name, :description)
end
end
Index.html.erb:
<p id="notice"><%= notice %></p>
<h1>This is a list of posts</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #stories.each do |story| %>
<tr>
<td><%= story.name %></td>
<td><%= story.description %></td>
<td><%= story.user.email %></td>
<td><%= link_to 'Show', story %></td>
<% if user_signed_in? %>
<td><%= link_to 'Edit', edit_story_path(#story) %></td>
<td><%= link_to 'Destroy', story, method: :delete, data: { confirm: 'Are you sure?'} %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'New Story', new_story_path %>
Routes.rb:
Rails.application.routes.draw do
resources :stories
devise_for :users
root to: 'stories#index'
end
rake routes:
Prefix Verb URI Pattern Controller#Action
stories GET /stories(.:format) stories#index
POST /stories(.:format) stories#create
new_story GET /stories/new(.:format) stories#new
edit_story GET /stories/:id/edit(.:format) stories#edit
story GET /stories/:id(.:format) stories#show
PATCH /stories/:id(.:format) stories#update
PUT /stories/:id(.:format) stories#update
DELETE /stories/:id(.:format) stories#destroy
In your view,
<td><%= link_to 'Edit', edit_story_path(#story) %></td>
Should be,
<td><%= link_to 'Edit', edit_story_path(story) %></td>
Because, your loop is,
You should be having story variable inside loop because you have taken |story| inside the parameters as |story|
So, you form will be.
<% #stories.each do |story| %>
<tr>
<td><%= story.name %></td>
<td><%= story.description %></td>
<td><%= story.user.email %></td>
<td><%= link_to 'Show', story %></td>
<% if user_signed_in? %>
<td><%= link_to 'Edit', edit_story_path(#story) %></td>
<td><%= link_to 'Destroy', story_path(story),method: :delete,data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
<% end %>
Now, the delete link should be like this,
<td><%= link_to 'Destroy', story_path(story),method: :delete,data: { confirm: 'Are you sure?' } %></td>

Filter paginated posts by date introduced by user (Rails)

I have the post_controller with index method like this:
def index
#posts = Post.paginate(:page => params[:page], :per_page => 3)
end
and in app / views / posts / index.html.erb i have:
.
.
.
<table>
<tr>
<th>User</th>
<th>Post</th>
<th>Date</th>
<th colspan="3"></th>
</tr>
<% #posts.each do |post| %>
<tr>
<td><%= post.user.id %></td>
<td><%= post.text %></td>
<td><%= post.created_at %></td>
<td><%= link_to 'Show', post_path(post) %></td>
<% if can? :update, post %>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<% end %>
<% if can? :destroy, post %>
<td><%= link_to 'Destroy', post_path(post),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
<% end %>
<%= will_paginate #posts %>
</table>
So i want a field in this view, where an user can input a date and the view shows all posts wich were created on that date onwards. (Posts date > date_by_user). I tried with "where" method but i couldnt. I dont want to use any gem. So, what is the best and simple way to do that?
You can search records in index method like below.
date_to_search = Date.parse('2015/1/1')
Post.where(
'created_at>=? and created_at<=?',
date_to_search.beginning_of_day
)
created_at is saved as UTC in rails, so you need to specify datetime. Not only date, to get correct data in your local time.

Ajax edit page in index

I have this page Index page:
<h1>Listing users</h1>
<table border="1">
<tr>
<th>Name</th>
<th>Description</th>
<th>Who</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.description %></td>
<td><%= user.who %></td>
<%= render 'users/form' %>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New User', new_user_path %>
Scaffold controller
def index
#users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #users }
end
end
I need to edit user in index page, not to go url /users/2/edit, stay on users page.
How I do this in Ajax?
Add remote:true to the Edit line in order to make an ajax call:
<td><%= link_to 'Edit', edit_user_path(user), remote:true %></td>
Also, add an edit.js.erb file (a javascript file with embedded ruby) in which you do all the javascript actions to be executed after the Edit. That edit.js.erb file is generated when the edit method is done. This is where you write all the changes to the javascript code which affect the current page (the page will not be re-rendered).
Additionally, add the following to the edit method:
respond_to do |format|
format.js
end

Sunspot search doesn't work

I'm trying to make an search option in my site, but it won't show results after an search query.
I have followed this tutorial: http://collectiveidea.com/blog/archives/2011/03/08/full-text-searching-with-solr-and-sunspot/
My routes.rb:
resources :users do
collection do
get :search
end
member do
get :following, :followers
end
end
My index.html.erb
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New User', new_user_path %>
My model user.rb
searchable do
text :name
end
And my user controller
def index
#users = User.paginate(page: params[:page])
end
def search
#users = User.search do
keywords params[:query]
end.results
respond_to do |format|
format.html { render :action => "index" }
end
end
What am i doing wrong?

Resources