Change price by another value RoR - ruby-on-rails

I'm beginner in RoR. Just creating smth like internet shop, I need make convert price at another currencies. For that i have service class
#product_decorator = Product.includes(:category).map{|product| ProductDecorator.new(product)}
Also i has table in db with actual courses
create_table :product_wrappers do |t|
t.decimal :euro
t.decimal :rubles
end
In view it works good if i manually change product's price method at euro/rubles, but i need do it at link_to methods with new params like eur/rub, I'm rly stuck at that i need create method in store_controller
Routes
put '/set_currency', to: 'store#set_currency'
View
<%= link_to "USD", set_currency_path(:currency => :usd), class: "btn btn-outline-secondary" %>
<%= link_to "RUB", set_currency_path(:currency => :rub) ,class: "btn btn-outline-secondary" %>
<%= link_to "EUR", set_currency_path(:currency => :eur), class: "btn btn-outline-secondary" %>

Just as ideas
To build URL with currency as query param use such helper
<%= link_to "My product", product_path(product, currency: :eur) %>
It will generate such HTML
My product
In your controller, probably ApplicationController
before_action :set_current_currency
def set_current_currency
#current_currency = params[:currency] || 'eur'
end
And to show price in selected currency in views something like this
product.public_send(#current_currency)
Also may be it's a good idea to save selected currency in session or use user profile
def set_current_currency
currency =
if params[:currency]
params[:currency]
elsif session[:currency]
session[:currency]
elsif current_user
current_user.preferred_currency
end
session[:currency] =
if currencies.include?(currency)
currency
else
default_currency
end
end
And than use session[:currency] in your views instead of #current_currency

Related

Ruby on Rails : link_to 'update' and 'delete' doesn't work for me

im newbie in RoR. I'm trying to build my own app. I keep getting error, even not error but nothing happens while I'm trying to delete or edit(update) a row. I created an Apprentice model with the values like; name, surname, grade. The application adds the values entered by me in form to the database. I display the entered data on the main page (root) in the table. I have a problem with deleting and editing these values. In the table I have placed two buttons - Delete and Edit and they display next to each row.
> <td>
> <!--buttons-->
> <%= link_to "Update", home_update_path(student.id) %>
> OR
> <%= link_to 'Delete', root_path(student.id), :confirm => 'Are you sure?', :method => :delete %>
> </td>
In the case of editing, I am redirected to the subpage, but the values that I want to display in the form do not appear. Only the id chosen by me displays in the address bar. For delete, nothing happens when pressed. Would someone be able to help me?
My controller:
class HomeController < ApplicationController
def index
#all_students = Apprentice.all
end
def insert
#name = params[:name]
#surname = params[:surname]
#grade = params[:grade]
#insert = Apprentice.new
if #name.present? && #surname.present? && #grade.present?
Apprentice.create(name: #name, surname: #surname, grade: #grade)
end
end
def update #the same here with edit
#update = Apprentice.find { params[:id] }
end
def delete
#delete = Apprentice.find { params[:id] }
end
Ah and there is also my update view where i want to display selected values
<%= form_with model: #update, local: true do |f| %>
<%= f.text_field :name %>
<%= f.text_field :surname %>
<%= f.text_field :grade %>
<%= f.submit %>
<% end %>
routes:
> Rails.application.routes.draw do root 'home#index' get
> 'home/insert' get 'home/update' get 'home/update/:id' =>
> 'home#update' get 'home/:id/delete', to: 'home#delete' delete
> '/home/:id' => 'home#delete' end
There is just too much wrong here to address usefully, I would recommend starting with the excellent scaffold generators built into Rails, so you can see an example of working code for the standard operations. So from your command line:
rails generate scaffold student name:string
Then take a look at the controller, views, etc. that were created for you.

Search form Couldn't find User with 'id'=

I need to create search form to search for all the cases pt_name of the user
I got this error
Couldn't find User with 'id'=
In cases controller
def index
#user =User.find(params[:id])
#cases=#user.cases
if params[:search]
#search_term = params[:search]
#cases= #user.cases.casesearch_by(#search_term)
end
end
in case model
class Case < ActiveRecord::Base
belongs_to :user
def self.casesearch_by(search_term)
where("LOWER(pt_name) LIKE :search_term OR LOWER(shade) LIKE :search_term",
search_term: "%#{search_term.downcase}%")
end
end
in cases index.html.erb
<%= form_for "",url: cases_path(#user.id), role: "search", method: :get ,class: "navbar-form navbar-right" do %>
<%= text_field_tag :search, #search_term,placeholder: "Search..." %>
<% end %>
The problem is the first line in your controller.
When the form is submitted it's going to cases_path(#user.id) - that's what you specified in your form.
If you're checking with rails routes you'll see that cases_path is actually going to "/cases" (I am assuming you did not overwrite it) and that there isn't any placeholder for an id (like it would be for the show action for example which goes to "/cases/:id".
Now you still specify #user.id in cases_path(#user.id) and then you try to find a user with the id from the params. But if you check your params once you arrived in the controller (with binding.pry or other tools), you will see there is no key :id in the params. You can also check the url it is going to, I believe it will look something like this: "/cases.1".
You can solve that by changing the path to
cases_path(user_id: #user.id)
This way you add a new key value pair to the params hash and then in your controller you need to change it accordingly:
#user =User.find(params[:user_id])
You can also add a hidden field into your form in order to pass along the user id:
<%= form_for "", url: cases_path, role: "search", method: :get, class: "navbar-form navbar-right" do %>
<%= text_field_tag :search, #search_term,placeholder: "Search..." %>
<%= hidden_field_tag :user_id, #user.id %>
<% end %>
And then retrieve it in the controller.
To check your params that you get in the controller action use a gem like pry byebug or just the keyword raise and then inspect the params variable.

RoR: Trying to search by ID number or License Number

hi so I'm trying to do 2 things, one of them is to basically redirect to a model's ID number so input is "1" and redirects to
localhost:3000/model/1
and the second part is actually doing a search. each model has a text field string for license_no and I want to be able to search and return results
currently, I am not sure how I would combine these into 1 search form if thats possible or if it would require 2 separate search forms
i have a search form with only the license_no field but it always returns no results found...
apologize that the model name isn't in singular, the guide I was using to learn RoR had it that way and everything worked, but I have so many references to renters in my project that it would be a while to find all of them
index.html.erb
<%= form_tag search_renters_path, method: get do |f| %>
<%= text_field_tag :license, nil, placeholder: "License Number" %>
<%= submit_tag 'Search', class: "btn-large" %>
<% end %>
models/renters.rb
class Renters < ActiveRecord::Base
validates_presence_of :license_no
def self.search(params)
renters = Renters.where("license_no LIKE?", "%#{params[:license]}%")
end
end
controller.rb
def search
#renter = Renters.search([params])
end
search.html.erb - snippet
<% if #renter.blank? %>
no results
<% else %>
#show results
<% end %>
editted code
models/renters.rb
def self.search(params)
license_query = "%#{params[:license]}%"
id_query = "%#{params[:id]}%"
renters = Renters.where("license_no LIKE ?", license_query) if params[:license].present?
renters = Renters.where("id LIKE ?", id_query) if params[:id].present?
end
controller
def search
#renter = Renters.search(params)
end
search form
<%= form_tag search_renters_path, method: :get do |f| %>
<%= text_field_tag :license, nil, placeholder: "Driver's License" %>
<%= text_field_tag :id, nil, placeholder: "ID number" %>
<% end %>
I'm trying to use the if present? statements to allow a user to decide whether to input ID No or License No. you don't need to input both just one. currently, if I search for a license no, it returns no results. but when I search for an ID, it returns the relevant result
you can do something like this if you are getting value on params[:licence] from your form submit on your controller action search
controller.rb
def search
#renter = Renters.search(params[:licence])
end
app/models/renters.rb
class Renters < ActiveRecord::Base
def self.search(query)
like_query = "%#{query}%"
renters = Renters.where("id LIKE ? OR license_no LIKE ?", like_query, like_query)
end
end

Select_tag login

I would like to have a drop down menu with a list of all the user names in the db. From there, I would like the user to choose his/her name and be able to click login and be taken to their respective page. At this point, a password is not needed. Currently, I have the following:
controller:
def login
#user = User.new
#users = User.all
# #user = User.find_by_id(:id)
# redirect_to user_path(#user)
end
view:
<%= form_for #user, url: '/login', html: {method: 'get'} do |f| %>
<%= f.label "Name" %>
<br/>
<%= select_tag :user, options_for_select(#users) do |users| %>
<%= link_to users.name, users %>
<% end %>
<br/>
<br/>
<%= f.submit 'Login' %>
<% end %>
I cannot seem to link the user to their path and also, i want to show the users name in the drop down menu. Currently, it shows a hexidecimal pointer.
Thank you in advance.
You shouldn't be making a new User object here: you just want to load one out of the database. What you want to do in the controller is just to set current_user to be one of the existing users, right?
Also you've got the form submitting back to the action which loads the form in, which seems weird. I would make it submit to a new action, like "set_current_user" which is a POST action.
in your login template:
<%= form_tag '/set_current_user' do %>
<%= f.label "Name" %>
<br/>
<%= select_tag "user_id", options_for_select(#users.collect{|user| [user.name, user.id] } %>
<br/>
<br/>
<%= submit_tag 'Login' %>
<% end %>
in the controller (you'll need to amend routes.rb to make the '/set_current_user' go to this action) you then need to set something which will keep the user logged in. The traditional way to do this is via session[:user_id], and to have a method current_user which uses this.
def set_current_user
session[:user_id] = params[:user_id]
redirect_to "/" and return
end
Your initial approach is reminiscent of how this sort of thing is normally handled, wherein you do have a form_for, but it's for a UserSession object rather than a User object.

Pre-populating a form field from a link

I'm trying to pre-populate a string field in a form when a link is clicked. I've tried:
$<%= link_to "New product", new_product_path(:product_name => "foo") %>
and
$<%= link_to "New product", new_product_path(:name => "foo") %>
Both didn't work. Anyone has any idea?
Try this,
<%= f.text_field :name,:value=>(#product.new_record? ? params[:name] : #product.name )%>
or in new action
def new
#product = Product.new(:name=>params[:name])
end
<%= f.text_field :name %>
I think this is best done from the controller. You can control what you send via the link_to as before.
class ProductsController < ApplicationController
def new
#product = Product.new new_product_params
end
def new_product_params
params.permit :name, :another_field
end
end
Is the field you're trying to populate on the current page, or the target page?
If it is on the current page you'll need some javascript to accomplish that, if its on the
target page you should probably set the default in your action based on passed parameters.

Resources