Unable to pass param on clicking button in rails - ruby-on-rails

I have a controller models_controller.rb as following:
class ModelsController < ApplicationController
def list
#models=["A","E","M","I"]
end
def get_config
Rails.logger.info(params[:model_id])
//some logic based on params[:model_id] value
end
end
The corresponding list.html.erb is as follows:
<table>
<thead>
<tr>
<th>Model</th>
<th></th>
</tr>
</thead>
<tbody>
<% #models.each_with_index do |model, index| %>
<tr>
<td><%= model %></td>
<td><%= button_to "view", {:controller => :models, :action => :get_config, :model_id => index }, :method => :get %></td>
</tr>
<% end %>
</tbody>
</table>
Each row of this table has a button, where upon clicking, I am passing the index of the element. But I am not been able to access it in the get_config method. Logger is printing an empty string. routes.rb is as follows:
DemoApp::Application.routes.draw do
get "models/list"
get "models/get_config"
get "models/list/:model_id" => "models#get_config"
end
What am I doing wrong? I am not able to figure that out.

Rails routes are matched in the order they are specified. So get "models/get_config" is catching the request before get "models/list/:model_id" => "models#get_config". Try changing the order to this:
DemoApp::Application.routes.draw do
get "models/list"
get "models/list/:model_id" => "models#get_config"
get "models/get_config"
end

Related

Facing issues with link_to "Edit" in rails

I am trying to display all the metrics in a table format with an edit option. But, I end up with the below error
In Index view, I am able to see all the data. But when I click on edit link, it is not redirecting to edit view where I have different columns to be showed.
index view:
<%= form_for :metrics_controller, url: metrics_path(#metric), method: :get do |f| %>
<table id="metrics">
<thead>
<tr id="AllMetricColumnNames">
<th id="CommentsColumn">Comments</th>
<th id="EditColumn">Edit</th>
</tr>
</thead>
<% #metricAll.each do |data| %>
<tr id="AllMetricValues">
<td id="Comments"><%= data.Comments %></td>
<td id="EditButton"><%= link_to "Edit", edit_metric_path(#metricAll) %></td>
<% end %>
</tr>
</table>
<% end %>
Controller:
class MetricsController < ApplicationController
def index
#metricAll = Metric.all
end
def show
#metric = Metric.find(params[:id])
end
def edit
#metric = Metric.find(params[:id])
end
private def post_params
params.require(:metric).permit(:Metric, :Comments)
end
end
routes:
root 'metrics#index'
get 'index' => 'metrics#index'
get 'edit' => 'metrics#edit'
resources :metrics
You're passing ALL the metrics for the edit route. Move from
<td id="EditButton"><%= link_to "Edit", edit_metric_path(#metricAll) %></td>
to
<td id="EditButton"><%= link_to "Edit", edit_metric_path(data) %></td>
data is the current metric in your code
According to your screenshot, the error is within the model.
Also, as mentioned by others, you should remove those get routes as the resources :metrics will generate the necessary routes for all your CRUD actions a.ka. for the index, show, edit, new, create, update, destroy.
My guess is that the metric.rb file has a belongs_to :automated_thresholding relationship but the metrics database table is missing the field automated_thresholding_id.
You should create a migration to add that field
add_reference :metrics, :automated_thresholding

CRUD in ruby on rails. Routing

Working through the "Ruby on rails 3 essential training" I'm encountering an issue with my routes and URL's. I have created a sujects_controller with a 'list' action and a 'show' action. Within my localhost, when I enter in:
localhost:3000.subjects/list, I get an error, 'couldn't find Subject with id=list'
But, when I enter in:
localhost:3000.subjects. I get the list that should have come up when entering the first URL. Also, after entering this second URL, I have links for Show, Edit, and Delete. When I hover my mouse over the link "Show", at the bottom of the page, the future output reads localhost.3000/subjects(3). This link works. But, according to my instructors tutorials, it should read, localhost.3000/subjects/show/3. For some reason my links and/or actions, aren't working appropriately and I'm not sure why since I'm following the instructions.
Here is my subjects_controller.rb file:
class SubjectsController < ApplicationController
def index
list
render('list')
end
# def index
# show
# render('show')
# end
def list
#subjects = Subject.order("subjects.position ASC")
##subjects = Subject.all
end
def show
#subjects = Subject.find(params[:id])
end
def new
#subjects = Subject.new
end
def create
#subject = Subject.new(params[:subject])
if #subject.save
redirect_to(:action => 'list')
else
render('new')
end
end
end
Here is my list.html and show.html file:
<div class="subject list">
<h2>Subjects</h2>
<table class="listing" summary="Subject list">
<tr class="header">
<th> </th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<% #subjects.each do |subject| %>
<tr>
<td><%= subject.position %></td>
<td><%= subject.name %></td>
<td class="center"><%= subject.visible ? 'yes' : 'no' %></td>
<td class="center"><%= subject.pages.size %></td>
<td class="actions">
<%= link_to("Show", {:action => 'show', :id => subject.id}, :class => 'action show') %>
<%= link_to("Edit", '#', :class => 'action edit') %>
<%= link_to("Delete", '#', :class => 'action delete') %>
</td>
</tr>
<% end %>
</table>
Show.html
<%= link_to("<< Back to list", {:action => 'list'}, :class => 'back-link') %>
<div class="subject show">
<h2>Show Subject</h2>
<table summary="Subject detail view">
<tr>
<th>Name</th>
<td><%= #subject.name %></td>
</tr>
<tr>
<th>Position</th>
<td><%= #subject.position %></td>
</tr>
<tr>
<th>Visible?</th>
<td><%= #subject.visible ? 'true' : 'false' %></td>
</tr>
<tr>
<th>Created</th>
<td><%= #subject.created_at %></td>
</tr>
<tr>
<th>Updated</th>
<td><%= #subject.updated_at %></td>
</tr>
</table>
</div>
And my config/routes.rb file:
Rails.application.routes.draw do
root :to=>"demo#index"
get 'demo/index'
get 'demo/hello'
get 'demo/other_hello'
resources :subjects
get 'subjects/list' => 'subjects#list'
get 'subjects/list' => 'subjects#show'
get 'subjects/create' => 'subjects#create'
The instructor never shows his routes.rb file so I just started using the gets in order to get my actions to work. The resources :subjects wasn't able to take care of everything so I used GET. Also in my subjects_controller I def:index to render list. If I don't do that nothing works at all. I'm obviously new to all of this but love learning about it and any information is appreciated. I have looked at the rails routing from the guides but that hasn't helped me much. Its more the concept/theory of why my links/actions aren't working that I'd be very interested to hear about. Thanks for any info!
When you specify in routes
resources :subjects
It creates seven actions( index, new, create, create, show, edit, update, destroy)
When you call localhost:3000/subjects it ask routes where to go. And index action is called
/subjects subjects#index display a list of all subjects
And when you are saying localhost:3000/subjects/list
It expect id after /subjects as there are routes mapped to that
localhost:3000/subjects/5 when you will call this it will call show method in controller asking for record with id 5 in database
/subjects/:id subjects#show
If you want to see all the routes, from console type
rake routes
And if you want to define extra method like list as below
get 'subjects/list' => 'subjects#list'
and remove all other routes for list from routes file
And there are many more options to specify routes and to learn that this is your best friend
http://guides.rubyonrails.org/routing.html
The issue is your routes.rb file. When you specify resources :subjects you're adding many routes, one of which is get 'subjects/:id' => 'subjects#show'
Since routes take precedence depending on the order you write them (top to bottom) this is overwriting your get 'subjects/list' => 'subjects#list' line.
You also have many other redundant routes that are being overwritten there.
get 'subjects/list' => 'subjects#list'
get 'subjects/list' => 'subjects#show'
subjects#show won't ever be called because the route calling subjects#list will take precedence
It's because you first decleared
resources :subjects
and then the other routes.
In your case the best way to add routes to a resource is the following:
resources :subjects do
collection do
get :list, to: :show
end
end
This will generate:
# rake routes
list_subjects GET /subjects/list(.:format) subjects#show
subjects GET /subjects(.:format) subjects#index
POST /subjects(.:format) subjects#create
new_subject GET /subjects/new(.:format) subjects#new
edit_subject GET /subjects/:id/edit(.:format) subjects#edit
subject GET /subjects/:id(.:format) subjects#show
PATCH /subjects/:id(.:format) subjects#update
PUT /subjects/:id(.:format) subjects#update
DELETE /subjects/:id(.:format) subjects#destroy

Ruby on rails-Tabular form for input taken from user

I want to print in tabular form the entries given by user. The table will contain 3 columns for name,registration number and classes attended, however the output is not as expected. Its first printing all the names and then all the registration numbers and so on. I want to print a name and the registration number according to name and classes attended.
</head><table width="100%">
<tr>
<th>NAME</th>
<th>REGISTRATION NUMBER</th>
<th>CLASSES ATTENDED</th>
</tr>
<tr>
<% #name_students.each do |t| %>
<th><%= t.name_student %></th><br>
<% end %>
<% #reg_nos.each do |t| %>
<th><%= t.reg_no %></th><br>
<% end %>
<% #classes_ats.each do |t| %>
<th><%= t.classes_at %></th><br>
<% end %>
</tr>
</table>
Here is my controller action.
class PagesController < ApplicationController
def home
#name_students = Page.all
#reg_nos = Page.all
#classes_ats = Page.all
end
def list
#name_students = Page.all
#reg_nos = Page.all
#classes_ats = Page.all
end
def add
Page.create(:name_student => params[:nam])
Page.create(:reg_no => params[:reg])
Page.create(:classes_at => params[:cls])
redirect_to :action => 'home'
end
end
If I understand what you're doing, you should probably have PagesController#home return something like #pages = Page.all object and display the data kind of like this:
<thead>
<tr>
<th>Name</th>
<th>Registration Number</th>
<th>Classes Attended</th>
</tr>
</thead>
<tbody>
<% #pages.each do |p| %>
<tr>
<td><%= p.name %></td>
<td><%= p.registration_number %></td>
<td><%= classes_attended(p.classes_attended) %></td>
</tr>
<% end %>
</tbody>
Above, classes_attended(p) is a call to a helper method that you would use to map the class names of the classes that student attended into an display-able array. Really, that kind of display logic might be better in a decorator, but a helper method should be fine for now.
Let me know if I've totally misunderstood what you're doing, and I'll delete my answer.
Edit to add:
Example index method:
def home
#pages = Page.all
end
Also, looking at your question again, is there a reason you're creating three Page objects with one attribute each instead of one Page object with all three attributes? It should probably look something like Page.create(:name_student => params[:nam], :reg_no => params[:reg], :classes_at => params[:cls]). That's pretty much the only way the solution I posted will work. Again, though, I might be totally misunderstanding what you're going for.

Undefined method `time_series_path' error

I have this error when loading index.html.rb.
undefined method `time_series_path' for #<#<Class:0x007f6aac0d2a28>:0x007f6aac0d1358>
In routes.rb I have
namespace :viewer do
resources :time_series
end
In the TimeSeriesController I have
class Viewer::TimeSeriesController < ApplicationController
def show
#time_series = TimeSeries.find(params[:id])
end
def index
#time_series = TimeSeries.all.paginate(:page => params[:page], :per_page => 20)
end
end
In the index.html.rb I have
<h1>Listing of time series</h1>
<table >
<tr>
<th>Kind</th>
<th></th>
</tr>
<% #time_series.each do |t| %>
<tr>
<td><%= t.kind %></td>
<td><%= link_to 'Show', t %></td>
</tr>
<% end %>
</table>
<%= will_paginate #time_series %>
The error occurs for the link_to 'Show' line.
Any ideas on how to resolve this? Thanks.
If you use a namespace, the namespace becomes part of the route name.
viewer_time_series_path
not
time_series_path
To double check, you can print out the list of all the routes
rake routes
and grep to immediately check the name
rake routes | grep time_series
In your code, you either pass the namespace as array in the link_to helper along with the object
<%= link_to 'Show', [:viewer, t] %>
or (I prefer this solution) you write the corresponding path explicitly.

rows disappear when I try to add a form

So, i'm trying to follow along with this example. I'm trying to translate it to my own own project where I have a set of rows that are displayed based on the results of applying the search criteria in a first form. I'm trying now to put a second form around the results to provide the administrator with checkboxes to be able to edit several of the rows displayed at the same time. however, when i try to put a form around the results, the results disappear altogether.
Here's the relevant piece of my controller:
def index
#search = Distribution.workflow.search(params[:traits_searchable_search])
respond_to do |f|
f.html {
#report = #search.paginate(:page => params[:page])
render :action => 'index'
}
f.csv do
send_data #report.to_csv, :type => "text/csv", :filename => "distribution_workflow_report.csv"
end
end
end
the view is nothing special. but i'm trying to wrap this tag (i've also tried removing the :method => :put piece and it's worth noting that the path provided to the form_tag is the page that's being displayed for now until i figure out how i'm going to get the routing to work):
<% form_tag admin_distributions_workflows_path, :method => :put do %>
around this table:
<table class="standard-grid">
<tr>
<th class="first"></th>
<th>ID</th>
<th>Customer</th>
<th>Customer Email</th>
<th>Resume URL</th>
<th>Partner</th>
<th>Partner Email</th>
<th>Status</th>
<th>Assigned To</th>
<th>Comments</th>
<th></th>
</tr>
<% #report.each do |row| %>
<tr>
<td><%= check_box_tag "row_ids[]", row.id %></td>
<td>
<%= row.owner.id %>
</td>
....
</td>
</tr>
<% end %>
</table>
<% end %>
Since Rails 3 you have to use <%= format with form_for and form_tag
<%= form_tag

Resources