I'm new to rails and I have this no method error
show.html.erb
<h1>Profile Page for <%= #item.id %></h1>
<p><%= #item.content %></p>
items_controller.rb
class ItemsController < ApplicationController
def new
#item = Item.new
end
def create
item = Item.new(item_Params)
if item.save
redirect_to user_path(item.user)
else
render 'new'
end
end
private
def item_Params
params.require(:item).permit(:user_id, :content)
end
end
I want to create a show page for items/:id and I want to be able to view each item by their id, how should I create it?
this is my show page
Show page for which resource? Is it /users/:id (since you're redirecting to it) or items/:id? Anyway, you need to define #item in show action of corresponding controller. If you add more information about corresponding controller and user-item association, I can help you with it
UPDATE:
Just add to the ItemsController
def show
#item = Item.find(params[:id])
end
Make sure that you have a line resources :items inside routes.rb.
I think you need to read more about rails controllers in guides
BTW: it is a convention in ruby to use snake_case for method naming. Should be 'item_params', not 'item_Params'
You need to add show action which is for show page and You find the item corresponding its Id. so you just write below action in your controller.
def show
#item = Item.find(params[:id])
end
You can find your item in your Item model using find method.
Related
**I made a controller and model for post and not able to show my post's
title on the show page but repeatedly i am getting the same error **
my posts_controller code :
class PostsController < ApplicationController
def index
end
def new
end
def create
#render plain: params[:post][:body].inspect
#post = Post.new(post_params)
if #post.save
redirect_to posts_path
else
render "new"
end
end
def show
#post= Post.find(:id=>params[:id])
# #article = "prateek"
end
private
def post_params
params.require(:post).permit(:title,:body)
end
end
my show.html.erb file :
<h1><%= #post.title %></h1>
my post.rb file:
class Post < ApplicationRecord
end
**I expect the result but I didn't get anything right
error = NoMethodError in Posts#show
undefined method `title' for nil:NilClass**
You're passing an hash to find, while you're suppose to pass just the id.
#post= Post.find(params[:id])
or use find_by if you want to pass an hash
#post= Post.find_by(:id => params[:id])
I need render different show pages for my blog posts.
I have 3 different categories : themes, snippets, projects.
Each blog post must related with those three categories.
If anyone click post(assume related category is snippets), it display in different show...etc... It is same for other categories.
How it is possible with conditional statements.
you can make routes like:
resources :posts, except:[:show]
get 'posts/:id/cat/:category' , to:'posts#show', as: :show
you have to create partial for categories as follows:
app/views/posts/_themes.html.erb
app/views/posts/_snippets.html.erb
app/views/posts/_projects.html.erb
then in controller's show action.
controllers/posts_controller.rb
def show
#post = Post.find(params[:id])
#category = params[:category]
...
end
Then render that category in show page.
views/posts/show.html.erb
...
<%= render '#{#category}'%>
Just one show method and you can render different views conditionally and simple render works for you, you can use below code:
Just render with HTML file name if file is in same controller's view
if #post.theme?
render 'themes'
elsif #post.snippet?
render 'snippets'
else
render 'projects'
end
I would start with this, then refactor for avoiding repetition
categories_controller.rb
def themes
##posts = Post.where(category_id: 1)
...
render layout: themes
end
def snippets
##posts = Post.where(category_id: 2)
...
render layout: snippets
end
def projects
##posts = Post.where(category_id: 3)
...
render layout: snippets
end
You can do something like this:
models/post.rb
def category
#category ||= ... #returns category name
end
controllers/post_controller.rb
def show
#post = Post.find(id)
#category = #post.category
...
end
views/posts/show.html.erb
...
<%= render "types/#{#category}" %>
...
Also you can render specified template from controller if you don't have any common parts for categories
You can use rails partial to achieve this:
to know more about partial refer Rails Partial
you can create partial for categories as follows:
app/views/categories/_themes.html.erb
app/views/categories/_snippets.html.erb
app/views/categories/_projects.html.erb
in show page of categories you can modify the show page as follows
class CategoriesController < ApplicationController
...
def show
...
render params[:category]
end
...
end
Now when you call show page pass a param category which denotes the type of category which you want to show
if you want to show category snippets then the params will be params[:category] = 'snippets' this will look for partial inside categories view.
I created a rails association "user has many follows" currently have lists of user's followers and followings. I want to have 2 links onusers#show to display lists of followers and followings. I know I can render two different views in my follows#index using if else statement but I'm not sure how to go about it. My initial thought was to have rails check which link was clicked (either 'Followers' or 'Followings') but I couldn't find an exact answer. I also thought of creating two different tables (Followers, Followings) but adding 2 same values for 1 action seemed redundant and waste of space.
Here is my FollowsController:
class FollowsController < ApplicationController
def index
#follows = Follow.all
render :index
end
def create
#follow = Follow.create(follow_params)
redirect_to user_url(#follow.user_id)
end
def destroy
#follow = Follow.find(params[:id])
#follower_id = #follow.user_id
#follow.destroy!
redirect_to user_url(#follower_id)
end
private
def follow_params
params.require(:follow).permit(:follower_id, :user_id)
end
end
What would be the best approach?
Example:
def index
#follows = Follow.all
if #follows.present?
render 'show_with_followers'
else
render 'show_without_followings'
end
end
views
app/views/follows/_show_with_followers.html.erb
app/views/follows/_show_without_followings.html.erb
Here is an answer I found so far. I will be using in my view
<%= link_to 'Followers', follows_path(:follows => 'followers') %>
<%= link_to 'Followings', follows_path(:follows => 'followings') %>
and use
def index
if params["follows"] == 'followers'
#render followers
else
#render followings
end
end
end
I thought that adding #links=Link.all to my controller would fix this problem.
I did make sure that I have valid entries in my Link DB by looking at Link.count=6
I am getting the following error:
NoMethodError in Pages#index
Showing /Projects/reddit_on_rails/app/views/pages/index.html.erb where line #6 raised:
undefined method `each' for nil:NilClass
<ul>
<% #links.each do |link| %>
<li>link.url</li>
<li>link.title</li>
<% end %>
My controller:
class LinksController < ApplicationController
#Added #links =Link.all did not solve the problem
def index
#links = Link.all
end
def show
end
def new
#link = Link.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #link }
end
end
def create
#link = Link.new(link_params)
if #link.save
#render "new"
redirect_to "/"
else
# This line overrides the default rendering behavior, which
# would have been to render the "create" view.
render "new"
end
end
private
def link_params
params.require(:link).permit(:title,:url)
end
end
According to your description, you got following error message
NoMethodError in Pages#index
Showing /Projects/reddit_on_rails/app/views/pages/index.html.erb where line #6 raised:
According to error message you have problem in Pages controller inside index method. So you need to fix the problem inside Pages controller not inside Links controller.
Do you have method called index inside your Pages controller?
#links is nil because you are not setting it anywhere in your controller with something like #links = Link.all.
You cannot call each on nil.
What view are you in? Assuming index.html.erb.
in your controller, try adding
def index
#link = Link.all
end
If that doesn't work, verify you have any links at all. In terminal instead of typing rails server type
rails console
Link.all
If that comes back as [] then you don't have any in your database.
so, following M. Hartl's Tutorial, I have this static_pages controller that displays this
class StaticPagesController < ApplicationController
def home
if signed_in?
#micropost = current_user.microposts.build
#feed_items = current_user.feed.paginate(page: params[:page])
end
end
def help
end
def about
end
def contact
end
end
And this makes it show on the home page, all of the current users' microposts, but only the current users
how do i make it show ALL Users micropost, every single one created?
You are scoping the query to fetch only the current_user microposts. Micropost.all should get you all microposts. Also ensure that you havent already set some default scopes in your model, in which case you will have to use Micropost.unscoped.