Missing Partial Error in Rails 3 - ruby-on-rails

I am getting an error:
Missing partial post/questions, application/questions with
{:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.
Searched in:* "/Users/..../<project name>/app/views"
I tried to render all posts in the database to index.html.erb.
My view part is post/index.html.erb:
<!--html codes -->
<%= render 'questions' %>
Controller controller/post_controller.rb:
def index
#posts=Post.all
end
def questions
end
questions.html.erb
<%=#posts.each do |post| %>
<table>
<tr>
<td>
<h2>
<%=post.title%>
</h2>
</td>
</tr>
<tr>
<td>
<h3><%=post.body%></h3>
</td>
</tr>
<tr>
<td>
This Post comes under:<h4><%=post.tag%></h4>
</td>
</tr>
</table>

Partials' file names must begin with an underscore. You should have _questions.html.erb saved in the post folder. Also, you don't need to define a 'questions' action.

Related

Controller fails to pass instance variables into view

In my home page I iterate over collections of objects, and for each object I render its attributes in a table row. There are four collections of objects, defined as instance variables in my controller, all making Guard (according to the used method) raising one of the following errors:
ActionView::Template::Error: undefined method `each_with_index' for nil:NilClass
ActionView::Template::Error: undefined method `any?' for nil:NilClass
The code in my application view raising the above errors is:
<table class="col-md-4 col-md-offset-1">
<thead>
<tr>
<th> Rank </th>
<th> Gamer </th>
<th> Points </th>
</tr>
</thead>
<tbody>
<% #atp_gamers.each_with_index do |gamer, index| %>
<tr>
<td class="index"> <%= index+1 %> </td>
<td class="gamer"> <%= gamer.name %> </td>
<td class="atppoints"> <%= gamer.atpscore %> </td>
</tr>
<% end %>
<tr class="current-user">
<td> <%= #atp_gamers.to_a.index(current_user) + 1 %> </td>
<td> <%= current_user.name %> </td>
<td> <%= current_user.atpscore %> </td>
</tr>
</tbody>
</table>
<table class="col-md-4 col-md-offset-2">
<thead>
<tr>
<th> Year </th>
<th> Champion </th>
<th> Points </th>
</tr>
</thead>
<tbody>
<% if #atp_champions.any? %>
<% #atp_champions.each do |champion| %>
<tr>
<td class="year"> <%= champion.created_at.year %> </td>
<td class="winnername"> <%= champion.name %> </td>
<td class="winnerpoints"> <%= champion.points %> </td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
The above code is part of a partial (named _gamers_home.html.erb) rendered in the original home page:
<% if logged_in? %>
<% if current_user.gamer? %>
<%= render 'static_pages/gamers_home' %>
<% else %>
<%= render 'static_pages/non_gamers_home' %>
<% end %>
<% else %>
<%= render 'static_pages/non_logged_in_home' %>
<% end %>
The logged_in? method is defined as !current_user.nil?
The instance variables that result nil are: #atp_gamers, #wta_gamers, #atp_champions and #wta_champions, defined in the controller as follows:
def home
if logged_in? && !current_user.gamer?
...l
elsif logged_in? && current_user.gamer?
#gamers = User.where(gamer: true)
#atp_gamers = #gamers.order(atpscore: :desc).limit(50)
#wta_gamers = #gamers.order(wtascore: :desc).limit(50)
#atp_champions = AtpChampion.all
#wta_champions = WtaChampion.all
...
end
end
The first instance variable raising the error (each_with_index' for nil:NilClass) is #atp_gamers. In view I tried to change it with its explicit value, that is User.where(gamer: true).order(atpscore: :desc).limit(50), and the respective code is accepted. After this change, Guard raises an error for #atp_champions.
With rails console #atp_gamers and #wta_gamers are not empty, returning 50 records out of 100 users. #atp_champions and #wta_champions are not nil, but empty arrays.
I suspect that this might be an issue raised only by Guard, because the rails server succeeds in rendering the view.
def home
if logged_in? # delete this line
...
end # delete this line
end
Delete the if logged_in?, and see what happens.
Maybe you have to use before_action :logged_in_user, only :home in controller and define the logged_in_user method as private method.
If non-logged-in users also allowed to access the home action, you need to use if statement erb in the view. Like,
<% if logged_in? %>
<% #atp_gamers.each_with_index do |gamer, index| %>
...
<% end %>
--UPDATE--
Maybe, it needs to toss variables to the partial.
Replace
<%= render 'static_pages/gamers_home' %>
to
<%= render partial: 'static_pages/gamers_home', locals: {atg_gamers: #atp_gamers, wta_gamers: #wta_gamers, atp_champions: #atp_champions, wta_champions, #wta_champions} %>
and, replace the #atp_gamers, #wta_gamers, #atp_champions, #wta_champions in the partial to atp_gamers, wta_gamers, atp_champions, wta_champions.
Try and see what happens.

Rails 4: Generated URL and Path. How to generate and show the corresponding view?

I'm new to Rails and have an issue that must be really simple but I can't work it out / find a solution on here.
According to this tutorial http://guides.rubyonrails.org/routing.html I tried creating a link for each entry (link to a matchthread) on the homepage. However, when I click on the links, I either get
Template is missing
Missing template matchthreads/show, application/show with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * blabla
or
Unknown action
The action 'show' could not be found for MatchthreadsController
The homepage view looks like this:
<h1>Current Match Threads</h1>
<table>
<tr>
<th>Title</th>
<th>Author</th>
<th>Kickoff</th>
</tr>
<% #match_threads.each do |thread| %>
<tr>
<!--<td><%= thread.title %></td>-->
<td><%= link_to thread.title, matchthread_path(thread.title) %></td>
<td><%= thread.author %></td>
<td><%= thread.kickoff %></td>
</tr>
<% end %>
</table>
<br>
The routes like this:
Rails.application.routes.draw do
get 'matchthreads/matchthread'
get 'home/index'
root 'home#index'
get '/matchthreads/:title', to: 'matchthreads#show', as: 'matchthread'
And the controller for the "thread-view" like this:
class MatchthreadsController < ApplicationController
def matchthread
#matchthread = Matchhreads.find(:title)
end
end
I hope you can help! Thanks!

Rails 3.2 group count with Kaminari

I am trying to display in my a table with most popular tag words, ordered by the count of impression. Then add kaminari to paginate through the records.
in my controller I tried:
#tag_answers = Tag.group(:content).page(params[:page]).order("count_all DESC").count
and in my view:
<table>
<thead>
<th>Tag</th>
<th>Count</th>
</thead>
<tbody>
<% #tag_answers.each do |tag_content, tag_count| %>
<tr>
<td> <%= tag_content %> </td>
<td> <%= tag_count %> </td>
</tr>
<% end %>
</tbody>
</table>
<%= paginate #tag_answers %>
but i am getting the following error
undefined method `current_page' for #<ActiveSupport::OrderedHash:0x000001031b8678>
Try
#tag_answers = Tag.group(:content).select('content, COUNT(*) as count').order("count DESC").page(params[:page])

Rails: DropDown Selection to Controller Action

Good Day, i have this form view/startseites/index.html.erb and i specified a methode in my people_controller, she doesnt go. I prepared some ghost code for understanding. I want to give the entries from dropdowns with the button_to tag to the controller action checkValid. There i want to validate the entries against the database. I have to read and write from and to the table. I hope its clearly enough.
class PeopleController < ApplicationController
def checkValid
#trainerName = params[:trainer_name]
#sportlerName = params[:sportler_name]
#trainerPID = params[:trainer_pid]
#sportlerPID = params[:sportler_pid]
#checks if sportlerID is null
#person = Person.find(params[:sportler_pid])
id = Person.sportler_id
if id = nil then
Person.sportler_id = params[:trainerPID]
else
puts "Sportler can have only one Trainer!"
end
end
...
view/startseites/index.html.erb: this code doesnt go
this should send the drop down selection to the controller action checkValid(). how can i use parameters?
<%=button_to( "Zuordnung erstellen", :action => "checkValid", :controller =>"people" %>**
<table>
<tr>
<td colspan="3">
Jeder Sportler kann ein Trainer haben. </br>
</td>
</tr>
<tr>
<td>Trainer</td>
<td>
<%= collection_select(:trainer, :trainer_id, Trainer.all, :id, :name) %>
</td>
<td>
<%= link_to 'Neuer Trainer', new_person_path(:type => "Trainer") %>
</td>
<tr>
<tr>
<td>Sportler</td>
<td>
<%= collection_select(:sportler, :sportler_id, Sportler.all, :id, :name) %>
</td>
<td>
<%= link_to 'Neuer Sportler', new_person_path(:type => "Sportler") %>
</td>
<tr>
<tr>
<td></td>
<td></td>
<td>
**<%=button_to( "Zuordnung erstellen", :action => "checkValid", :controller => "people") %>**
</td>
<tr>
</table>
i added this line to my routes
match '/people/checkValid', :controller => 'people', :action => 'checkValid'
but: No route matches {:controller=>"people/checkValid", :method=>:checkValid}
no i think it goes but
Template is missing
Missing template people/checkValid, application/checkValid with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "C:/Users/jord/testmood/app/views"
The Missing template error refers to a missing view. You should have check_valid.html.erb view file in app/views/people/ directory.
Also, run rake routes on the command line anywhere within your app's directory. You will receive a list routes that are generated by your routes.rb file. You can double-check there if people#checkValid exists.
Btw, you might want to change checkValid to check_valid so you follow the naming convention for actions in Rails.

form_remote_tag select_tag parameter not passed

I have problem with my form_remote_tag. it did not pass the parameters.Have been trying solutions from the net and from the ones here, but to no avail.
<%=form_remote_tag(:url=> {:action=>"showteam"},:update=>"display_div") do -%>
<tr>
<th colspan="2" scope="col">Choose team </th>
</tr>
<tr>
<td align="center">
<%=select_tag ('team_id',options_for_select(#teams.collect{|t| [t.name,t.id]}))%>
</td>
<td><%=submit_tag "Show"%> </td>
</td>
</tr>
</table>
<%=end_form_tag%>
<div id="display_div"></div>
that is my list.rhtml view. Here's my controller.
def showteam
#team = params[:team_id]
$logger.info("#{Time.now} Received rm12 >> #{#team.inspect} >> #{params[:team_id]} #{session[:user_name]} ")
render(:layout => false)
end
SO from the controller, i get nil value for #team.inspect.
is it the .rhtml file?must i do partial?
Thank u.
OK i think i have solved the problem.
It is indeed because of the HTML table tags.
It seems that they have certain order for my codes to work eg so that the params are being passed.
here's the link for more details.
Thank u for those who tries to help. ;)

Resources