Trying to create a table that will display the results a users search that will allow the user to add the required result to a database table.
When I first created and tested the submit button was disabled, so I added disable_with: false to the submit action. But its still not firing.
What have I overlooked?
<tbody>
<% #stock_items.each do |stock| %>
<tr>
<td><%= stock.product_code %></td>
<td><%= stock.desc1.titleize %></td>
<td class="text-right"><%= stock.uoi %></td>
<td class="text-right"><%= strip_trailing_zero(stock.qty_free) %></td>
<td class="text-right"><%= number_to_gbp(stock.price)%></td>
<td class="text-right"><%= number_to_percentage(((stock.price-stock.cost_sop)/stock.price)*100,precision: 0)%></td>
<%= form_tag new_customer_opportunity_sop_header_sop_detail_path(#customer,#opportunity, #sop_header) do %>
<td><%= hidden_field_tag 'stock_id', stock.id %>
<%= submit_tag 'Add', data: { disable_with: false } %></td>
<% end %>
</tr>
<% end %>
</tbody>
Related
I'm having some trouble with check_box_tag and my method enable (a product can be disabled) in Ruby on Rails.
I created some checkbox so the user select the products that he wants to set enable = true.
Keeps returning the error "Couldn't find Product with 'id'=enable"
My method enable:
def enable
Product.update_all(["enable=?", true], :id => params[:selected_products])
redirect_to root_url
end
My routes.rb:
Rails.application.routes.draw do
get 'product_export/new'
get 'product_imports/new'
resources :users
resources :product_imports
resources :products do
collection do
post :import
post :export
post :enable
end
end
root to: 'products#index'
end
and finally my view with the table e the check_box_tag:
<%= form_tag enable_products_path, :method => :put do %>
<%= submit_tag "Enable products" %>
<table class="table table-striped table-responsive" id="productsTable">
<thead class="thead-inverse">
<tr>
<th/>
<th>Code</th>
<th>Enable</th>
<th>Bar code</th>
<th>Unit cost</th>
<th>Description</th>
<th>Family</th>
<th>Final</th>
<th>Measure</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #products.each do |product| %>
<tr>
<td><%= check_box_tag 'selected_products[]', product.id %></td>
<td><%= link_to (product.code), edit_product_path(product) %></td>
<td><%= product.enable %></td>
<td><%= product.bar_code %></td>
<td><%= product.unit_cost %></td>
<td><%= product.description %></td>
<td><%= product.family %></td>
<td><%= product.final %></td>
<td><%= product.measure %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
Thanks for the help!
Use where to find selected products and when to update them:
Product.where(id: params[:selected_products]).update_all(enable: true)
I want to create a table named groups that lists of each group. Inside of each group, I want to list the groups courses. The group has_many :courses, and the course belongs_to :group. My current attempt does not work, and I have no idea where to go from there
Current Attempt
courses_controller.rb
def index
#courses = Course.find_by(group: :group_id)
#groups = Group.all
end
index.html.erb
<table>
<thead>
<tr>
<th>Title</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #groups.each do |group| %>
<tr>
<td><%= group.title %></td>
<td><%= link_to 'Show', group %></td>
<td><%= link_to 'Edit', edit_group_path(group) %></td>
<td><%= link_to 'Destroy', group, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<tr>
<td>
<table>
<thead>
<tr>
<th>Title</th>
</tr>
</thead>
<tbody>
<% #courses.each do |course| %>
<tr>
<% if Lesson.exists?(user: current_user, course: course) %>
<td><%= link_to course.title, edit_lesson_path(Lesson.find_by(user: current_user, course: course)) %></td>
<% else %>
<td><%= link_to course.title, new_course_lesson_path(course) %></td>
<% end %>
<% if current_user.master? %>
<td><%= link_to 'Edit', edit_course_path(course) %></td>
<td><%= link_to 'Destroy', course, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% else %>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if current_user.master? %>
<%= link_to 'New Course', new_course_path %>
<% end %></td>
</tr>
<% end %>
</tbody>
</table>
Any help is appreciated :)
UPDATE
:group_id is the groups id for each created course, since the group has_many :courses The lesson belongs_to :course but that isnt important, as I just want the table, Thanks XP
As I said in my comment, what do you mean it isn't working? Are you getting any error messages? How have you structured you models? Have you created a migration to add group id to courses? What does your routes file look like - are courses nested under groups? If you don't provide this kind of information, its difficult to provide help.
If you look in the logs, you'll get some messages which will direct you to where things are going wrong. Have you tried running
Course.find_by(group: :group_id)
in a rails console. I suspect you'll find that this line is causing problems. Comment out this line and amend your erb as follows (I've not included all the code, but just to give you an idea). Also its not a good idea to use tables within tables.
<table>
<thead>
<tr>
<th>Title</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #groups.each do |group| %>
<tr>
<td><%= group.title %></td>
</tr>
<tr>
<td>
<table>
<thead>
<tr>
<th>Title</th>
</tr>
</thead>
<tbody>
<% #group.courses.each do |course| %>
<tr>
<% if Lesson.exists?(user: current_user, course: course) %>
<td><%= link_to course.title, edit_lesson_path(Lesson.find_by(user: current_user, course: course)) %></td>
<% else %>
<td><%= link_to course.title, new_course_lesson_path(course) %></td>
<% end %>
I am using cancan to authorize the user in controller,And in views i am using Can?to check if the user is authorize to see that function
index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Noticeboards</h1>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>society</th>
<th>Notice heading</th>
<th>Notice text</th>
<th>Active</th>
<th>Isdelete</th>
<th>Expire</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #noticeboards = #noticeboards.where(Isdelete: 0).find_each %>
<% #noticeboards.each do |noticeboard| %>
<tr>
<td><%= Society.find_by(id: noticeboard.id_society, Isdelete: 0).name %></td>
<td><%= noticeboard.notice_heading %></td>
<td><%= noticeboard.notice_text %></td>
<td><%= noticeboard.active %></td>
<td><%= noticeboard.IsDelete %></td>
<td><%= noticeboard.Expire %></td>
<td><%= link_to 'Show', noticeboard %></td>
<% if can? :update, noticeboard %>
<td><%= link_to 'Edit', edit_noticeboard_path(noticeboard) %> <% end %></td>
<% if can? :delete , noticeboard %>
<td>
<%= link_to 'Destroy', noticeboard, method: :delete, data: { confirm: 'Are you sure?' } %></td> <% end %>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if can? :create, #noticeboard %>
<%= link_to 'New Noticeboard', new_noticeboard_path %>
<% end %>
In this if i change :delete to anything else, say :Hello, its still checking can, and if i am not using anything, it gives error saying "wrong number of arguments" what is the error, why its not taking :delete function?
I have zero experience with Ruby, and I'm having the following issue:
I receive a "We're sorry, but something went wrong" error message when I try to login into my admin panel (mydomain.com/administrator).
Checking the logs I found out the following:
As well, I checked the login_controller.rb:
Here you have the template:
<h1><%= t(".title")%></h1>
<!-- Pages -->
<% if #pages.length > 0 %>
<h3><%= t(".lastets_pages")%></h3>
<table>
<thead>
<tr>
<th>#</th>
<th><%=t("activerecord.attributes.page.title")%></th>
<th><%=t("activerecord.attributes.page.slug")%></th>
<th><%=t("generic.user_id")%></th>
<th><%=t("generic.created_at")%></th>
<th><%=t("generic.updated_at")%></th>
<th><%=t("generic.actions")%></th>
</tr>
</thead>
<tbody>
<% #pages.each do |page| %>
<tr>
<td><%= page.id %></td>
<td><%= page.title %></td>
<td><%= page.slug %></td>
<td><%= page.user.username %></td>
<td><%= l(page.created_at, format: :long) %></td>
<td><%= l(page.updated_at, format: :long) %></td>
<td class="actions_links">
<%= link_to t("generic.edit"), edit_administrator_page_path(page) %>
<%= link_to t("generic.delete"), administrator_page_path(page), :confirm => t("generic.delete_confirmation"), :method => :delete, class: "delete" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<h3><%= t(".lastets_pages")%></h3>
<div class="alert alert-warning"> <%= t(".lastets_pages_empty")%> </div>
<% end %>
<!-- Notices -->
<% if #notices.length > 0 %>
<h3><%= t(".lastets_notices")%></h3>
<table>
<thead>
<tr>
<th>#</th>
<th><%=t("activerecord.attributes.notice.title")%></th>
<th><%=t("activerecord.attributes.notice.countries")%></th>
<th><%=t("activerecord.attributes.notice.slug")%></th>
<th><%=t("generic.user_id")%></th>
<th><%=t("generic.created_at")%></th>
<th><%=t("generic.updated_at")%></th>
<th><%=t("generic.actions")%></th>
</tr>
</thead>
<tbody>
<% #notices.each do |notice| %>
<tr>
<td><%= notice.id %></td>
<td><%= notice.title %></td>
<td><%= notice.show_countries %></td>
<td><%= notice.slug %></td>
<td><%= notice.user.username %></td>
<td><%= l(notice.created_at, format: :long) %></td>
<td><%= l(notice.updated_at, format: :long) %></td>
<td class="actions_links">
<%= link_to t("generic.edit"), edit_administrator_notice_path(notice) %>
<%= link_to t("generic.delete"), administrator_notice_path(notice), :confirm => t("generic.delete_confirmation"), :method => :delete, class: "delete" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<h3><%= t(".lastets_notices")%></h3>
<div class="alert alert-warning"> <%= t(".lastets_notices_empty")%> </div>
<% end %>
<!-- Faqs -->
<% if #faqs.length > 0 %>
<h3><%= t(".lastets_faqs")%></h3>
<table>
<thead>
<tr>
<th>#</th>
<th><%=t("activerecord.attributes.faq.title")%></th>
<th><%=t("activerecord.attributes.faq.slug")%></th>
<th><%=t("generic.user_id")%></th>
<th><%=t("generic.created_at")%></th>
<th><%=t("generic.updated_at")%></th>
<th><%=t("generic.actions")%></th>
</tr>
</thead>
<tbody>
<% #faqs.each do |faq| %>
<tr>
<td><%= faq.id %></td>
<td><%= faq.title %></td>
<td><%= faq.slug %></td>
<td><%= faq.user.username %></td>
<td><%= l(faq.created_at, format: :long) %></td>
<td><%= l(faq.updated_at, format: :long) %></td>
<td class="actions_links">
<%= link_to t("generic.edit"), edit_administrator_faq_path(faq) %>
<%= link_to t("generic.delete"), administrator_faq_path(faq), :confirm => t("generic.delete_confirmation"), :method => :delete, class: "delete" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<h3><%= t(".lastets_faqs")%></h3>
<div class="alert alert-warning"> <%= t(".lastets_faqs_empty")%> </div>
<% end %>
Notice.rb model:
Can you help me to identify the issue? I tried to clear the cache and nothing happened.
The error backtrace points to the notice.rb:7 file, which is the line inside the block:
countries << country_post.country.name
Undefined method 'name' for nil:NilClass means you are trying to call nil.name, so for a given country_post, country_post.country returns nil.
You'll have to check this part of the code to solve the bug. Another thing you can do is use the try method:
countries << country_post.country.try(:name)
this try method will return country.name if country is not nil, ornil` otherwise:
https://github.com/rails/rails/blob/cd2d3664e3b434d15b6c19e652befb386187642f/activesupport/lib/active_support/core_ext/object/try.rb#L93
https://github.com/rails/rails/blob/cd2d3664e3b434d15b6c19e652befb386187642f/activesupport/lib/active_support/core_ext/object/try.rb#L62
In your Notice.rb model use:
countries << country_post.country.try(:name)
It seems that you are trying to use the notice.show_countries method.
The log is telling you that the method name does not exist for nil, which means that your object is nil.
Please show us the content of the "show_countries" method and please check that you have an acutal object given to the line of code responsible for the method name call.
Have you tried using a debugger ?
UPDATE:
Based on the notice code, your problem is that some post, does not have a coutry, then, country_post.country is nil which result to your error.
Either you ensure that you have a default country at the creation, or you do it in two step when trying to retrieve the information
if country_post.country.blank?
countries << 'DEFAULT_COUNTRY_NAME'
else
country << country_post.country.name
The answer by #mrcasals most directly answers the question and will result in that exception no longer being raised.
However, a more "fundamental" problem here is that you are expecting some data to be persisted to the database which is not. Rails' canonical solution to the problem of ensuring the existence of attributes is via ActiveRecord validations. The general overview is a great read, and the presence validation should do the trick for this problem in particular.
I have a displaying table grid in my index page. I have a new button in the same page. When i click the new button, the form should appear in the same index page at the top of the table grid. How can i do that? i am new to ROR. So please help me in detail.
Here is my index page.
<div id="new_survey_link">
Create a
<%= link_to 'New', new_enr_rds_surv_rdsap_xref_path %>
</div>
<table class="gridView">
<tr class="gridViewHeader">
<th>Section</th>
<th>Questions</th>
<th>Answer</th>
<th>Element</th>
<th>Sub Element</th>
<th>Material</th>
<th>Action</th>
</tr>
<% if #enr_rds_surv_rdsap_xrefs.empty? %>
<td class="empty_data" colspan="7">No Energy/Survey Cross references are currently exist.</td>
<% else %>
<% #enr_rds_surv_rdsap_xrefs.each do |survey| %>
<tr class="<%= cycle('gridViewclickableRowDialog', 'gridViewAltclickableRowDialog') %>">
<td><%= survey.Section %></td>
<td><%= survey.enr_rds_question_2009.question_text %></td>
<td><%= survey.Answer_No %></td>
<td><%= survey.element.Element %></td>
<td><%= survey.sub_element.Sub_Element %></td>
<td><%= survey.renew_schedule.Material %></td>
<td>
<%= link_to 'Edit', '#', :remote => true, class: "create-user" %> |
<%= link_to 'Delete', survey, method: :delete,
confirm: "Are you sure?" %>
</td>
</tr>
<% end %>
<% end %>
</table>
You can add the form already in the page and hide it with style="display:none";
Then bind the click event of the "new" link to a javascript that toggles the visibility. like with jQuery you can have onclick="$('#new_survey_link a').toggle();"