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.
Related
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.
im using rails to make a basic chair selling website. At the moment someone can post a new chair and people can look at each individual show page. However im having issues with the show page when trying to display the email of who posted the chair.
If i click on the first chair, it works fine, however if i click on the other it says 'Couldn't find User with 'id'=2'. I have made all the chairs under 1 user.
thanks. let me know if you need anymore info.
class ChairsController < ApplicationController
def index
#chairs = Chair.all
end
def show
#chair = Chair.find(params[:id])
#user = User.find(params[:id])
end
def new
#chair = Chair.new
#user = current_user
end
def create
#user = current_user
#chair = Chair.new(chair_params)
#chair.user = #user
if #chair.save
redirect_to chairs_path
end
end
private
def chair_params
params.require(:chair).permit(:name, :description)
end
end
<h1><%= #chair.name %></h1>
posted by <%= #user.email %>
At the moment you try to load the user by the same params[:id] as the chair your show method. Change your show method to:
def show
#chair = Chair.find(params[:id])
#user = #chair.user
end
I am trying to create a policy so that only admins can acces a page. I've already managed to get pundit to work in another controller, but for some reason this policy wont work.
I've created a controller: users_controller.rb which is as follows:
def index
#user = current_user
authorize #user
end
end
I've created a Policy user_policy.rb which is:
def initialize(current_user, record)
#user = current_user
#record = record
end
def index?
#user.admin?
end
end
Any idea's what's going wrong?
I messed up, it routes to the users page, not an index.
Problem solved by changing
def index
to
def users
And do the same for the policy method
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'm working on a blog like application,
my user module has_many posts and the posts module belongs_to user
I want to access both users/:id/posts and posts/
routes.rb is something like this:
resources :users do
resources :posts
end
resources:posts
how can i know within the posts controller if its accessed directly (/posts) or through the nested route (/users/:id/posts) ?
for example, what should be the index method of the posts controller for doing the correct INDEX action for /users/:id/posts and for /posts
is there a better way for doing this ?
One solution could be to use a before filter on your controller, like:
before_filter :load_user
def load_user
#user = User.find(params[:user_id]) if params[:user_id]
#posts = #user ? #user.posts : Post.all
end
Then you have to rewrite your controller a bit to function properly.
No refactoring needed on index action, #posts already loaded correctly, but you can do further filtering as you like
def index
#posts = #posts.where('updated_at < ?' Time.now)
end
Then update every member action: new, create, show, edit, update, destroy and use posts as a base like:
def new
#post = #posts.build
end
def create
#post = #posts.build(params[:task])
end
def show
#post = #posts.find(params[:id])
end
def edit
#post = #posts.find(params[:id])
end
def update
#post = #posts.find(params[:id])
end
def destroy
#post = #posts.find(params[:id])
end
Of course you can add other before filters to remove duplicate code.
Check the params.
If just post you'll just have :id
If user/post you'll have user and ID for post.
So check if params[:user]...
n.b. If not user, try params[:user_id]
As for the index method for posts I think it will actually be the SAME in both cases. What will change things is its usage, association and scoping within user.