NoMethodError in Devise when calling Data - ruby-on-rails

I am using devise gem for users. I have a contents_controller.rb and I want to display content.name in my application.html.erb but there seems to be a conflict with Devise when I do this. When I load the page the data is rendered but when I click login, to access editing abilities, I get a NoMethodError in Devise::Sessions#new undefined method `name' for nil:NilClass. My code is listed below.
Application.html.erb :
<% #contents.each do |content| %>
<a class="navbar-brand" ><%= content.name %></a>
<%end%>
Contents_controller.rb :
class ContentsController < ApplicationController
before_action :find_content, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
#contents = Content.all
#services = Service.all.order("created_at asc")
#portfolios = Portfolio.all.order("created_at asc")
end
def new
#content = Content.new
end
def create
#content = Content.new content_params
if #content.save
redirect_to root_path, notice: "content Created!"
else
render 'new', notice: "Sorry content failed to create!!!"
end
end
def edit
end
def update
if #content.update content_params
redirect_to root_path, notice: "You Updated: #{#content.name}."
else
render 'edit'
end
end
def destroy
#content.destroy
redirect_to content_path
end
private
def content_params
params.require(:content).permit(:name, :header_title, :services_title, :slogan_title, :about_title, :about_description, :contact_title, :contact_subtitle, :fb_username, :ig_username, :twitter_username, :in_username, :pin_username)
end
def find_content
#content = Content.find(params[:id])
end
end
Can someone please help me explain why this is happening and how to fix this issue?

Related

How to fix this error with impressionist gem rails

Hi i got error with impressionist gem. i use Rails 5.2.4.4
i did these steps :
gem 'impressionist'
then bundle install
then rails g impressionist and bundle install
Then next step :
in my blogs_controllers
class BlogsController < ApplicationController
before_action :authorize_admin, only: [:new, :edit, :create, :update]
before_action :set_blog, only: [:show, :edit, :update, :destroy]
impressionist :actions => [:show, :index]
def index
#categories = Category.all
cate = params[:cate]
if !cate.nil?
#blogs = Blog.where(:category_id => cate)
else
#blogs = Blog.all
end
end
def show
impressionist(#blog)
end
def new
#blog = Blog.new
end
def create
#blog = Blog.new(blog_params)
if #blog.save
redirect_to blog_path(#blog), notice: "Successfully Created"
else
render :new
end
end
def edit
end
def update
#blog.slug = nil
if #blog.update(blog_params)
redirect_to blog_path(#blog), notice: "Successfully Update"
else
render 'edit'
end
end
def destroy
#blog.destroy
redirect_to blogs_path
end
private
def set_blog
#blog = Blog.find(params[:id])
end
def blog_params
params.require(:blog).permit(:title, :body, :image, :category_id)
end
def authorize_admin
return unless !current_user.admin?
redirect_to root_path
end
end
in blog model
is_impressionable
in blog show
<%= #blog.impressionist_count(:filter=>:ip_address) %>
And i got this error :
NameError in BlogsController#show
uninitialized constant ActiveSupport::ParameterFilter
Extracted source (around line #80):
# If the constant was actually loaded, something else went wrong?
raise(e) if from_mod.const_defined?(const_name)
CoreExt::ActiveSupport.without_bootsnap_cache { super }
end
# Signature has changed a few times over the years; easiest to not

Filtering out other users's notes in Searchkick rails 5

I'm a beginner to Ruby on Rails working on a Notebook app. I'm trying using Searchkick to enable users to quickly search their notes. I currently have 2 users (via devise gem).
I have just set up Searchkick, but when I search for a word that both the users have in their notes, the result shows notes by both users. So, a user can see the other's note in this case as in the image below.
Here is my notes_controller.rb code:
class NotesController < ApplicationController
before_action :find_note, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
#notes = Note.where(user_id: current_user).order("created_at DESC")
end
def search
if params[:search].present?
#notes = Note.search(params[:search])
else
#notes = Note.all
end
end
def new
#note = current_user.notes.build
end
def create
#note = current_user.notes.build(note_params)
if #note.save
redirect_to root_path, notice: "Note successfully created."
else
render 'new'
end
end
def show
end
def edit
end
def update
if #note.update(note_params)
redirect_to note_path(#note), notice: "Note successfully updated."
else
render 'edit'
end
end
def destroy
#note.destroy
redirect_to root_path, notice: "Note successfully deleted."
end
private
def note_params
params.require(:note).permit(:title, :body)
end
def find_note
#note = Note.find(params[:id])
end
end
My routes.rb code:
Rails.application.routes.draw do
devise_for :users
resources :notes do
collection do
get :search
end
end
authenticated :user do
root "notes#index"
end
root "welcome#home"
end
My search.html.erb code, which is the same as index.html.erb code:
<% #notes.each do |note| %>
<h2><%= link_to note.title, note_path(note) %></h2>
<% end %>
I have a feeling I need to add a conditional statement in the search action in the notes_controller but that is not working.
Can anyone help please?
Thank you.
That would be
#notes = Note.search params[:search], where: { user_id: current_user.id }

Params[:id] returning ActiveRecord::RecordNotFound Error

I need help figuring out why I'm getting a ActiveRecord::RecordNotFound Error when I attempt to retrieve an object by it's ID. Below is my error and code. Please let me know if any other files need to be added to this post. Thank you in advance for the help!
Error
ActiveRecord::RecordNotFound in WikisController#show
Couldn't find Wiki with 'id'=edit
def show
#wiki = Wiki.find(params[:id]) #Highlighted line within error
authorize #wiki
end
Controller
class WikisController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
def index
#wikis = Wiki.visible_to(current_user)
authorize #wikis
end
def new
#wiki = Wiki.new
authorize #wiki
end
def create
#wiki = current_user.wikis.create(wiki_params)
authorize #wiki
if #wiki.save
flash[:notice] = "Wiki was saved."
redirect_to #wiki
else
flash.now[:alert] = "Error saving Wiki. Try again."
render :new
end
end
def show
#wiki = Wiki.find(params[:id])
authorize #wiki
unless #wiki.private == nil
flash[:alert] = "You must be signed in to view private topics."
redirect_to new_session_path
end
end
def edit
#wiki = Wiki.find(params[:id])
authorize #wiki
end
def update
#wiki = Wiki.find(params[:id])
authorize #wiki
if #wiki.update_attributes(wiki_params)
flash[:notice] = "Wiki was updated."
redirect_to #wiki
else
flash.now[:alert] = "Error saving the Wiki. Try again."
render :edit
end
end
def destroy
#wiki = Wiki.find(params[:id])
authorize #wiki
if #wiki.destroy
flash[:notice] = "\"#{#wiki.title}\" was deleted successfully."
redirect_to root_path
else
flash.now[:alert] = "Error deleting Wiki. Try again."
render :show
end
end
private
def wiki_params
params.require(:wiki).permit(:title, :body, :role)
end
end
Routes
Rails.application.routes.draw do
resources :wikis
resources :charges, only: [:new, :create]
devise_for :users
resources :users, only: [:update, :show] do
post 'downgrade'
end
get 'welcome/index'
get 'welcome/about'
root 'welcome#index'
end
It seems like you are accessing wikis/edit URL instead of wikis/:id/edit. Make sure your link is correctly generated in your view.
Make sure in your view you have something like this (if using erb):
<%= link_to edit_wiki_path(#wiki) %>
Also you can definitely shorten up your controller using before_action
class WikisController < ApplicationController
...
before action :set_wiki, only: [:show, :edit, :update, :destroy]
...
private
def set_wiki
# Now this is being set in your show, edit, update, destroy method
# Make sure to delete from the above mentioned methods
#wiki = Wiki.find(params[:id])
end

Implementing most recent post in my index.html.erb Rails template

I've been putting the finishing touches on my app all day with the help of some useful answers here and would like to know how this feature can be executed. I have an idea set up in my post_controller file where I want to show the top 10 most recent posts created based on the date that they were created. I also plan on doing this for my comments as well laster on. I am showing all users post in the views/post/index.html.erb file. I wrote this line of code in the posts_controller: posts = Post.order('created_at DESC').limit(10). I've searched here thoroughly but don't understand how some other users got this to work, any insight? Thanks in advance.
posts_controller
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :vote]
before_action :require_user, only: [:new, :create, :edit, :update, :vote]
before_action :require_creator, only:[:edit, :update]
def index
posts = Post.order('created_at DESC').limit(10)
#posts = Post.all.page(params[:page]).per_page(10)
end
def show
#comment = Comment.new
end
def new
#post = Post.new
end
def create
#post = Post.new(post_params)
#post.creator = current_user
if #post.save
flash[:notice] = "You created a post!"
redirect_to posts_path
else
render :new
end
end
def edit
end
def update
#post = Post.find(params[:id])
if #post.update(post_params)
flash[:notice] = "You updated the post!"
redirect_to post_path(#post)
else
render :edit
end
end
def vote
Vote.create(voteable: #post, creator: current_user, vote: params[:vote])
respond_to do |format|
format.js { render :vote } # Renders views/posts/vote.js.erb
end
end
private
def post_params
params.require(:post).permit(:url, :title, :description)
end
def set_post
#post = Post.find(params[:id])
end
def require_creator
access_denied if #post.creator != current_user
end
end
The posts variable you've declared in your index action will not be available in your view. What you need to do is update the line where you're retrieving the posts using instance variable as follows:
def index
#posts = Post.page(params[:page]).order('created_at DESC').per_page(10)
end

How to fix undefined method error in rails app using has_many?

I get an error when I try to visit the below url in my rails app.
http://localhost:3000/origtexts/1/reviews/new
routes.rb
resources :origtexts do
resources :reviews
end
It passes the param of the review (1) correctly but I the error I get is undefined method `review' for the line in ReviewsController#new.
reviews_controller.rb
class ReviewsController < ApplicationController
before_filter :find_origtext
before_filter :find_review, :only => [:show, :edit, :update, :destroy]
def new
#review = #origtext.review.build
end
def show
end
def create
#review = #origtext.reviews.build(params[:review])
if #review.save
flash[:notice] = 'Review has been created'
redirect_to [#origtext, #review]
else
flash[:alert] = 'Review has not been created'
render :action => 'new'
end
end
private
def find_origtext
#origtext = Origtext.find(params[:origtext_id])
end
def find_review
#review = #origtext.reviews.find(params[:id])
end
end
Any advice on how to fix this?
Change review to reviews in this line
#review = #origtext.review.build
To
#review = #origtext.reviews.build

Resources