the cart controller - im having trouble declaring the delete function
class CartController < ApplicationController
def add
id = params[:id]
cart = session[:cart] ||= {}
cart[id] = (cart[id] || 0) + 1
redirect_to :action => :index
end
def index
#cart = session[:cart] || {}
end
end
the main item page - the link to delete the item is already defined, I'm confused in the controller part
<h1 id="prodhead">Products</h1>
<table class="catalog">
<% for item in #items %>
<tr>
<td>
<div class="image">
<%= link_to (image_tag item.image_url), item %>
</div>
</td>
<td>
<div class="title">
<%= link_to item.title, item %>
</div>
<div class="description">
<%=h item.description %>
</div>
<div class="links">
<% if session[:login] == 1 %>
<%= link_to 'Edit Item', edit_item_path(item) %> |
***<%= link_to 'Delete Item', item, :confirm => 'Are you sure?', :method => :delete %>***
<% else %>
<%= link_to "Add to Cart", :controller => :cart, :action => :add, :id => item %><br />
<% end %>
</div>
<div class="price">
<%= number_to_currency(item.price, :unit => "$") %>
</div>
</td>
</tr>
<% end %>
</table>
<% if session[:login] == 1 %>
<p><%= link_to 'New item', new_item_path %></p>
<% end %>
The Routes.rb - the route for deleting is also defined already
OnlineShop::Application.routes.draw do
get "cart/index"
get "cart/add"
get "cart/checkout"
get "cart/del"
get "site/about"
get "site/contact"
get "user/admin_login"
get "user/logout"
resources :items
Well first of all I've notice you reposted your issue however this time somewhat more descriptive, please for future issues, think before you create a ticket and take the time to edit previous tickets which need refining to keep the community clean.
OT:
I see you've created item and cart resources with basic CRUD functionality however the way you did it is not quite "The rails way" I suggest you start a project bij using the scaffold command to learn how this CRUD and routes should be implemented.
If you are willing to learn more about the topic I advice reading: http://guides.rubyonrails.org/getting_started.html
Also I've noticed you use the "h" syntax to escape your output this is only done before Rails 2.3.8 may I suggest using a newer rails version when starting out?
Related
My application needs to duplicate a Skill (from skills index) as many times the user needs it in his cart. So I decided to trigger the add-to-cart method of the skills_controller when the related form, including the number of duplicates and the Skill's id, is submitted. For this purpose, I added counter to the strong parameters of skills_controller.
Unfortunately, I am missing something to correctly setup the form: when submitted, it triggers the create method. Here is the code:
routes.rb extract
resources :skills, :path => "variables" do
resources :values_lists
member do
post :add_to_cart
get :create_values_list
get :upload_values_list
get :remove_values_list
end
collection do
get :index_all
end
end
skills_controller.rb method
def add_to_cart
#template_skill = Skill.find(params[:id])
iterations = params[:skill][:counter].to_i
until iterations == 0
#skill = #template_skill.deep_clone include: [:translations, :values_lists]
#skill.business_object_id = session[:cart_id]
#skill.template_skill_id = #template_skill.id
#skill.code = "#{#template_skill.code}-#{Time.now.strftime("%Y%m%d:%H%M%S")}-#{iterations}"
#skill.is_template = false
#skill.save
iterations -= 1
end
#business_object = BusinessObject.find(session[:cart_id])
redirect_to #business_object, notice: t('SkillAdded2BO') # 'Skill successfully added to business object'
end
index.html.erb table content
<tbody>
<% #skills.each do |skill| %>
<tr data-href="<%= url_for skill %>">
<% if not session[:cart_id].nil? %>
<td>
<%= form_with model: #skill, :action => "add_to_cart", :method => :post, remote: false do |f| %>
<%= f.text_field :counter, value: "1", class: "mat-input-element", autofocus: true %>
<button type="submit" class="mat-icon-button mat-button-base mat-primary add-button" title="<%= t('AddToUsed') %>">
<span class="fa fa-plus"></span>
</button>
<% end %>
</td>
<% end %>
<td class="no-wrap"><%= skill.code %></td>
<td><%= link_to skill.translated_name, skill %></td>
<td><%= link_to translation_for(skill.parent.name_translations), skill.parent %></td>
<td><%= skill.responsible.name %></td>
<td><%= skill.updated_by %></td>
<td class="text-right"><%= format_date(skill.updated_at) %></td>
</tr>
<% end %>
</tbody>
Thanks a lot for your help!
According to this form helpers guide, the syntax you used doesn't exist
form_with model: #model, action: :custom_action
So in this case, you have to specify the url parameter for form_with to make it works.
<%= form_with model: #skill, url: :add_to_cart_skill_path(#skill), method: :post, remote: false do |f| %>
I have manager remark model that takes input as a remark and decision value and saves it with the project site ID. I have a project site model that takes input as name, date, and file and stores it. Many remarks have a many to one relation with project site ID, and the project site belongs to the manager remark. I want to access the decision attribute boolean value in project site index form, but I am unable to access that boolean value in the index page of the project site. Here is my code of project site and manager remarks model, view and controller-
project site index.html.erb
<table>
<thead>
<tr>
<th>Name</th>
<th>Date</th>
<th>Attendance</th>
<th>Status</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #project_sites.each do |project_site| %>
<tr>
<td><%= project_site.name.titleize %></td>
<td><%= project_site.date %></td>
<td><%= link_to ' View attendance', project_site.file, :class => "fi-page-export-csv" %></td>
<td><%= "here i want to access manager remark decision value" %></td>
<td><%= link_to 'Remark ', project_site %><span>(<%= project_site.manager_remarks.size %>)</span></td>
<td><%= link_to 'Edit', edit_project_site_path(project_site) %></td>
<td><%= link_to 'Destroy', project_site, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
project site controller
def index
#project_sites = ProjectSite.all.order("created_at DESC")
#manager_remark = ManagerRemark.joins(:project_site).where(:project_sites => { :user_id => #user.id })
end
# GET /project_sites/1
# GET /project_sites/1.json
def show
#manager_remark = ManagerRemark.new
#manager_remark.project_site_id = #project_site.id
end
# GET /project_sites/new
def new
#project_site = ProjectSite.new
end
def project_site_params
params.require(:project_site).permit(:name, :date, :file)
end
manager_remark controller
class ManagerRemarksController < ApplicationController
def create
#manager_remark = ManagerRemark.new(remark_params)
#manager_remark.project_site_id = params[:project_site_id]
#manager_remark.save
redirect_to project_site_path(#manager_remark.project_site)
end
def remark_params
params.require(:manager_remark).permit(:remark, :decision)
end
end
manager_remark view form
<%= form_for [ #project_site, #manager_remark ] do |f| %>
<div class="row">
<div class="medium-6 columns">
<%= f.radio_button :decision, true %>
<%= f.label :approve %>
<%= f.radio_button :decision, false %>
<%= f.label :reject %>
</div>
<br>
<br>
<div class="medium-6 cloumns">
<%= f.label :remark %><br/>
<%= f.text_area :remark %>
</div>
</div>
<div>
<%= f.submit 'Submit', :class => 'button primary' %>
</div>
<% end %>
routes.rb
Rails.application.routes.draw do
root to: 'home#index'
devise_for :users
resources :project_sites do
resources :manager_remarks
end
get '/project_manager_level_two' => 'project_manager_level_two#index'
get '/project_managers' => 'project_managers#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
If I understand correctly, you have a ProjectSite that contains a ManagerRemark with a decision, right? If that's the case, the simple answer is:
<%= project_site.ManagerRemark.decision %>
If you are saying that each ProjectSite has many ManagerRemarks, you'll want to place the above inside a loop, like so:
<% project_site.manager_remarks.each do |manager_remark| %>
<%= manager_remark.decision %><br/>
<% end %>
This assumes that your models are correctly configured to recognize these relationships. The above may also be optimized by adding an include clause to your fetch inside the controller and there's no need to fetch the ManagerRemark objects separately. Therefore, you'd probably want something like:
def index
#project_sites = ProjectSite.all.includes( :manager_remark ).order("created_at DESC")
end
I am working on my first web and Rails app and can not figure out how to get a search feature work from my main page to one of the controllers.
How to send the request and redirect to a results page to show the results from the search.
I can't get this to work as am not sure how to route in a way my variable #histories will keep results and display on the show page.
I would appreciate some insight into search from any page and displaying results on a dedicated page.
Here is what i have so far in terms of the controller, model and partials.
Shipments Model:
def self.search(search)
search_condition = search
find_by_sql("SELECT cargo_transit_histories.current_location,cargo_transit_histories.updated_at FROM cargo_transit_histories
INNER JOIN shipments ON shipments.id = cargo_transit_histories.shipment_id WHERE shipments.tracking_number='search_condition'")
end
Tracking Controller:
def search
#histories = Shipment.search(params[:search])
render('show')
end
Show (Found in Tracking view):
<div class="search_result">
<%= render 'track/search_results' %>
</div>
_search (partial):
<%= form_tag :controller => 'tracking', :action => 'search', :method => 'get' do %>
<%= text_field_tag :search, params[:search], :id => 'search_field' %>
<%= submit_tag "Search", :name => nil %>
<%= link_to_function "Clear", "$('search_field').clear()" %>
<% end %>
_search_results (partial):
<div class="Results list">
<table class="Resultslisting" summary="Result list">
<tr class="header">
<th>Current Location</th>
<th>Date/Time</th>
</tr>
<% if !#histories.empty? %>
<% #histories.each do |result| %>
<tr>
<td><%= result.current_location %></td>
<td><%= result.updated_at %></td>
</tr>
<% end %>
</table>
<% else %>
<p> The tracking number does not exist!</p>
<% end %>
</div>
Try something like the following adapted example:
https://gist.github.com/4173716
But you need to dig a bit deeper into Rails 3 to understand why.
I'm new, so forgive my complete ignorance. I'm on Rails 3.2.3 and am doing the Lynda Ruby on Rails Essentials course. I've completed the chapter listed in the title, but am getting an error page after confirming the delete button. Here is a video to show you the error in action.
I've reviewed the following docs to confirm that I've not made typing errors.
Here is my list.html.erb
<div class="subject list">
<h2>Subjects</h2>
<%= link_to("Add New Subject", {:action => 'new'}, :class => 'action new') %>
<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", {:action => 'edit', :id => subject.id}, :class => 'action edit') %>
<%= link_to("Delete", {:action => 'delete', :id => subject.id}, :class => 'action delete') %>
</td>
</tr>
<% end %>
</table>
Here is my delete.html.erb
<%= link_to("<< Back to List", {:action => 'list'}, :class => 'back-link') %>
<div class="subject delete">
<h2>Delete Subject</h2>
<%= form_for(:subject, :url => {:action => 'destroy'}, :id => #subject.id) do |f| %>
<p>Are you sure you want to permanently delete this subject?</p>
<p class="reference-name">Subject name: <%= #subject.name %></p>
<div class ="form-buttons">
<%= submit_tag("Delete Subject") %>
</div>
<% end %>
</div>
Here is my subjects_controller.rb
class SubjectsController < ApplicationController
def index
list
render('list')
end
def list
#subjects = Subject.order("subjects.position ASC")
end
def show
#subject = Subject.find(params[:id])
end
def new
#subject = Subject.new
end
def create
# Instantiate a new object using form parameters
#subject = Subject.new(params[:subject])
# Save the object
if #subject.save
# If save succeeds, redirect to the list action
redirect_to(:action => 'list')
else
# If save fails, redisplay the form so user can fix problems
render('new')
end
end
def edit
#subject = Subject.find(params[:id])
end
def update
# Find object using form parameters
#subject = Subject.find(params[:id])
# Update the object
if #subject.update_attributes(params[:subject])
# If save succeeds, redirect to the list action
redirect_to(:action => 'show', :id => #subject.id)
else
# If save fails, redisplay the form so user can fix problems
render('edit')
end
end
def delete
#subject = Subject.find(params[:id])
end
def destroy
Subject.find(params[:id]).destroy
redirect_to(:action => 'list')
end
end
Here is the resulting error page referenced in the video above.
If I can get some help in understanding why I can't get this properly delete the file, that would be great.
I'm sorry, I have never heard of "Lynda Ruby on Rails Essentials course" but I can see from your posted code that your delete form looks totally messed up, you have a form_for but you are using a submit_tag! That doesn't make sense. The problem is that if you look in your log file you will see that the params passed in to the delete action do not include an id therefore when you use #subject = Subject.find(params[:id]) you will get the error you are seeing.
The solution - change the submit_tag to f.submit OR change the form_for to a form_tag and make sure the correct route is called, that way the id should be passed back through the params hash.
Your log file stack trace is ALWAYS your very best friend, learn to understand it ASAP and you will find your life a lot easier.
UPDATE
For a large number of reasons I think your best option would be to change your form to look like this
<%= link_to("<< Back to List", {:action => 'list'}, :class => 'back-link') %>
<div class="subject delete">
<h2>Delete Subject</h2>
<p>Are you sure you want to permanently delete this subject?</p>
<p class="reference-name">Subject name: <%= #subject.name %></p>
<div class ="form-buttons">
<%= button_to "Delete", subject_path(#subject), method: :delete %>
</div>
</div>
The reason the first way did not work is because you missed the leading opening tag "<" which is why the text got displayed instead of the button
you don't need the form, you are entering no data so just use the button_to and tell it to go to the delete action.
I haven't tested the code but it should be fine
I'm creating a simple project tagging application using the Act_As_Taggable_On gem.
Adding projects, and adding tags (in my case 'types' / 'type_list') to each project works great. Where I'm stuck is how to remove individual tags using Act_As_Taggable_On. What I would like is to be able to click the 'x' text link next to each tag (see link) to remove the tag from that projects type_list.
I've searched the documentation and found a method along the lines of:
project.type_list.remove("your tag")
But what I need help with is how to call the remove method on the specific tag, especially since the whole thing is being iterated with .each do
My controller and model code is pretty minimal and standard - based on Act_As_Taggable_On docs. Here is my view code for generating the layout above:
<h1><%= #title %></h1>
<div class="column-left">
<% #projects.each do |project| %>
<div class="p_wrapper">
<table>
<tr>
<td><div class="project p_name"><%= project.name %></div></td>
<td><div class="p_link"><%= link_to 'Edit', edit_project_path(project) %></div></td>
<td><div class="p_link"><%= link_to 'Nuke', project, :confirm => 'Are you sure?', :method => :delete %></div></td>
</tr>
</table>
<table>
<tr>
<td>
<% project.type_list.each do |tag|%>
<div class="p_tag">
<%= tag %>
<%= link_to "x", # %> <!-- THIS IS THE PART I'M STUCK ON -->
</div>
<% end %>
</td>
</tr>
</table>
<table>
<tr>
<td>
<%= form_for(project) do |f| %>
<%= f.text_field :inject_tags %>
<%= f.submit "Add Tag" %>
<% end %>
</td>
</tr>
</table>
</div>
<% end %>
<br />
<%= link_to 'Add new project', new_project_path %>
</div>
Can anyone point me in the right direction? Am I implementing this correctly to be able to actually remove tags as described?
Thanks guys!
I would simply add a new method to your projects controller, like so:
def remove_tag
Project.find(params[:id]).type_list.remove(params[:tag])
end
And in your routes file
resources :projects do
member do
put 'remove_tag', :as => :remove_tag
end
end
And in your view
<%= link_to 'x', remove_tag_project_path(project), :tag => tag, :method => :put %>
Of course you should add some sanitation, but it should work this way...
Based on #Vapire's suggested code - finally worked out a working solution. Just some minor edits to the view, route, and controller. Let me know if you see anything dodgy in here - still trying to get a good grasp of Ruby/Rails so all suggestions/refactoring ideas welcome.
The updated test site is at project-list.heroku.com.
Updated projects controller to find current project, remove :tag passed from index view through route:
def remove_tag
#project = Project.find(params[:id])
#project.type_list.remove(params[:tag])
#project.save
redirect_to projects_path, :flash => { :success => "Updated - tag nuked."}
end
Updated route:
resources :projects
match 'projects/:id/remove_tag/:tag' => 'projects#remove_tag'
Updated the link_to 'x' code to pass :tag params through the updated route above:
<% project.type_list.each do |tag|%>
<div class="p_tag">
<%= tag %>
<%= link_to 'x', {:action => "remove_tag", :id => project.id, :tag => tag,
:controller => "projects"} %>
</div>
<% end %>
This is obviously new ground for me so would appreciate if you have a different / better way of handling this issue please let me know! Also, thanks for your help #Vapire!