I have the following action method:
class CommandsController < ApplicationController
.
.
.
def usersList
#command = Command.find_by(id: params[:id])
#users_list = #command.user_commands.paginate(page: params[:page], per_page: 10)
end
end
The following view: users_list.html.erb
<% provide(:title, "Command list") %>
<h1>Lista Utenti per <%= #command.content %></h1>
<%= will_paginate #users_list %>
<ul class="users">
<% #users_list.each do |user| %>
<li><%= user.name.capitalize %>, <%= user.email %></li>
<% end %>
</ul>
And in the routes.rb
get 'commands/:id/usersList' => 'commands#users_list', as: :list
Now I don't understand why going to lochalhost:3000/commands/1/usersList I receive the following error:
undefined method `content' for nil:NilClass
refered to line <h1>Lista Utenti per <%= #command.content %></h1>
Why does it do so? When I call an action, before it renders the page and then after it executes the code inside the method?
I resolved the problem by changing the names: usersList --> userslist and then users_list.html.erb --->userslist.html.erb and in the routes.rb ----> get 'commands/:id/userslist' => 'commands#userslist', as: :list
Then I don't understand the naming convention...if I declare an action usersList how should I rename the related view?
You should change your action name to users_list.
Related
I'm building a micropost rails app where users can create posts, I created a route and an action to display posts that belong to the signed-in user, happens that the general index layout for posts is exactly the same as the "myposts" layout so instead of duplicating code I would like to use just one layout with different parameters.
This is what I have until now:
routes.rb
resources :posts
get '/myposts', to: 'posts#my_posts', as: 'myposts'
posts_controller.rb
def index
#posts = Post.all
end
def my_posts
#myposts= Post.where(user_id: current_user.id)
end
index.html.erb
<% #posts.each do |post| %>
<div>
<h1><%= link_to post.title, post %></h1>
<%= link_to image_tag(post.meme_url(:thumb)), post, :target => "_blank" %>
</div>
<% end %>
my_posts.html.erb
<% #myposts.each do |post| %>
<div>
<h1><%= link_to post.title, post %></h1>
<%= link_to image_tag(post.meme_url(:thumb)), post, :target => "_blank" %>
</div>
<% end %>
Thanks in advance!
You can use 'render' on 'my_posts' action - http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
Add before 'end' in my_posts action:
render :index
I just started learning rails. I have a doubt in the following section.
Controller:
book_controller.rb
class BookController < ApplicationController
end
View:
list.html.erb
<% if #books.blank? %>
<p>There are not any books currently in the system.</p>
<% else %>
<p>These are the current books in our system</p>
<ul id="books">
<% #books.each do |c| %>
<li><%= link_to c.title, {:action => 'show', :id => c.id} -%></li>
<% end %>
</ul>
<% end %>
Router:
get 'list' => 'book#list'
When I goto localhost:3000/list, it displays the content of list.html.erb. How does that happen when I don't have an list action in Controller? How is my understanding wrong?
If the #books is defined in the book controller there is no trouble executing this code since the list.html.erb is already define inside the books folder and the route is set as
get 'list' => 'book#list'
Ruby MVC structure is integrated such that when you create a variable #varibale it is already available at view.
I created a new controller called browse and this is what I have inside it
class BrowseController < ApplicationController
def index
#hashtags = Hashtag.all
end
end
I'm trying to display all of the hashtags in view using this (views\browse\index.html.erb)
<ul>
<% #post.hashtags.each do |h| %>
<li><%= h.hashtags %></li>
<% end %>
</ul>
Here's the error that I'm getting
NoMethodError in Browse#index
undefined method `hashtags' for nil:NilClass
My routes.rb
get'/browse' => 'browse#index', :as => :index
How can I fix this error to display the list in browse.html.erb?
And if I wanted to display them in descending order of date, where do I need to put that?
You should use code in index view as follows:
<ul>
<% #hashtags.each do |h| %>
<li><%= h.{field_name} %></li>
<% end %>
</ul>
Where {field_name} must be the name of field included in Hashtagm model.
You did #hashtags = Hashtag.all in the controller, while you do #post.hashtags.each in your view.
Maybe you should do #hashtags.each in your view too.
This is my code for rendering the partial (the #parties collection is being generated correctly, I have tested that):
<% #parties.each do |party| %>
<div class="item">
<%= render 'parties/party', :object => party %>
</div>
<% end %>
And this is the code in the partial:
<%= party.name %>
However, I get the following error:
undefined method `name' for nil:NilClass
I'm at my wits end, someone please help :-|
Also, this is the code for the controller to render the view containing the partial (The controller's called default_controller):
def index
#parties = Party.all
end
Is it of any consequence that this isn't the parties_controller?
I've tried something like below and it worked
<%= render :partial => 'party', :object => party %>
and I can access like party.name. the local variable is named after the partial name which is party here.
Note: Im assuming that your both partials are of parties_controller. So this should work.
Update:
Here is what ive tried with again
class PostsController < ApplicationController
#... ...
def index
#posts = Post.all
#comments = Comment.all #<---- Loading comments from PostsController
#... ...
end
end
#views/posts/index.html.erb
<% #comments.each do |comment| %>
<%= render :partial=>"comments/comment", :object=>comment %>
<% end %>
#views/comments/_comment.html.erb
<%= comment.body %>
And its working :)
I am trying to ensure that each job has the correct link when a user clicks on it.
The issue I am having is that I keep getting the error Couldn't find job without an ID.
In my view I have the following code:
<% #jobs.group_by{|x| x.created_at.strftime("%d %b. %Y")}.each do |date,jobs_on_that_date| %>
<section class="date"><%= date %></section>
<section id="top-job">
<% jobs_on_that_date.each do |job| %>
<section id="job-wrapper">
<%= link_to url_with_protocol(#link.job_url), :target => '_blank' do %>
<section id="job">
<%= job.title %> - <%= job.company %>
<%= job.salary %>
<%= job.location %>
<%= job.job_type %>
</section>
</section>
<% end %>
<% end %>
</section>
<% end %>
In my controller I am creating an object called #link as follows:
def index
#user = current_user
#jobs = Job.where('created_at > ?', 30.days.ago).reverse
#link = Job.find(params[:id])
end
Finally I have the following Routes setup.
JobappV2::Application.routes.draw do
devise_for :users
resources :jobs
resources :users do
resources :jobs
end
root :to => 'jobs#index'
end
If I use #link = Job.find(2) this works but every job ends up with the link input by the job with id 2.
Thanks in advance for any help!
what is params[:id]?
I suspect that if you do raise params[:id].inspect in your controller, you will find it's nil. Besides, what's the point in creating #link? just put job.job_url in your view instead of #link.job_url