Rails: DropDown Selection to Controller Action - ruby-on-rails

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.

Related

Implementing sort ASC or DESC in Rails

I am working with an open source school management software, Fedena and I am trying to sort a list of users by their surnames. The software currently shows all users with their first names. I've found these two files to be responsible for showing the information I intend to change.
student_controller.rb
def list_students_by_course
#students = Student.find_all_by_batch_id(params[:batch_id], :order => 'last_name ASC')
render(:update) { |page| page.replace_html 'students', :partial => 'students_by_course' }
end
When I delete the above section of the file, the users names don't show up so I believe this section to be responsible for populating the table with the usernames.
_students_by_course.erb
<div class="students-table">
<table align="center" width="100%" cellpadding="1" cellspacing="1">
<tr class="tr-head">
<td><%= t('sl_no') %></td>
<td><%= t('name') %></td>
<td><%= t('adm_no') %></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<% #students.each_with_index do |r, i| %>
<tr class="tr-<%= cycle('odd', 'even') %>">
<td class="col-1">
<%= i+1 %>
</td>
<td class="col-2">
<%= link_to r.full_name,:controller => "student", :action => "profile", :id => r.id %>
</td>
<td class="col-1">
<%= r.admission_no %>
</td>
<td class="col-7">
<%= link_to "#{t('view_profile')}", :controller => "student", :action => "profile", :id => r.id %>
</td>
</tr>
<% end %>
</table>
</div>
I have tried changing 'last_name ASC' to 'last_name DESC' but nothing changed.
Any help is appreciated.
You've clarified in the comments that you want not sort order (I take it you've figured that out?) but the format of full name displayed for each instance of a model Student.
It's not hard if traced to the source. Let's start with a view, since that's what we see.
#students.each_with_index do |r, i|
We start a loop, each iteration of which processes an entry r with an index i. Since we're looping on a collection of Students (seems like a valid assumption), r is an instance of Student. The problematic line is:
<%= link_to r.full_name,:controller => "student", :action => "profile", :id => r.id %>
Actually, we should only look at r.full_name since that's what we get as a link label. It's a Student's method. Here it is.
def full_name
"#{first_name} #{middle_name} #{last_name}"
end
You might be wondering where is a second space, since such implementation implies that if middle_name is absent, we'd have two. Look at the source of the page and you'll see that there are actually two! So in order to change how the full name looks, you'll have to modify this method.

Missing template in RoR application

In my ruby on rails application, I want to get controls value into controllers page:
below is view page:
<%= form_for :managerviewresume, :url => { :method => :post, :action => :managerviewresume }) do |f| %>
<table class="table" width="100%">
<tr>
<td><%= #selection %></td> //Here I am checking radio button value
<td>
<label style="font-size:small;">Selected?</label>
<label class="radio inline" style="font-size: small">
</br>
<%= f.radio_button :select, "Yes", :id => "rb_select1" %>
Yes
</label>
<label class="radio inline" style="font-size: small">
<%= f.radio_button :select, "No", :id => "rb_select2" %>
No
</label>
</td>
</tr>
<tr>
<td>
<%= f.submit "Save", { :class => "stylbutton" } %>
</td>
</tr>
</table>
<% end %>
below is controllers page:
class ManagersController < ApplicationController
def managerviewresume
#selection = params[:select]
render "managerviewresumes"
end
end
In the controller's page I am getting below error at this line render 'managerviewresumes' :
Missing template managers/managerviewresumes, application/managerviewresumes with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "C:/Sites/VideoResume/app/views"
below is the route:
match '/managerviewresumes', to: 'managers#managerviewresume', via: 'get'
Kindly suggest what should I do get radio button value from view page into controller page.
waiting for reply.
Thanks.
Render
Firstly, you don't need to use render to render the same view as your action name
When using a Rails controller, you can call this without issue:
#app/controllers/your_controller.rb
Class YourController < ApplicationController
def your_action
#-> will automatically render the "your_action" view
end
end
So I would remove your reference to the render action (as it is an unnecessary step). This will not resolve the issue directly, but should ensure your application is more convention over configuration
--
Routes
Secondly, you may need to look at resourceful routing
In Rails' routing structure, you are able to call resources :controller to generate a series of RESTful routes:
I understand you likely want to keep using your action, but for the sake of correctness, can I recommend you look into your config/routes.rb file & ensure you're using as many resource-based routes as possible:
#config/routes.rb
resources :managers do
collection do
post :managerviewresume
end
end
--
Form
Finally, I think your form needs to be improved
You're using form_for, which is mainly for ActiveRecord objects (if you want to create new record etc. It seems you'll be better suited to using form_tag instead:
<%= form_tag managers_managerviewresume_path do %>
<table class="table" width="100%">
<tr>
<td><%= #selection %></td> //Here I am checking radio button value
<td>
<label style="font-size:small;">Selected?</label>
<label class="radio inline" style="font-size: small">
</br>
<%= radio_button_tag :select, "Yes", :id => "rb_select1" %>
Yes
</label>
<label class="radio inline" style="font-size: small">
<%= radio_button_tag :select, "No", :id => "rb_select2" %>
No
</label>
</td>
</tr>
<tr>
<td>
<%= submit_tag "Save", { :class => "stylbutton" } %>
</td>
</tr>
</table>
<% end %>
This syntax might need checking, but this will send the :select params as required, which is not what your current form will be doing.
This should be coupled with a views/managers/managerviewresume.html.erb file for Rails to load

Get id of an object to pass into form_for

I am trying to find my time sheet id to pass into a form_for
here is my controller
def show
#time_id = current_user.time_sheets.find(params[:id])
if current_user
#current = current_user.time_sheets
else
redirect_to new_user_session_path, notice: 'You are not logged in.'
end
end
This is my form_for in my view:
<%= form_for(:entry, :url => {:controller => Entry, :action => 'create'}) do |f| %>
<table summary="Subject form fields">
<tr>
<th>Customer</th>
<td><%= f.text_field(:customer_name) %></td>
</tr>
<tr>
<th>Order Number</th>
<td><%= f.text_field(:order_number) %></td>
</tr>
<tr>
<th>Time In</th>
<td><%= f.text_field(:time_in) %></td>
</tr>
<tr>
<th>Time Out</th>
<td><%= f.text_field(:time_out) %></td>
</tr>
<tr>
<th>Time Sheet ID</th>
<td><%= f.hidden_field :time_sheet_id, value: #time_id.id %></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Add Entry") %>
</div>
<% end %>
What needs to happen is that the timesheet id needs to get passed into the form_for so the entry can have the timesheet id.
I have user, timesheets and entries. user has_many time sheets, timesheets belongs to users and users have many time sheets. Entry belongs to timesheet.
I am getting this error "Couldn't find TimeSheet without an ID"
You should use "Nested Resources", eg. see here: Rails 3: How to create a new nested resource?
You could use form_for([#timesheet, #entry]) to pass the id of the timesheet without using a hidden field. If you do this, the :url param also become obsolete.
Try adding:
<%= #time_id.id %>
to output your variable and make sure it's exactly what you think it is. If #time_id is a timesheet, you should probably rename that variable to #time_sheet.

Missing Partial Error in Rails 3

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.

Vestal_versions : List all the versions of the edit in the rails app

i am using vestal_version in my rails app. And i am wondering how do I show the previous versions edit links with respect to what is at the above entry. My html.erb code is
<tbody>
<% #page.versions.each do |page| %>
<tr class="odd">
<td><input type="checkbox" /></td>
<td> </td>
<td><%= page.created_at%></td>
<td>
<% if params[:version] %>
<%= link_to "Previous Version", :version => #page.version-1 %>
<%end%>
</td>
</tr>
</div>
<% end %>
and in controller i have #page.revert_to(params[:version].to_i) if params[:version]
I have attached a link to the screenshot. And if closely observe. the ?version=5 for all the entries. I want to have all the versions that is 1,2,3,4 and 5. How do i do that ?
Update : Image is hyper linked in the comment. I am not allowed add an image as of now.
Use page instead of #page.
<%= link_to "Previous Version", :version => page %>
page changes with each iteration, while #page stays the same.
EDIT:
Since #page.versions.each iterates a collection of versions, it should be passed into the block as version, not page.
<% #page.versions.each do |version| %>
<tr class="odd">
<td><input type="checkbox" /></td>
<td> </td>
<td><%= version.created_at%></td>
<td>
<%= link_to "Show Version", :version => version %>
</td>
</tr>
<% end %>
This method only shows the version. Actually reverting a version should be done via POST, since you are making changes to the database.
Reverting negates changes, it does not discard changes. If page is at version 4, and you revert to version 2, it does not delete versions 3 and 4. The version number will revert to 2, but will count as a version itself.
#revert_to does the reversion but does not save. To revert and save, use #revert_to!
To properly use #revert_to!, you need to put it in a controller action.
Example, in your pages controller:
def revert
#page = Page.find params[:id]
if #page.revert_to!(params[:version_id]) # <= revert_to!
redirect_to #page
else
render :text => "Did not revert"
end
end
In your routes.rb:
resources :pages do
member do
post 'revert_to/:version_id' => 'pages#revert', :as => :revert
end
end
then in your view:
<% #page.versions.each do |version| %>
<tr class="odd">
<td><input type="checkbox" /></td>
<td> </td>
<td><%= version.created_at %></td>
<td>
<%= link_to "Show Version", :version => version %>
</td>
<td>
<%= button_to "Revert to this version", revert_page_path(#page, version.id) %>
</td>
</tr>
<% end %>

Resources