I'm trying to build an admin backend for a cms website.
this is the structure of my application
├── app.rb
├── Gemfile
├── models
│ └── models.rb
├── routes
│ └── routes.rb
└── views
├── categories.erb
├── # ... other view files
app.rb
require 'sinatra'
require 'data_mapper'
require 'dm-core'
require 'dm-migrations'
require 'digest'
enable :sessions
DataMapper.setup(:default, 'mysql://username:password#localhost/database')
require './models/models.rb'
require './routes/routes.rb'
DataMapper.finalize
models.rb
class Category
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :posts
end
# other model definitons
I've defined a categories route inside my routes.rb
...
get '/categories' do
#categories = Category.all
erb :categories
end
...
Contents of view (categories.erb) file.
#table headers
<tbody>
<% #categories.each do |c| %>
<tr>
<td>
<%= c.id %>
</td>
<td>
<%= c.name %>
</td>
<td>
<%= с.posts.count %>
</td>
<td>
<%= c.posts(:order => [:updated_at.desc]).first.updated_at.strftime("%d/%m/%Y") %>
</td>
</tr>
<% end %>
</tbody>
When I browse to /categories route, server spits this error
NameError at /categories
undefined local variable or method `с' for #<Sinatra::Application:0x0000000284bc08>
I have not faced such problem before. And really don't know what is going on.
The problem probably is not with application structure (the require sequences inside app.rb) because, before get '/categories' I've defined a post '/login' route which checks user inputs against database records. And it works as I want.
post '/login' do
email = params[:email]
pwd = params[:password]
user = Author.first(:email=>email)
if user.nil?
redirect to ('/register')
elsif !user.nil?
if Digest::MD5.hexdigest(pwd) == user.password
... #and so on
UPDATE
I'm listing other table/models with same methods, and all of them works as i expected, but categories.
other routes
get '/articles' do
#articles = Post.all(:is_blog_post => false)
erb :site_articles
end
##blog articles
get '/blogposts' do
#barticles = Post.all(:is_blog_post => true)
erb :blog_articles
end
#users
get '/admin_users' do
#admins = Author.all(:is_admin=>true)
erb :admin_users
end
#bloggers
get '/bloggers' do
#bloggers = Author.all(:is_admin=>false)
erb :blog_users
end
and corresponding view files
site articles
<tbody>
<% #articles.each do |art| %>
<tr>
<td>
<%= art.title %>
</td>
<td>
<%= art.author.full_name %>
</td>
<td>
<%= art.category.name %>
</td>
<td>
<%= art.updated_at.strftime("%d/%m/%Y %H:%M") %>
</td>
<td>
<%= art.featured? ? "Yes" : "No" %>
</td>
</tr>
<% end %>
</tbody>
blog articles
<tbody>
<% #barticles.each do |art| %>
<tr>
<td>
<%= art.title %>
</td>
<td>
<%= art.author.full_name %>
</td>
<td>
<%= art.category.name %>
</td>
<td>
<%= art.updated_at.strftime("%d/%m/%Y %H:%M") %>
</td>
<td>
<%= art.featured? ? "Yes" : "No" %>
</td>
</tr>
<% end %>
</tbody>
admins
<tbody>
<% #admins.each do |admin| %>
<tr>
<td>
<%= admin.full_name %>
</td>
<td>
<%= admin.email %>
</td>
<td>
<%= !admin.twitter.nil? ? admin.twitter : "N/A" %>
</td>
<td>
<%= !admin.facebook.nil? ? admin.facebook : "N/A" %>
</td>
<td>
<%= !admin.phone.nil? ? admin.phone : "N/A" %>
</td>
<td>
<%= admin.posts.count %>
</td>
</tr>
<% end %>
</tbody>
bloggers
<tbody>
<% #bloggers.each do |blogger| %>
<tr>
<td>
<%= blogger.full_name %>
</td>
<td>
<%= blogger.email %>
</td>
<td>
<%= !blogger.twitter.nil? ? blogger.twitter : "N/A" %>
</td>
<td>
<%= !blogger.facebook.nil? ? blogger.facebook : "N/A" %>
</td>
<td>
<%= !blogger.phone.nil? ? blogger.phone : "N/A" %>
</td>
<td>
<%= blogger.posts.count %>
</td>
</tr>
<% end %>
</tbody>
You’ve somehow got an odd character in your source. That’s not a “normal” latin c in the line <%= с.posts.count %>, it’s U+0441, CYRILLIC SMALL LETTER ES. It just looks like a latin c.
To fix it just delete the character and re-write it with a normal c.
Related
I'm new at programming and I have a question related to making a form in Ruby on Rails. I'm making a database for storing products' units. These units are, upon creation, initially located at the storage. This location is stored in a column Unit model. Then I have a model Store and a model Remission.
class Unit < ActiveRecord::Base
belongs_to :store
belongs_to :remission
attr_accessor :into_remission
end
class Remission < ActiveRecord::Base
belongs_to :store
has_many :units
accepts_nested_attributes_for :units
end
class Store < ActiveRecord::Base
has_many :units
has_many :remissions
end
Store has_many :remissions and Remission has_many :units. The idea is that when I put some units for selling in a store I have to create a remission. This remission is the list of products that I gave the store so we both can have a reference of which products are at the store. So I have a form to create a remission in which you can select the store that would change the store_id(reference) of the Remission. I also want to select from this same form the units that would take part in this new remission changing the remission_id(reference) from each unit. For that I made a attar_accessor :into_remissions in the Unit model so I could change that to true in each unit that would end up in the remission with a checkbox. I having a lot of problems making this work, here is the code of my form:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#remission) do |f| %>
<%= render 'shared/error_messages_remissions' %>
#Here you select the store that would own the remission and the products
<div class="field">
<%= f.label :store_id, "Producto" %> <br/>
<%= f.collection_select :store_id, #stores.order(:name), :id, :name, prompt: "Select Store" %>
</div>
#Here its a dynamic table that display all the products in storage
<table id="products" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Product Name</th>
<th>Remission</th>
</tr>
</thead>
<tbody>
#HERE I WANT TO MAKE A CHECK_BOX THAT CHANGES THE INTO_REMISSION VALUE FROM EACH UNIT. SO IT MARKS THE UNITS THAT WOULD TAKE PART IN THE REMISSION
<%= f.fields_for :units do |ff| %>
<% #units.each do |unit| %>
<% unless unit.sold %>
<tr>
<td><%= unit.product.name %></td>
<td>
<%= ff.label :into_remission, "Nombre de tu Marca:" %>
<%= ff.check_box :into_remission%>
</td>
</tr>
<% end %>
<% end %>
<% end %>
</tbody>
</table>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
</div>
Needless to say he check_box its now showing nor working.
I not sure how to make this work and any advices would be welcome as I'm new with rails and programming. Thanks
Try changing this:
<%= f.fields_for :units do |ff| %>
<% #units.each do |unit| %>
<% unless unit.sold %>
<tr>
<td><%= unit.product.name %></td>
<td>
<%= ff.label :into_remission, "Nombre de tu Marca:" %>
<%= ff.check_box :into_remission%>
</td>
</tr>
<% end %>
<% end %>
<% end %>
into this:
<% #units.each do |unit| %>
<%= f.fields_for :units, unit do |ff| %>
<% unless unit.sold %>
<tr>
<td><%= unit.product.name %></td>
<td>
<%= ff.label :into_remission, "Nombre de tu Marca:" %>
<%= ff.check_box :into_remission%>
</td>
</tr>
<% end %>
<% end %>
<% end %>
I think you should first iterate over the units and pass each unit separately into the fields for. That should solve the showing issue.
I want to fetch data by ajax call using Rails 3.I have a form which is given below.
views/users/new.html.erb
<%= form_for :user ,:url => {:action => "search" } do |f| %>
<div class="input-group bmargindiv1 col-md-12"> <span class="input-group-addon text-left">Receipt No. Scan :</span>
<%= f.text_field :user_no,:class => "form-control",placeholder:"user number" %>
</div>
<% end %>
This form says when user will type user_no and as soon as number will typed the search action will execute.A table should present below of this form to show other data by matching according to that user_no.That table is given below.
<table>
<tr>
<th>User Name</th>
<th>User Email</th>
<th>User Number</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
This table will be hide initially.Once search will complete it will be display with appropriate data.
The below is my controller page.
controller/users_controller.rb
class UsersController < ApplicationController
def new
#user=User.new
end
def search
end
end
All the data(i.e-user no,name and email) already saved in DB.Please help me to complete this task using Rails Ajax and Data should display without reloading the page after form submit.I am using Rails version 3.2.19.
If I understand you correctly then try something like that:
In controller:
def search
#user = User.find_by_user_no(params[:user_no])
end
In view with form:
<%= form_for :user ,:url => {:action => "search" }, remote: true do |f| %>
<div class="input-group bmargindiv1 col-md-12"> <span class="input-group-addon text-left">Receipt No. Scan :</span>
<%= f.text_field :user_no,:class => "form-control",placeholder:"user number" %>
</div>
<% end %>
remote: true means that request will be asynchronous.
In view with table:
<div id="search-output-table">
<%= render partial: "search_output_table", locales: {user: #user} %>
</div>
In app/views/users/_search.js.erb:
$("#search-output-table").html("<%= escape_javascript( render(partial: "search_output_table") ) %>");
In app/views/users/_search_output_table.html.erb:
<table>
<tr>
<th>User Name</th>
<th>User Email</th>
<th>User Number</th>
</tr>
<tr>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td><%= user.number %></td>
</tr>
</table>
I know there are alot of questions on this, but after looking at about a dozen or so none of them appear to be the same problem. I have an "Action" model that I am just simply trying to edit and update. (fyi, this isn't a nested form, I'm not using devise, and this isn't an activeadmin problem...like virtually all other questions about this topic are). I'm getting
error:
unpermitted parameters: utf8, _method, authenticity_token when I do this.
Action params in Actions_controller:
def action_params
params.permit(:id, :call_answered, :is_customer, :category_id, :actiontype_id, :why, :reviewer_id, :reviewed, :spam, :lead)
end
IF I change it to:
params.require(:action).permit(:id, :call_answered, :is_customer, :category_id, :actiontype_id, :why, :reviewer_id, :reviewed, :spam, :lead)
end
than I get the below error (and I can't find any solution for this):
undefined method `permit' for "update":String
Actions Controller:
def edit
#action = Action.find(params[:id])
#actiontypes = Actiontype.all
#categories = Category.all
#lead_categories = #categories.where(lead: true)
#user = current_user
#reviewer = User.find(#action.reviewer_id) if #action.reviewed?
end
def update
#action = Action.find(params[:id])
if #action.update!(action_params)
binding.pry
flash[:success] = "Loop closed!"
redirect_to '/closingloop/actions?reviewed=false'
else
flash[:danger] = "Something Went Wrong! The loop was not closed!"
render :edit
end
end
def action_params
params.permit(:id, :call_answered, :is_customer, :category_id, :actiontype_id, :why, :reviewer_id, :reviewed, :spam, :lead)
end
View:
<%= form_for [:closingloop, #action] do |f| %>
<%= f.hidden_field :reviewer_id, :value => #user.id %>
<%= f.hidden_field :reviewed, :value => true %>
<table width="70%" align="center" class="table table-responsive">
<tr>
<th>Tracking Number</th>
<td><%= #action.company.tracking_phone_number %></td>
</tr>
<tr>
<th>Target Number</th>
<td><%= #action.company.phonenumber %></td>
</tr>
<tr>
<th>Opportunity Name</th>
<td><%= #action.opportunity_name %></td>
</tr>
<tr>
<th>Opportunity Address</th>
<td><%= #action.customer_address %></td>
</tr>
<tr>
<th>Opportunity Phone</th>
<td><%= #action.caller_phone_number %></td>
</tr>
<tr>
<th>Call Recording</th>
<td><%= audio_tag(#action.call_recording_link, controls: true) %></td>
</tr>
<tr>
<th>Duration</th>
<td><%= #action.duration %></td>
</tr>
<tr>
<th>Call Status</th>
<td><%= #action.call_status %></td>
</tr>
<tr>
<th>Date & Time</th>
<td><%= #action.created_at.to_formatted_s(:long) %></td>
</tr>
<% if #action.reviewed? %>
<tr id="row_reviewed_by">
<th>Reviewed By</th>
<td><%= #reviewer.first_name %> <%= #reviewer.last_name %></td>
</tr>
<% end %>
<tr><td colspan="2"> </td></tr>
<tr id="q_call_answered">
<th>Call Answered?</th>
<td>
<div class="radio-toolbar">
<%= f.radio_button :call_answered, true, class: 'call_answered_true' %>
<%= f.label :call_answered, "Yes" %>
<%= f.radio_button :call_answered, false, class: 'call_answered_false' %>
<%= f.label :call_answered, "No" %>
</div>
</td>
</tr>
<tr id="why_not_answered" style="display:none;">
<th>Why wasn't it answered?</th>
<td>
<%= f.select :why, options_for_select([["No Answer", "N"], ["Abandoned", "A"], ["After Business Hours", "H"]], #action.why), { include_blank: true } %>
</td>
</tr>
<tr id="q_opportunity" style="display:none;">
<th>Was it a opportunity?</th>
<td>
<div class="radio-toolbar">
<%= f.radio_button :is_customer, true, class: 'opportunity_true' %>
<%= f.label :is_customer, "Yes" %>
<%= f.radio_button :is_customer, false, class: 'opportunity_false' %>
<%= f.label :is_customer, "No" %>
</div>
</td>
</tr>
<tr id="opportunity_type" style="display:none;">
<th>Opportunity Type</th>
<td>
<%= f.select :actiontype_id, options_from_collection_for_select(#actiontypes, 'id', 'action_type', #action.actiontype_id), { include_blank: true } %>
</td>
</tr>
<tr id="reason_for_call" style="display:none;">
<th>Reason for Call</th>
<td><%= f.select :category_id, options_from_collection_for_select(#categories, 'id', 'reason', #action.category_id), { include_blank: true } %></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save" id="save" class="btn btn-success" /></td>
</tr>
</table>
<% end %>
The main problem here is that if you have model named 'action', your params by default will be named in the same way, which collides with ActionController default params. The simplest solution is, I guess, renaming your model.
I have this index.html.erb file:
<h3>Pažymiai</h3><br />
Nuo: <%= #begining_of_week = Date.commercial(2011, 48, 1) %>
iki: <%= #end_of_week = Date.commercial(2011, 48, -1) %>
<table class="macs">
<tr>
<th>Vardas, pavardė</th>
<th>Dalykas</th>
<% (#begining_of_week..#end_of_week).each do |d| %>
<th class="calendar"><%= d.day %></th>
<% end %>
</tr>
<% #students.each do |student| %>
<tr>
<td class="name" rowspan="<%= #subjects.count %>"><%=link_to(admin_student_path(student)) do %><%= student.name%><br /> <%= student.surname %><% end %></td>
<% #subjects.each do |subject| %>
<td class="subject"><%=subject.name%></td>
<% (#begining_of_week..#end_of_week).each do |d| %>
<% #mac = Mac.where(:student_id => student.id, :subject_id => subject.id, :date => d) %>
<% if #mac.blank? %>
<td class="calendar_mac" onclick="location.href='<%= new_admin_mac_path %>'"><center>
</center>
<%= link_to 'new_mac', new_admin_mac_path, :remote => true %>
</td>
<% else %>
<% #mac.each do |mac| %>
<td class="calendar_mac" onclick="location.href='<%= edit_admin_mac_path(mac) %>'"><center>
<%= mac.mac%>
<% end %>
<% end %>
</center></td>
<% end %>
</tr>
<% end %>
<% end %>
</table>
How to make if cell is empty to write new value, if cell has value make it editable, when click cell i want a popup window where you could edit or add new element to database. My table looks:
You can try the best in place gem and follow a sample screen cast by ryanb this might help you out
I have a string with emails (name, surname, email):
#emails = "Nina Beeu luda#hotmail.com, Vasilina Korute valaj#kos.co.uk, Hikote Ewefs valaj#kos.co.uk,
Egert Erm papa#sasee.ee, Sambuka Ioas valaj#kprivet.com, Vanish Kiki sasa#sas.com, Inoke Xxx saop#hot.ee"
I need to substring from this string: name, surname and email and paste them into table:
<table border=1>
<tr>
<td>
Name
</td>
<td>
Surname
</td>
<td>
Email
</td>
</tr>
</table>
How i can do it?
<table>
<% #emails.split(", ").each do |chunk| %>
<tr>
<% ["Name", "Surname", "Email"].zip(chunk.split(" ")).each do |data| %>
<td><%= data.join(": ")</td>
<% end %>
</tr>
<% end %>
</table>
#emails.split(/,\s+/).each do |details|
name, surname, email = details.split(" ")
# do your html creaty thing here
end
More explicitly, you could do this in erb:
<table border=1>
<% #emails.split(/,\s+/).each do |details| %>
<% name, surname, email = details.split(/\s+/) %>
<tr>
<td><%= name %></td>
<td><%= surname %></td>
<td><%= email %></td>
</tr>
<% end %>
</table>
And a variant in haml:
%table(border=1)
- #emails.split(/,\s+/).each do |details|
%tr
- details.split(/\s+/) do |detail|
%td= detail