In my app when I am rendering Post.all in my static page home I am getting some code like this at the end of the page
[#<Post id: 3, title: "I loved you...", content: "how to short and place enter if the line is large ...", created_at: "2015-04-03 09:17:48", updated_at: "2015-04-03 10:34:47", image_file_name: "Penguins.jpg", image_content_type: "image/jpeg", image_file_size: 777835, image_updated_at: "2015-04-03 09:18:50">, #<Post id: 2, title: "I loved you...", content: "I loved you, and I probably still do,\r\nAnd for a w...", created_at: "2015-04-02 15:44:19", updated_at: "2015-04-02 15:44:19", image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil>, #<Post id: 1, title: "উত্তম ও অধম", content: "কুকুর আসিয়া এমন কামড়\r\nদিল পথিকের পায়\r\nকামড়ের চ...", created_at: "2015-04-02 15:41:51", updated_at: "2015-04-02 15:41:51", image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil>]
how to remove that or stop getting that?
here is my gemfile
source 'https://rubygems.org'
gem 'rails', '4.2.1'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'therubyracer', platforms: :ruby
gem 'jquery-rails'
gem 'paperclip', '~> 4.2.1'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
group :development, :test do
gem 'sqlite3'
gem 'byebug'
gem 'web-console', '~> 2.0'
end
group :production do
gem 'pg'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
In Pages controller home action I have put #post = Post.all
and in view I have put
<%= #post.each do |post|%>
<% if post.image.present?%>
<h1> <%= link_to post.title, post %></h1><br>
<%= image_tag post.image.url(:thumb)%>
<%= simple_format post.content %>
<hr color="blue" >
<% else %>
<h1> <%= link_to post.title, post %></h1><br>
<%= simple_format post.content %>
<hr color="blue">
<% end %>
<%end%>
you should remove the = in the first loop <%= #post.each do |post|%>
<% #post.each do |post|%>
<% if post.image.present?%>
<h1> <%= link_to post.title, post %></h1><br>
<%= image_tag post.image.url(:thumb)%>
<%= simple_format post.content %>
<hr color="blue" >
<% else %>
<h1> <%= link_to post.title, post %></h1><br>
<%= simple_format post.content %>
<hr color="blue">
<% end %>
<% end %>
Related
i want to create database in ruby on rails.
This worked fine in the development environment.
but upload AWS lightsail, this occurred error.
What's the problem?
below using gem
source 'https://rubygems.org'
gem 'kaminari'
gem 'faker'
gem 'carrierwave'
gem 'fog-aws'
gem 'mini_magick'
gem 'devise'
gem 'sunspot_rails'
gem "bootstrap_form", ">= 4.0.0.alpha1"
gem 'rails', '5.0.6'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'figaro'
gem 'sunspot_solr'
gem 'sqlite3'
group :development, :test do
gem 'byebug'
end
group :development do
gem 'web-console', '~> 2.0'
gem 'spring'
end
below create controller
def create
#person = Person.new(params.require(:person).permit(:name, :phone, :relation, :user_id, :image))
if #person.save
redirect_to people_path
else
redirect_to new_person_path
end
end
below new.html.erb
<body class="main4">
<div class="container" style = "letter-spacing:-1px;">
<center><h2 class="mt-5">연락처 추가</h2></center>
<div style="font-weight: bold; display:flex; justify-content:center; margin-top: 10px; letter-spacing:-1px;">
<%= form_for :person, :html => { :multipart => true, name: "personForm", :onsubmit => "return validateform();"}, url: people_path do |f| %>
<div class="group">
<%= f.file_field :image %>
<span class="highlight"></span><span class="bar"></span>
</div>
<br/>
<div class="group">
<%= f.text_field :name , required: true %>
<span class="highlight"></span><span class="bar"></span>
<label>이름</label>
</div>
<br/>
<div class="group">
<%= f.number_field :phone , required: true %>
<span class="highlight"></span><span class="bar"></span>
<label>전화번호</label>
</div>
<br/>
<div class="group">
<span>관계    </span>
<%= f.select(:relation, ['가족', '친구', '지인', '직장동료', '거래처', '기타'], {:class => 'form-control'})%>
</div>
<%= f.hidden_field :user_id, value: current_user.id %>
<p style="text-align:right;"><%= f.button :추가, label: '추가', :class => 'btn btn-outline-light' %></p>
<% end %>
</div>
</div>
</div>
<script>
function validateform(){
var personName = document.personForm.person_name;
var personPhone = document.personForm.person_phone;
var personImage = document.personForm.person_image;
if(personName.value==""){
alert("이름이 입력되지 않았습니다.");
document.personForm.person_name.focus();
document.getElementById('person_name').select();
return false;
}
else if(personName.value.length > 4){
alert("이름은 4글자 이하로 작성해주세요.");
document.personForm.person_name.focus();
document.getElementById('person_name').select();
return false;
}
else if(personPhone.value==""){
alert("전화번호가 입력되지 않았습니다.");
document.personForm.person_phone.focus();
document.getElementById('person_phone').select();
return false;
}
}
</script>
</body>
below error scene in lightsail
enter image description here
Again, it works in the development environment, but if you put it on the AWS lightsail, i'm get this error
Error in terminal while running rails s:
Started POST "/users/confirm" for ::1 at 2017-01-28 15:12:30 -0600
Processing by UsersController#confirm as HTML
No template found for UsersController#confirm, rendering head :no_content
It seems that this is actually an Unknown Format error in rails that is passing as a 204 No Content error. I am really new to rails, and doing this for a course.
I have a controller with one def confirm action. It isn't repeated anywhere in my file. Unlike most of the posts previous there isn't a respond_to method located within this action.
Where does the error actually stem from? I don't know if I should be looking at config or re-working the controller
This is my gemfile:
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'rails', '5.0.1'
group :production do
gem 'pg'
gem 'rails_12factor'
end
group :development do
gem 'sqlite3'
gem 'pry-rails'
end
group :development, :test do
gem 'rspec-rails', '~> 3.0'
gem 'shoulda'
gem 'rails-controller-testing'
end
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'listen'
gem 'bootstrap-sass'
gem 'bcrypt'
edit.html.erb
<h1>Sign Up Confirmation</h1>`
<div class="row">
<div class='col-md-2'>
<h3>Are you sure these values are correct?</h3>
<h5>Name: <% #user.name %></h5>
<h5>Email: <% #user.email %></h5>
</div>
</div>
<div class="row">
<div class="col-md-2", style="width: auto;">
<%= button_to "Yes", {controller: "users",
action: "create", params: params}, class: 'btn btn-success' %>
</div>
<div class="col-md-2", style="width: auto;">
<%= link_to "No", new_user_path, class: 'btn btn-default' %>
</div>
</div>
It looks like you're missing a confirm.html.erb file in your views/users directory. Rails looks for a view file to render when a action is called, and it's looking for a file with the same name as the action.
If you haven't already, create that file, save and refresh the page. The error should be gone. The page will be blank unless you populate the file with some HTML, but from here you should be good to go.
I've created a new Rails project, added gem 'simple_form', '~> 3.2', '>= 3.2.1' to my gemfile and configured my project to use Bower, than added Bootstrap via Bower.
Even though I used rails generate simple_form:install --bootstrap, the form elements don't get the Bootstrap css classes.
I've checked the HTML after the page is rendered and the Bootstrap css/js are there. Do I need to use Bootstrap gem instead of Bower package??
Here is the .bowerrc file:
{
"directory": "vendor/assets/components"
}
bower.json
{
"name": "TesteKlipbox",
"authors": [
"Juliano Nunes"
],
"description": "",
"main": "",
"license": "MIT",
"homepage": "",
"private": true,
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"vendor/assets/components",
"test",
"tests"
],
"dependencies": {
"bootstrap": "^3.3.6"
}
}
Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.6'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.15'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'simple_form', '~> 3.2', '>= 3.2.1'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
And the form code:
<%= simple_form_for(#noticia, html: { class: 'form-horizontal' }) do |f| %>
<% if #noticia.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#noticia.errors.count, "error") %> erros impediram que esta notícia fosse salva:</h2>
<ul>
<% #noticia.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :titulo %><br>
<%= f.text_field :titulo %>
</div>
<div class="field">
<%= f.label :texto %><br>
<%= f.text_area :texto %>
</div>
<div class="field">
<%= f.label :data %><br>
<%= f.datetime_select :data %>
</div>
<div class="field">
<%= f.label :tags %><br>
<%= f.text_field :tags %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
UPDATE
Another user has already answered the question. My mistake was that I didn't replace f.text_field by f.input. It has nothing to do with the suggested bootstrap theme question.
To start using Simple Form you just have to use the helper it
provides:
<%= simple_form_for #user do |f| %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>
The helper being f.input instead of the Rails helper(s) such as f.text_field, f.text_area etc. See Simple_Form Usage and Rails Form Helpers.
The below image shows the first input using f.text_field and the second using f.input.
Make sure that bootstrap is stored in the correct location:
bootstrap.min.css as well as bootstrap.css,
bootstrap.min.js and bootstrap.js,
all need to be located in app/assets/stylesheets and app/assets/javascripts respectively.
Add *= require bootstrap to app/assets/stylesheets/application.css and,
add //= require bootstrap to app/assets/javascripts/application.js
I've scoured the internet for an answer and found nothing... this is driving me insane!
No assert_select statements are working in any of my tests. I've repeatedly confirmed that the element is actually in the dom on the targeted page — and it is — but every test fails on an assert_select statement. Even one as general as assert_select 'body'!
Here's one test:
require 'test_helper'
class UsersIndexTest < ActionDispatch::IntegrationTest
def setup
#user = users(:matt)
end
test "index including pagination" do
log_in_as(#user)
get users_path
assert_template 'users/index'
assert_select 'div.pagination'
User.paginate(page: 1).each do |user|
assert_select 'a[href=?]', user_path(user), text: user.first_name
end
end
end
Here's users/index.html.haml
%h1 All Users
= link_to 'New User', new_user_path
= will_paginate
%table
%thead
%tr
%th
%th First name
%th Last name
%th Email
%th
%th
%th
%tbody
- #users.each do |user|
%tr
%td= gravatar_for(user)
%td= user.first_name
%td= user.last_name
%td= user.email
%td= link_to 'Show', user
%td= link_to 'Edit', edit_user_path(user)
%td= link_to 'Destroy', user, :method => :delete, :data => { :confirm => 'Are you sure?' }
= will_paginate
Pretty boring stuff, right? And yet, the test fails:
FAIL["test_index_including_pagination", UsersIndexTest, 0.9574]
test_index_including_pagination#UsersIndexTest (0.96s)
Expected at least 1 element matching "div.pagination", found 0..
Expected 0 to be >= 1.
test/integration/users_index_test.rb:12:in `block in <class:UsersIndexTest>'
Even when I change the assertion to assert_select div, it fails. And, again, I can confirm that the div is present when I visit the index page.
What am I missing here!?!?!?!
If it helps, here's my gemfile:
source 'https://rubygems.org'
gem 'rails', '4.2.0'
gem 'pg'
gem 'puma'
gem 'turbolinks'
gem 'jquery-rails'
gem 'bcrypt', '~> 3.1.7'
gem 'faker', '~> 1.4.2'
gem 'sass-rails', '~> 5.0.1'
gem 'bootstrap-sass', '~> 3.2.0'
gem 'will_paginate', '~>3.0.7'
gem 'bootstrap-will_paginate','~>0.0.10'
gem 'uglifier', '>= 2.5.0'
gem 'haml-rails', '~> 0.8'
gem 'jbuilder', '~> 2.2.3'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'rest-client', '~> 1.7.3'
gem 'bourbon'
group :development, :test do
gem 'byebug'
gem 'web-console', '~> 2.0.0'
gem 'spring'
gem 'better_errors'
end
group :test do
gem 'minitest-reporters', '~> 1.0.5'
gem 'mini_backtrace', '~> 0.1.3'
gem 'guard-minitest', '~> 2.3.1'
end
group :production do
gem 'rails_12factor'
end
ruby '2.2.0'
Thanks to Steve Klein for the suggestion of printing out the response body. Unfortunately, the plot only thickens. The body clearly has the tag I'm targeting!
Added a line to the above test
assert_template 'users/index'
puts #response.body
assert_select 'div.pagination'
And the output has the tag!
← Previous 1 2 Next →
And yet still fails!!!
FAIL["test_index_as_admin_including_pagination_and_delete_links", UsersIndexTest, 1.412393] test_index_as_admin_including_pagination_and_delete_links#UsersIndexTest (1.41s)
Expected at least 1 element matching "div.pagination", found 0..
Expected 0 to be >= 1.
test/integration/users_index_test.rb:15:in `block in <class:UsersIndexTest>'
Make sure you are loading enough test data to trigger pagination. This selector won't be on your page unless pagination kicks in (depends on how you have configured will_paginate in your controller action).
It might be that the problem lies in line 13: User.paginate(page: 1).each do |user|. Try commenting the loop or cutting it out and run the test again. If it succeeds now, it could be that you have changed the default pagination method in your /users_controller.rb file. Try matching your custom pagination method with the one in your integration test. Something like the following for example: User.paginate(page: 1, per_page: 10).each do |user|
I had same problem with assert_select 'div.pagination', so I cought this thread googling the error.
I read the thread and got back to my app.
I tried to test:
assert_select 'div'
which passed.
I then switched back to:
assert_select 'div.pagination'
and test passed as well :O
For your reference, this is my users_index_test.rb
require 'test_helper'
class UsersIndexTest < ActionDispatch::IntegrationTest
def setup
#user = users(:archer)
end
test "index including pagination" do
log_in_as(#user)
get users_path
assert_template 'users/index'
assert_select 'div.pagination'
User.paginate(page: 1).each do |user|
assert_select 'a[href=?]', user_path(user), text: user.name
end
end
end
and this is my fixture users.yml
michael:
name: Michael Example
email: michael#example.com
password_digest: <%= User.digest('password') %>
archer:
name: Sterling Archer
email: duchess#example.gov
password_digest: <%= User.digest('password') %>
lana:
name: Lana Kane
email: hands#example.gov
password_digest: <%= User.digest('password') %>
mallory:
name: Mallory Archer
email: boss#example.gov
password_digest: <%= User.digest('password') %>
<% 30.times do |n| %>
user_<%= n %>:
name: <%= "User #{n}" %>
email: <%= "user-#{n}#example.com" %>
password_digest: <%= User.digest('password') %>
<% end %>
I really don't know why now it pass, but I wanted to contribute in the research of a more logical solution.
Double check your fixture file for typos and make sure that you are indeed loading enough comments to trigger inclusion of a pagination.
I had the same problem as you, originating from chapter 13 of the Ruby on Rails Tutorial. I included 'puts response.body' in my test and discovered that the ERb comments that I attempted to generate in my "comments.yml" were not being generated during the test. The issue turned out to be that I forgot a single '=' sign in an ERb tag. Hopefully for you the solution is equally simple!
For whoever runs into this problem in the future, knowing fully well you tried all of the solutions above and nothing changed.
Pay attention to this line in the users.yml file
Wrong:
<% 30.times do |n| %>
user_<%= n %>:
name: <%= "User #{n}" %>
email: <%= "user-#{n}#example.com" %>
password_digest: <%= User.digest('password') %>
<% end %>
Wrong:
<% 30.times do |n| %>
user_<%= n %>:
name: <%= "User #{n}" %>
email: <%= "user-#{n}#example.com" %>
password_digest: <%= User.digest('password') %>
<% end %>
Correct:
<% 30.times do |n| %>
user_<%= n %>:
name: <%= "User #{n}" %>
email: <%= "user-#{n}#example.com" %>
password_digest: <%= User.digest('password') %>
<% end %>
I'm new to Rails.
I've been tring bootstrap-timepicker-rails gem in my Rails4 application but it's doesn't work.
https://github.com/tispratik/bootstrap-timepicker-rails
Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
gem 'twitter-bootstrap-rails'
gem 'bootstrap-timepicker-rails'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-timepicker
//= require_tree .
//= require_self
$('.t_picker').timepicker()
application.css
*= require_self
*= require bootstrap-timepicker
*= require_tree .
/event/_form.html.erb
<%= form_for(#event) do |f| %>
<% if #event.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#event.errors.count, "error") %> prohibited this event from being saved:</h2>
<ul>
<% #event.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :location %><br>
<%= f.text_field :location %>
</div>
<div class="field">
<%= f.fields_for :schedules do |sch| %>
<%= sch.time_field :start %>
<%= sch.time_field :end %>
<%= sch.text_field :description %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I don't know if you still need this, but I made it work for me (Rails 4 app) by using the 'modal' template & setting specifically the id of my field, like this:
=f.input :time, as: :string, input_html: { class: 'bootstrap-timepicker', :id => 'time'}
& in the script, like this:
$('#time').timepicker({
minuteStep: 15,
etc etc..
Hope that helps :)
Try changing this:
<%= sch.time_field :start %>
<%= sch.time_field :end %>
to this:
<%= sch.time_field :start, class: 't_picker' %>
<%= sch.time_field :end, class: 't_picker' %>
Maybe it will help.