I have views/search/search_textbook.html.erb that shows the results of search. When a user click textbook.title by <%=link_to "#{textbook.title}", textbook_path(textbook.id) %>, The user is invoking the show method from textbooks_controller.
However, inside views/textbooks/show.html.erb has <%= link_to 'Back to list', textbooks_path %> that only take user to the index page of textbook (views/textbooks/index.html.erb).
below is my page that shows the search results views/search/search_textbook.html.erb:
<div class="container">
<div class="row">
<div class="col-sm-12">
<table class = "table table-striped">
<h1>Textbook Search Results</h1>
<thead>
<tr>
<th>Textbook Title</th>
<th>Subject</th>
<th>Price</th>
<th>Accept Offer</th>
<th>Created on</th>
</tr>
</thead>
<tbody>
<% #textbooks.each do |textbook| %>
<tr>
<!--<td><%= textbook.title %></td>-->
<td><%=link_to "#{textbook.title}", textbook_path(textbook.id) %></td>
<td><%= textbook.subject %></td>
<td>$<%= textbook.price %></td>
<td>
<% if textbook.offer == true %>
<!--<%= "\u2713"%>-->
<p class="offer_yes">✓</p>
<% else %>
<!--<%= "\u2715" %>-->
<p class="offer_no">✕</p>
<%end%>
</td><!--somehow below need Date.parse()-->
<td><%= textbook.created_at.strftime("%d %b. %Y") %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<div>
<%= form_tag search_textbook_path, :method => :get do %>
<p>
<a style="color:#000000" title="Suggest!" data-toggle="popover" data-trigger="hover" data-content="Title or Subject">
<%= text_field_tag :search, params[:search], placeholder: "Search textbooks" %>
</a>
<%= submit_tag "Search", :name => nil, class: "btn btn-success btn-sm" %>
<% end %>
</div>
<%= link_to 'Sell Textbook', new_textbook_path, class: "btn btn-primary" %>
<%= link_to 'Back to list', textbooks_path %>
<script>
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
</script>
</div>
</div>
</div>
below is my views/textbooks/show.html.erb file:
<div class="container">
<div class="row">
<!--<div class="col-xs-offset-4 col-xs-4 col-xs-offset-4">-->
<div class="col-sm-offset-4 col-sm-4 col-sm-offset-4">
<!--<p id="notice"><%= notice %></p>-->
<p>
<h4><strong>Title:</strong>
<%= #textbook.title %></h4>
</p>
<p>
<strong>Subject:</strong>
<%= #textbook.subject %>
</p>
<p>
<strong>Price:</strong>
$<%= #textbook.price %>
</p>
<p>
<strong>Accept Offer:</strong>
<%if #textbook.offer == true%>
<%='Yes'%>
<%else%>
<%='No'%>
<%end%>
</p>
<p>
<strong>Description:</strong>
<pre><%= #textbook.description %></pre>
</p>
<p>
<% if !(#textbook.thumbnail_content_type =~ /^image/).nil? %>
<strong>Image:</strong>
<pre><%= image_tag #textbook.thumbnail.url(:medium) %></pre>
<%else%>
<!--<strong>No Image.</strong>-->
<%end%>
</p>
<p>
<strong>Created on:</strong>
<%= #textbook.created_at.strftime("%d %b. %Y") %>
</p>
<p>
<!--<%= link_to 'Contact', new_contact_path %>-->
<%= link_to 'Contact', new_contact_textbook_path(#textbook), class: 'btn btn-info' %>
</p>
<%if #textbook.user_email == current_user.email %>
<%= link_to 'Edit', edit_textbook_path(#textbook) %> |
<%= link_to 'Back to list', textbooks_path %>
<%else %>
<%= link_to 'Back to list', textbooks_path %>
<%end%>
</div>
</div>
</div>
How can I add a clickalbe button back to search results IF ONLY IF the user is came from search results??
Thanks!
This is how I would have done it.
1) Find the current path and assign it to a flash:
flashes are not only used for messages, they are actually the simplest way to send temporary data to an other action.
So in the search action of the textbook controller:
flash[:last_search_path] = [the current path]
note: [the current path] isn't ruby code, we'll see later how to find it
2) Use the link in your textbook show view:
In textbook show view all you have to do is:
<% if flash[:last_search_path] %>
<%= link_to "back to search results", flash[:last_search_path] %>
<% end %>
But... how do you find [the current path] ?
Obviously [the current path] isn't ruby code, so you'll have to replace it with something else.
there's multiple ways to find the current path or url: look here and here
Flashes ?
Flashes can be very useful for passing temporary data, I highly suggest you to read more about them. Learn more about flashes here
Other solutions to pass data between actions
You can use params to pass that link parameters but it would add more complexity. read more about params here
You can also use cookies but that would save it to the users browser and wouldn't work only if he came from the search result: read more about sessions here
Above flash is good way. But I use below way.
<p>
<%if URI(request.referer).path == "/search_textbook" %>
<%= link_to 'Back to Search Result', request.referer, class: "btn btn-warning" %>
<%end%>
</p>
Related
I was looking at the question below, which has a good answer for what I will ask, but I have a doubt. I cannot comment yet so that is why I am making another post.
How to submit multiple, duplicate forms from same page in Rails - preferably with one button
This is my view for the form
new.html.erb
<h1 class="page-title">Nuevo Precio</h1>
<hr class="title-division">
<div class="alta-form-container">
<%= form_tag "/precios" do %>
<% 2.times do %>
<%= render 'form', precio: #precio %>
<% end %>
<%= submit_tag "Submit", class: "btn btn-success" %>
<% end %>
<%= link_to 'Lista Precios Cliente', client_precios_path %> <br><%= link_to 'Ver', [#client, #precio] %> <br>
</div>
and my _form.html.erb
<div class="field">
<%= label_tag :precio, "Precio" %>
<%= text_field_tag "precios[][precio]" %>
</div>
<div class="field">
<%= text_field_tag "precios[][cant_acomodato]" %>
</div>
<div class="field">
<%= hidden_field_tag "precios[][client_id]" %>
</div>
When I submit the form I get an error that says
'No route matches [POST] "/precios"'
which I'm guessing is because on my new.html.erb I wrote form_tag "/precios" do
Any thoughts on what I should change or edit? thanks in advance
actually you have only one form here, because _form.html.erb will generate only inputs, that according to form below
<%= form_tag "/precios" do %>
that will generate
form action="/precios" method="post">
if you want leave it like that, you should add to your config/routes.rb
post '/precios', to: 'controller_name#method_name', as: :controller_name_method_name
your situation:
post '/precios', to: 'PreciosController#create', as: :precios_create
also check this
documentation
so on my website, there is an unwanted text node that contains the record of my databse. There is no tag or element whatsoever on the html code, but somehow it exist.
I reckon it is caused by this line of code:
<%= #workspace.items.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.owner %></td>
<td><%= item.quantity %></td>
<td><%= item.details %></td>
<td><%= link_to 'Edit Item', edit_workspace_item_path(#workspace,item) %></td>
<td><%= link_to 'Delete', [item.workspace, item],
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
The view.html.erb:
<p>
<strong>Name:</strong>
<%= #workspace.name %>
</p>
<p>
<strong>maxitem:</strong>
<%= #workspace.max %>
</p>
<p>
<strong>details:</strong>
<%= #workspace.details %>
</p>
<p>
<%= link_to 'Edit Phase', edit_workspace_path(#workspace) %>
</p>
<p><strong>Items</strong>
</p>
<p>
<%= link_to 'Add Item', new_workspace_item_path(#workspace) %></br>
</p>
<table>
<tr>
<tr>
<th><strong>Name</strong>
<th><strong>Owner</strong>
<th><strong>Quantity</strong>
<th><strong>Details</strong>
</tr>
<%= render 'items/item' %>
</table>
<%= link_to 'Back to List of Phases', workspaces_path %>
I don't know why this suddenly show up on my website
like this
<%= #workspace.items.each do |item| %>
is the issue as <%= %> in ERB stands for "print this". Should be <% %> which will execute the Ruby code without rendering it.
I'm trying to make a product cart.
I want to be able choose the quantity of product when i add the product to cart.
This is LineItemsController
class LineItemsController < ApplicationController
include CurrentCart
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
before_action :set_cart, only: [:create]
def index
#line_items = LineItem.all
end
def show
end
def new
#line_item = LineItem.new
end
def edit
end
def create
product = Product.find(params[:product_id])
#line_item = #cart.add_product(product)
#line_item.quantity = params['q'].to_i
if #line_item.save
redirect_to #line_item.cart, notice: 'Item added to cart.'
else
render :new
end
end
def update
if #line_item.update(line_item_params)
redirect_to #line_item, notice: 'Line item was successfully updated.'
else
render :edit
end
end
def destroy
#cart = Cart.find(session[:cart_id])
#line_item.destroy
redirect_to cart_path(#cart), notice: 'Item successfully removed.'
end
private
def set_line_item
#line_item = LineItem.find(params[:id])
end
def line_item_params
params.require(:line_item).permit(:product_id, :quantity)
end
end
This is view/line_item/_form.html.erb
<%= simple_form_for(#line_item) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.association :product %>
<%= f.association :cart %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
This is view/product/show.html.erb
<% content_for :body_class, 'bg-light' %>
<section class="section instrument-show">
<div class="columns">
<div class="column is-8">
<%= render 'shared/breadcrumb', category: #product.category %>
<h1 class="title is-2"><%= #product.title %></h1>
<!--ul class="pv1">
<%# if #product.brand? %>
<li class="inline-block pr3"><%#= #product.brand %></li-->
<%# end %>
<!--li class="inline-block pr3"><%#= #product.model %></li-->
<%# if #product.condition? %>
<!--li class="inline-block pr3"><%#= #product.condition %></li>
<%# end %>
</ul-->
<div class="feature-image">
<%= image_tag(#product.image_url(:default).to_s) %>
</div>
<div class="content pa4 mt3 bg-white border-radius-3">
<h3 class="subtitle is-4">Description</h3>
<%= #product.description %>
<h3 class="subtitle is-4 pt5">Product Specs</h3>
<table class="table is-narrow">
<% if #product.category.present? %>
<tr>
<td class="has-text-weight-bold">Category:</td>
<td><%= link_to #product.category.name, #product.category %></td>
</tr>
<% end %>
<%# if #product.finish %>
<tr>
<td class="has-text-weight-bold">Finish:</td>
<td><%#= #product.finish %></td>
</tr>
<%# end %>
<tr>
<td class="has-text-weight-bold">Model:</td>
<td><%#= #product.model %></td>
</tr>
</table>
</div>
</div>
<div class="column is-3 is-offset-1">
<div class="bg-white pa4 border-radius-3">
<h4 class="title is-5 has-text-centered"><%= number_to_currency(#product.price) %></h4>
<p class="has-text-centered mb4">Sold by <%#= #product.user.name %></p>
<form>
<input type="number" name="q" min="1" max="50" step="1" class="input label">
</form>
<%= button_to 'Add to cart', line_items_path(product_id: #product), class: 'button is-warning add-to-cart' %>
</div>
</div>
</div>
<% if user_signed_in? && current_user.admin? %>
<%= link_to 'Edit', edit_admin_product_path(#product), class: 'button' %>
<% end %>
</section>
Now in this string #line_item.quantity = params['q'].to_i from
<form>
<input type="number" name="q" min="1" max="50" step="1" class="input label">
</form>
i'm getting "nil" and i desperately don't now what should i do to get actual number.
P.S. This is view/line_items/new.html.erb
<h1>New Line Item</h1>
<%= render 'form', line_item: #line_item %>
<%= link_to 'Back', line_items_path %>
new.html.erb is to enter your data and if you submit, the data will flow to create method inside your controller, meanwhile show is just to show the data, and you cannot input form inside show,
my suggestion try to put q part of new.html.erb, and put it inside simple_form_for tag
since it is number, you can use number_field rails tag helper as sample below, with name :q and range between 1 .. 50 with step 1, below is the sample probably can help your problem
when you submit from new.html.erb, you will receive params[:q] as part of parameters and save it with create method
<%= simple_form_for(#line_item) do |f| %>
<%= f.error_notification %>
<%= number_field(:q, in: 1.0..50.0, step: 1) %>
<div class="form-inputs">
<%= f.association :product %>
<%= f.association :cart %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
I've been following a tutorial Follower Tutorial and I keep getting the error undefined method `find_by_username' for the line.
<%= render '/components/follow_button', :user => User.find_by_username(params[:id]) %>
show.html.erb
<%= render '/components/follow_button', :user => User.find_by_username(params[:id]) %>
<div class="panel panel-default">
<div class="panel-body">
<h5 style="color: grey; font-size: 125%;">Who to follow</h5>
<% for #u in #toFollow do %>
<p style="font-weight: bold; opacity: 0.85;" ><%= #u.username %></p>
<% end %>
</div>
</div>
user.rb
def unfollow(other)
active_relationships.find_by(followed_id: other.id).destroy
_follow_button.html.erb
<% if current_user.id != user.id %>
<div class="panel panel-default">
<div class="panel-body">
<center>
<% if !current_user.following?(user) %>
<%= form_for(current_user.active_relationships.build) do |f| %>
<div><%= hidden_field_tag :followed_id, user.id %></div>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>
<% else %>
<%= form_for(current_user.active_relationships.find_by(followed_id: user.id),
html: { method: :delete }) do |f| %>
<%= f.submit "Unfollow", class: "btn" %>
<% end %>
<% end %>
</center>
</div>
</div>
I've been messing around for ages now and can't work it out. Any help would be appreciated. Feel free to ask for more code etc.
In newer version of rails you should use following code:
Inside controller:
#user = User.find_by(username: username)
In Rails 4 onwards the find_by_... methods are deprecated therefore your User.find_by_username will not work for Rails 4 and higher. You should be able to use either:
User.find(id)
or
User.where(:username=> username)
I have a form called cattle finances, in this form, i use collection select to input the batch number, i have other form columns shown in the _form.html.erb code below
<%= simple_form_for #cattle_finance, remote: true do |f| %>
<% if #cattle_finance.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#cattle_finance.errors.count, "error") %> prohibited this cattle_finance from being saved:</h2>
<ul>
<% #cattle_finance.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<br>
<%= f.collection_select :batch_number, CattleList.all, :batch_number, :batch_number, :prompt => "please select Batch Number" %></br>
<%= f.input :total_litres_of_batch, as: :integer %>
<%= f.input :total_income, as: :integer %>
<%= f.input :gross_income, as: :integer %>
<%= f.input :total_expenses, as: :integer %>
<%= f.input :net_income, as: :integer %>
<%= f.input :date, as: :date %>
<%= f.button :submit %>
<% end %>
My index.html.erb code is
<div class="row">
<div class="col-md-5 col-md-offset-1">
<h2>Cattle Finances</h2>
</div>
<div class"btn-group menu2" align = right>
<%= link_to raw("<span class=''></span> Creditors"), cattle_creditors_path, :class=>"btn btn-default" %>
<%= link_to raw("<span class=''></span> Debtors"), cattle_debtors_path, :class=>"btn btn-default" %>
<%= link_to raw("<span class=''></span> Cattle"), cattles_path, :class=>"btn btn-default" %>
<%= link_to "Home", root_path, :class=>"btn btn-default" unless current_page?(root_url) %>
</div>
<div class="col-md-2 col-md-offset-4">
<%= link_to new_cattle_finance_path, remote: true do %>
<button class="btn btn-default">New</button>
<% end %>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-2" id="cattle_finance-form" style="display:none;"></div>
</div>
<div class="row">
<div class="" id="cattle_finances"><%= render #cattle_finances %></div>
</div>
So what is need help with is the way in which i can select a particular batch_number and automatically all other fields are populated, or picked from the database from the corresponding database tables and filled in respectively. Any help his highly appreciated coz am stuck, although
i know what i want to do, I have no idea of how i can start.