Rails - best_in_place not updating view until after reload - ruby-on-rails

I'm pretty new at Ruby on Rails and I'm struggling with best_in_place
I'm using best_in_place to enable editing of data within a HTML table. I'm able to successfully enter data and hit enter for it to save. The new data briefly shows in the table, but it soon reverts back to the previous value. If I reload the page the table will show the new, updated data. Does anyone have any suggestion how to make the updated data show straight away?
This is the code for my controller:
class GamesController < ApplicationController
def index
#games = Game.all
end
def show
#game = Game.find(params[:id])
#render 'index'
end
def new
#game = Game.new
end
def edit
#game = Game.find(params[:id])
end
def create
#game = Game.new(game_params)
if #game.save
redirect_to #game
else
render 'new'
end
end
def update
#game = Game.find(params[:id])
if #game.update(game_params)
redirect_to #game
else
render 'edit'
end
end
def destroy
#game = Game.find(params[:id])
#game.destroy
redirect_to games_path
end
private
def game_params
params.require(:game).permit(:number, :title, :weight, :owner, :borrower, :borrowed)
end
end
And this is the code for the view:
<h1>Listing games</h1>
<%= link_to 'New game', new_game_path, :class => "pure-button pure-button-primary" %>
<p>
<table class="pure-table sortable">
<thead>
<tr>
<th width="110px">Number</th>
<th width="300px" >Title</th>
<th width="110px">Weight</th>
<th width="110px">Owner</th>
<th width="120px">Borrowed</th>
<th width="140px">Borrowed by</th>
<th class="sorttable_nosort">Action</th>
</tr>
</thead>
<tbody>
<% #games.each do |game| %>
<tr>
<td><%= best_in_place game, :number, :inner_class => "input_number" %></td>
<td><%= best_in_place game, :title , :inner_class => "input_title" %></td>
<td><%= best_in_place game, :weight, :inner_class => "input_weight" %></td>
<td><%= best_in_place game, :owner, :inner_class => "input_owner" %></td>
<td><%= best_in_place game, :borrowed, as: :checkbox, collection: {false: "No", true: "Yes"} %></td>
<td><%= best_in_place game, :borrower, :inner_class => "input_borrower" %></td>
<td><%= link_to 'Show', game_path(game) %> |
<%= link_to 'Edit', edit_game_path(game) %> |
<%= link_to 'Destroy', game_path(game),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
</p>
</div>
<%= render 'grid_bottom' =%>
Any help or suggestion is greatly appreciated!

Related

I cannot find the source of a print in Ruby on Rails website project. The array is printed sloppily above the table it is displayed in

It looks like this. The information being printed is from the table creation below it I just cannot find out how to stop it without stopping the table from being printed as well.
Here is relevant code.
My index view for the projects file
<div class="jumbotron jumbotron-fluid bg-primary">
<div class="container">
<h1 class="display-4">Hochschule München Student Projects</h1>
<p class="lead">This website holds all the student led projects along with information about each of them.</p>
</div>
</div>
<div class="jumbotron jumbotron-fluid bg-primary">
<div class="container">
<h1 class="display-4">Hochschule München Student Projects</h1>
<p class="lead">This website holds all the student led projects along with information about each of them.</p>
</div>
</div>
<h1>Listing projects</h1>
<br>
Filter by Professor
<%= collection_select(:project, :project_professor, Project.all.select(:project_professor).uniq, :project_professor, :project_professor, prompt: true) %>
<br>
<table border="1">
<tr>
<th><%= sortable "project_title", "Project Title" %></th>
<th><%= sortable "project_professor", "Professor" %></th>
<th><%= sortable "project_partner", "Partner" %></th>
<th></th>
<th></th>
<th></th>
</tr>
<%= #projects.each do |project| %>
<tr>
<td><%= project.project_title %></td>
<td><%= project.project_professor%></td>
<td><%= project.project_partner%></td>
<td><%= link_to 'Show', project_path(project) %></td>
<td><%= link_to 'Edit', edit_project_path(project) %></td>
<td><%= link_to 'Destroy', project_path(project),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br>
<%= link_to 'New project', new_project_path %>
And here is the controller file.
class ProjectsController < ApplicationController
helper_method :sort_column, :sort_direction
def index
#projects = Project.order(sort_column + " " + sort_direction); 0
end
def new
#project = Project.new
end
def create
#project = Project.new(project_params)
if #project.save
redirect_to #project
else
render 'new'
end
end
def edit
#project = Project.find(params[:id])
end
def update
#project = Project.find(params[:id])
if #project.update(project_params)
redirect_to #project
else
render 'edit'
end
end
def show
#project = Project.find(params[:id])
end
def destroy
#project = Project.find(params[:id])
#project.destroy
redirect_to projects_path
end
private
def project_params
params.require(:project).permit(:project_title, :project_semester, :project_partner, :project_professor, :project_format, :project_department, :project_description, :project_image, :project_link)
end
def sort_column
Project.column_names.include?(params[:sort]) ? params[:sort] : "project_title"
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
end
Thanks for any help! I've been having this problem for days now and cannot find the error.
<%= #projects.each do |project| %>
<tr>
<td><%= project.project_title %></td>
<td><%= project.project_professor%></td>
<td><%= project.project_partner%></td>
<td><%= link_to 'Show', project_path(project) %></td>
<td><%= link_to 'Edit', edit_project_path(project) %></td>
<td><%= link_to 'Destroy', project_path(project),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
In Erb, <%= is used to write ruby code that will produce output and <% is used to write ruby code that will not produce output such as loops or conditionals.
Try writing your loop like this and see if that helps.
<% #projects.each do |project| %>
More info here: https://guides.rubyonrails.org/action_view_overview.html#erb
change
<%= collection_select(:project, :project_professor, roject.all.select(:project_professor).uniq, :project_professor, :project_professor, prompt: true) %>
for
<% collection_select(:project, :project_professor, roject.all.select(:project_professor).uniq, :project_professor, :project_professor, prompt: true) %>
The problem is the character "=" since it tells rails that it should show this variable and shows what the query brings
Change
<%= #projects.each do |project| %>
to
<% #projects.each do |project| %>
Remove = to avoid printing the return value of each method
Hope that helps!

Index view of Rails model printing string with all model data

I just finished Rails' Getting Started guide and everything works perfectly except for this mysterious entry that's being printed in the Index view as shown below. I've been trying to find the cause to no avail, neither have I found no suitable terms to Google this issue.
Index.html.erb
<h1>Index</h1>
<%= link_to 'New Client', new_client_path %>
<table>
<tr>
<th>Name</th>
<th>Issued On</th>
<th>Notes</th>
<th>Finished?</th>
<th>Payments</th>
</tr>
<%= #clients.each do |client| %>
<tr>
<td><%= client.name %></td>
<td><%= client.date %></td>
<td><%= client.note %></td>
<td><%= client.finished %></td>
<td><%= client.payment %></td>
<td><%= link_to 'Show', client_path(client) %></td>
<td><%= link_to 'Edit', edit_client_path(client) %></td>
<td><%= link_to 'Destroy', client_path(client),
method: :delete,
data: { confirm: 'This client will be permanentally deleted, do you want to continue?' } %></td>
</tr>
<% end %>
</table>
Clients Controller
class ClientsController < ApplicationController
def index
#clients = Client.all
end
def show
#client = Client.find(params[:id])
end
def new
#client = Client.new
end
def edit
#client = Client.find(params[:id])
end
def create
#client = Client.new(client_params)
if #client.save
redirect_to #client
else
render 'new'
end
end
def update
#client = Client.find(params[:id])
if #client.update(client_params)
redirect_to #client
else
render 'edit'
end
end
def destroy
#client = Client.find(params[id])
#client.destroy
redirect_to clients_path
end
private
def client_params
params.require(:client).permit(:name, :date, :note, :finished, :payment)
end
end
Remove = from <%= #clients.each do |client| %>. It is outputting result of each which you don't need to print.
In ERB,
<%= %>
executes ruby code as well as outputs the result
For example: <%= client.name %>
<% %>
executes ruby code but doesn't output the result
For example: <% #clients.each do |client| %>....<% end %>

Show a form if no search results found Sunspot Solr Rails 3

I am using Sunspot and Solr to full text search my rails app. I am trying to figure out how to show a form (or a link) when no search results are returned. It currently will just show my blank index page when nothing is found. Would I do this with an if/else statement?
Here is my model (dairy.rb)
class Dairy < ActiveRecord::Base
attr_accessible :title
searchable do
text :title
end
end
and controller (dairies_controller.rb)
class DairiesController < ApplicationController
before_filter :set_dairy, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
#search = Sunspot.search [Dairy, Drink] do
fulltext params[:search]
end
#results = #search.results
end
def show
respond_with(#dairy)
end
def new
#dairy = Dairy.new
respond_with(#dairy)
end
def edit
end
def create
#dairy = Dairy.new(params[:dairy])
#dairy.save
respond_with(#dairy)
end
def update
#dairy.update_attributes(params[:dairy])
respond_with(#dairy)
end
def destroy
#dairy.destroy
respond_with(#dairy)
end
private
def set_dairy
#dairy = Dairy.find(params[:id])
end
end
and index (index.html.erb)
<body class="DI">
<%= form_tag dairies_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search], style:"width:550px; height:30px;", :autofocus => true %><br>
<%= submit_tag "Search!", :name => nil, class: "btn btn-primary btn-lg", style: "margin-top:10px" %>
</p>
<% end %>
<%= link_to 'Add New Item', new_dairy_path, class: "btn btn-default" %>
<p id="notice"><%= notice %></p>
<table>
<thead>
<tr>
<th>Title</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #results.each do |dairy| %>
<tr>
<td><%= dairy.title %></td>
<td><%= link_to 'Show', dairy %></td>
<td><%= link_to 'Edit', edit_dairy_path(dairy) %></td>
<td><%= link_to 'Destroy', dairy, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
</body>
Let me know if you need any other pages. Probably a very simply way to do what I want, but I am still learning. My real goal is to have a contact form when no search results are found. Thank you!!
def index
#search = Sunspot.search [Dairy, Drink] do
fulltext params[:search]
end
if #search.results.any?
#results = #search.results
else
return redirect_to some_path
end
end
or in the view
if #results.any?
render partial: 'list_results'
else
render partial: 'empty_results'
end

In Rails show method is not getting executed

Frnds I have created a controller, named products_controller and product model and also index.html.erb and show.html.erb
but when i call localhost:3000/products it get the details from database. in the same page i have given a link to show the details of a product clearly. But it is not loading. when i clicks on show link i has to get the details of each product. but localhost:3000/products), it loads to the same page.
result for localhost:3000/products
Model Name Brand Name Price Discount Qty Available
Nokia Lumia Nokia 15000 5.0 25 Show
Samsung galaxy Nexus Samsung 45000 15.0 5 Show
Sony Experia Sony Ericsson 4500 1.0 10 Show
Nikon D5100 Nikon 25500 10.0 100 Show
Panasonic Lumix Panasonic 25500 10.0 104 Show
Olympus Granite Olympus 24520 12.0 45 Show
products_controller.rb
class ProductsController < ApplicationController
def index
#products = Product.select("model_name,brand_name, price, discount, qty_avaliable")
respond_to do |format|
format.html # index.html.erb
format.json { render json: #products }
end
end
#render :text => params and return false
def show
#product = Product.find_by_sql("select model_name,brand_name, price, discount, qty_avaliable from products where prod_id='PR1'")
respond_to do |format|
format.html # show.html.erb
format.json { render json: #product }
end
end
end
product.rb
class Product < ActiveRecord::Base
attr_accessible :prod_id, :model_name, :brand_name, :price, :discount, :qty_avaliable
end
index.html.erb
<p id="notice"><%= notice %></p>
<h1>Products List</h1>
<table>
<tr>
<th>Model Name</th>
<th>Brand Name</th>
<th>Price</th>
<th>Discount</th>
<th>Qty Available</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #products.each do |product| %>
<tr>
<td><%= product.model_name %></td>
<td><%= product.brand_name %></td>
<td><%= product.price %></td>
<td><%= product.discount %></td>
<td><%= product.qty_avaliable %></td>
<td><%= link_to 'Show', Product %></td>
</tr>
<% end %>
</table>
show.html.erb
<p prod_id="notice"><%= notice %></p>
<p>
<b>Model Name:</b>
<%= #product.model_name %>
</p>
<p>
<b>Brand Name:</b>
<%= #product.brand_name %>
</p>
<p>
<b>Price:</b>
<%= #product.price %>
</p>
<p>
<b>Discount:</b>
<%= #product.discount %>
</p>
<p>
<b>Quantity:</b>
<%= #product.qty_avaliable %>
</p>
First thing, why don't you have an id for your model, and why are you doing raw SQL?
It should be:
class ProductsController < ApplicationController
def index
#products = Product.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #products }
end
end
def show
#product = Product.where(id: params[:id]).first
respond_to do |format|
format.html # show.html.erb
format.json { render json: #product }
end
end
end
And on your views, the link is wrong, you should pass the id for the current product in the iteration
<% #products.each do |product| %>
<tr>
<td><%= product.model_name %></td>
<td><%= product.brand_name %></td>
<td><%= product.price %></td>
<td><%= product.discount %></td>
<td><%= product.qty_avaliable %></td>
<td><%= link_to('Show', product) %></td>
</tr>
<% end %>
link_to will do some meta programming behind the scenes, but its mostly the same than doing
<td><%= link_to('Show', product_path(id: product.id) %></td>
It seems to me, that you should use <%= link_to 'Show', product %> instead of <%= link_to 'Show', Product %> in index.html.erb
Here:
<td><%= link_to 'Show', Product %></td>
You need to use product, which is the block variable, not Product, which is a class.

Sunspot search doesn't work

I'm trying to make an search option in my site, but it won't show results after an search query.
I have followed this tutorial: http://collectiveidea.com/blog/archives/2011/03/08/full-text-searching-with-solr-and-sunspot/
My routes.rb:
resources :users do
collection do
get :search
end
member do
get :following, :followers
end
end
My index.html.erb
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New User', new_user_path %>
My model user.rb
searchable do
text :name
end
And my user controller
def index
#users = User.paginate(page: params[:page])
end
def search
#users = User.search do
keywords params[:query]
end.results
respond_to do |format|
format.html { render :action => "index" }
end
end
What am i doing wrong?

Resources