Values are not showing using Rails 3 - ruby

I want to display data inside text field and those values will be fetched dynamically.But i am not getting any value(only blank filed is coming) when show action is executing.
Please check mu code given below.
Views/posts/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<input type="text",value="<%= #post.name %>">
</p>
<p>
<b>Title:</b>
<input type="text",value="<%= #post.title %>">
</p>
<p>
<b>Content:</b>
<input type="text",value="<%= #post.content %>">
</p>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%= link_to 'Back', posts_path %>
Please help me to resolve this issue.

Replace the value Property by removing quotes as:
<input type="text",value=<%= #post.content %>>

Related

how to "Each do |article|" but after every n-articles it has to start in a new row(foundation)

I got this Rails blogs app and for the indexpage where the articles are posted it shows a short version of the article.
<%= render 'layouts/header' %>
<%= render 'layouts/topbar' %>
<div class="contentnews">
<%= render 'layouts/bannerad' %>
<div class="row">
<% if current_user.try(:admin?) %>
<%= link_to 'Nieuws toevoegen', new_article_path, :class => "button ala" %>
<% end %>
</div>
<div class="row">
<%= render 'overview' %>
</div>
</div>
The overview-partial :
<div class="articlebox ">
<div class="large-4 columns">
<% #articles.reverse_each do |article| %>
<div class="articlebox2">
<div class="articleindex">
<%= link_to article.title, article %>
</div>
<div class="articlebody">
<%= article.short.to_s.html_safe %>
</div>
</div>
<% end %>
</div>
</div>
The problem is that this way all the articles end up in one row. I want it to show the short articles inline on the page and after every two or three article it has to start in a new row with again two or three short articles.
Anybody got any idea?
You don't actually need to create a row every n-elements. With foundation just wrap all the elements in a row, and give them columns class:
<div class="articlebox">
<div class="large-4 columns">
<div class="row"> <!-- Wrap in a row -->
<% #articles.reverse_each do |article| %>
<div class="articlebox2 large-6 columns"> <!-- Give a column and width class here -->
...
</div>
<% end %>
</div>
So you can nesting grids with foundation, and creating new rows is not necessary, foundation will just wrap if the elements exceeds the grid.
If you actually really want to create rows, you can have a look at the method each_with_index API and then insert row elements if index % 2 == 0. But this is unnecessary complexity.

undefined method `errors' for nil:NilClass using RoR

I am getting the following error in my login form when i am trying to attempt by putting the wrong data which should usually shows the login failed message.
Error:
NoMethodError in Sessions#loginadmin
Showing C:/Site/swargadwar_admin/app/views/homes/index.html.erb where line #71 raised:
undefined method `errors' for nil:NilClass
Extracted source (around line #71):
68: <% end %>
69: </div>
70: <div class="error-div">
71: <% if #admin.errors.any? %>
72: <div id="error_explanation">
73: <h2><%= pluralize(#admin.errors.count, "error") %> prohibited this post from being saved:</h2>
74:
Please check my codes below and let me to know where i am doing the mistake.
views/homes/index.html.erb
<div class="container">
<div style="text-align:center;"><img src="/assets/admin.png" style="width:100px; height:120px; " /></div>
<div class="text-div" style="text-align:center;">Swargadwar, Puri Municipality,govt of odisha</div>
<section>
<% if !current_user %>
<div id="container_demo" >
<!-- hidden anchor to stop jump http://www.css3create.com/Astuce-Empecher-le-scroll-avec-l-utilisation-de-target#wrap4 -->
<a class="hiddenanchor" id="toregister"></a>
<a class="hiddenanchor" id="tologin"></a>
<div id="wrapper">
<div id="login" class="animate form">
<%= form_for :admin,:url => {:action =>'loginadmin',:controller => 'sessions' } do |f| %>
<h1>Log in</h1>
<p>
<label for="username" class="uname" data-icon="u" > Your email or username </label>
<%= f.email_field :email,placeholder:"mysupermail#mail.com",:id => "username" %>
</p>
<p>
<label for="password" class="youpasswd" data-icon="p"> Your password </label>
<%= f.password_field :password,placeholder:"eg. X8df!90EO",:id => "password" %>
</p>
<p class="keeplogin">
<%= f.check_box :remember_me,:id => "loginkeeping" %>
<label for="loginkeeping">Keep me logged in</label>
<%= link_to 'Forgetting password ?',admins_forget_path %>
</p>
<p class="login button">
<%= f.submit "Login" %>
</p>
<p class="change_link">
Not a member yet ?
Join us
</p>
<% end %>
</form>
</div>
<div id="register" class="animate form">
<%= form_for :admin,:url => {:action => 'create_registration',:controller => "admins" } do |f| %>
<h1> Sign up </h1>
<p>
<label for="usernamesignup" class="uname" data-icon="u">Your username</label>
<%= f.text_field :user_name,placeholder:"mysuperusername690",:id => "usernamesignup" %>
</p>
<p>
<label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
<%= f.email_field :email,placeholder:"mysupermail#mail.com",:id => "emailsignup" %>
</p>
<p>
<label for="passwordsignup" class="youpasswd" data-icon="p">Your password </label>
<%= f.password_field :password,placeholder:"eg. X8df!90EO",:id => "passwordsignup" %>
</p>
<p>
<label for="passwordsignup_confirm" class="youpasswd" data-icon="p">Please confirm your password </label>
<%= f.password_field :password_confirmation,placeholder:"eg. X8df!90EO",:id => "passwordsignup" %>
</p>
<p>
<label for="usernamesignup" class="uname" data-icon="u">Add Image</label>
<%= f.file_field :picture %>
</p>
<p class="signin button">
<%= f.submit "Sign Up"%>
</p>
<p class="change_link">
Already a member ?
Go and log in
</p>
<% end %>
</div>
<div class="error-div">
<% if #admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#admin.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #admin.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
</div>
</div>
</div>
<% end %>
</section>
</div>
controller/sessions_controller.rb
class SessionsController < ApplicationController
def loginadmin
#admin=Admin.authenticate(params[:admin][:email], params[:admin][:password])
if #admin
session[:user_id]=#admin.id
cookies.signed[:user_id]=#admin.id
params[:admin][:remember_me] == '1' ? remember(#admin) : forget(#admin)
flash[:notice]="Login Successfull"
flash[:color]="valid"
redirect_to :action => "new", :controller => "admins"
else
flash[:notice]="Login Failed"
flash[:color]="invalid"
render 'homes/index'
end
end
def removeuser
session[:user_id] = nil
cookies.delete :user_id
flash[:notice]="user logged out successfully"
flash[:color]="valid"
redirect_to :action => 'index', :controller => 'homes'
end
end
controller/homes_controller.rb
class HomesController < ApplicationController
def index
#admin=Admin.new
end
end
Please help me to resolve this error.
Error is self explanatory.
NoMethodError in Sessions#loginadmin
undefined method `errors' for nil:NilClass
You are getting this error because #admin is nil and you are calling errors method on a nil object..
If you look loginadmin action is sessions controller you are rendering 'index' template when there is no #admin object, which is causing this error.
Fix:
Either create separate templates for both these actions with common code in partials or simply use rails try method which returns nil rather than raising an exception
<% if #admin.try(:errors).try(:any?) %>
OR
Just use a condition at top to separate out error code:
<% if #admin %>
<div class="error-div">
<% if #admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#admin.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #admin.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
</div>
<% end %>
P.S I would recommend you to use different templates with common code separated in partials.

submit action doesnt work in Rails 4

when I submitted the form, the new data don't save in database and as a result I can not see them, but when I make a new data like welcome= Welcome.new and welcome.title="your are" by rails console they work and I can only see things that I have made in console not by application. It seems submit bottom doesnt save any data.
new.html.erb
<h1> Submite </h1>
<form action="new" method="post" class="form-horizontal" role ="form">
<div class="form-group">
<label for"kind"> kind </label>
<input type="string" id="kind" name="kind" class="form-control">
</div>
<br>
<div class="form-group">
<label for"title"> title </label>
<input type="string" id="title" name="title" class="form-control">
</div>
<br>
<div class="form-group">
<label for"text">"text </label>
<input type="text" id="text" name="text" class="form-control">
<br>
</div>
<br>
</div>
<input type="submit" value="Submit" class="btn btn-primary">
List
</div>
index.html.erb
<% #welcomes.each do |welcome| %>
<li><%= welcome.title %> </li>
<% end %>
welcomes_controller
def index
#welcomes = Welcome.all
end
def new
#welcome = Welcome.new
end
def create
#welcome = Welcome.new(user_params)
if #welcome.save
flash.keep[:notice]="Successfuly created"
render :index
else
render :new
# format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
private
def user_params
params.require(:welcome).permit(:kind, :title, :text)
end
Your form is not submitting to the right route. Use form builder instead:
<%= form_for #welcome do |f| %>
<%= f.label :kind %>
<%= f.text_field :kind %>
<%= f.submit %>
<% end %>
This will put right route for form submission into your HTML

Showing check box label with single quotes

Not able to show jQuery mobile check box with values like aa''a#a.com
#contact_info = [{:source_id=>40021, :object=>"148302514605161.2", :account_number=>"332950", :contact_type=>"E", :contact_value=>"aa''a#a.com", :sequence=>"1"}, {:source_id=>40021, :object=>"148302514605162.2", :account_number=>"332950", :contact_type=>"E", :contact_value=>"asdf#asd.org", :sequence=>"2"}, {:source_id=>40021, :object=>"148302514605163.2", :account_number=>"332950", :contact_type=>"E", :contact_value=>"dog#g.com", :sequence=>"3"}, {:source_id=>40021, :object=>"148302514605164.2", :account_number=>"332950", :contact_type=>"E", :contact_value=>"gh'g#ff.bn", :sequence=>"4"}, {:source_id=>40021, :object=>"148302514605165.2", :account_number=>"332950", :contact_type=>"F", :contact_value=>"2579525795", :sequence=>"5"}]
In .erb file
<table id="previous_email_fax_list">
<% if #contact_info. size > 0 %>
<% #contact_info.each do |contact|%>
<tr>
<td style="border:1px solid black;">
<div class="email_fax_check">
<input type="checkbox" name="<%= contact.contact_value %>" id="<%= contact.contact_value %>" class="custom" />
<label for="<%= contact.contact_value %>"><br/></label>
</div>
<div class="email_fax_label">
<% if contact.contact_type == "E" %>
<%= contact.contact_value %>
<% elsif contact.contact_type == "F" %>
<%= number_to_fax(contact.contact_value) %>
<% end %>
</div>
</td>
</tr>
<% end %>
<% else %>
<tr>
<td align='center'>
No email addresses or fax numbers found for this account
</td>
</tr>
<% end %>
</table>
I am getting Error: Syntax error, unrecognized expression: for='gh'g#ff.bn']
I copied this code:
<input type="checkbox" name="<%= contact.contact_value %>" id="<%= contact.contact_value %>" class="custom" />
<label for="<%= contact.contact_value %>"><br/></label>
and added it to an existing app view, adding a variable, removing contact. and removing all of the double quotes to make a test:
<% contact_value = "'gh'g#ff.bn'" %>
<input type="checkbox" name=<%= contact_value %> id=<%= contact_value %> class="custom" />
<label for=<%= contact_value %>><br/><%= contact_value %></label>
I get:
without any errors. Give it a try without any quotes around the <%= contact.contact_value %> in the view code.

Issue in kaminari ajax pagination

Am using kaminari ajax pagination in my project. It is working fine, But contents are displaying number of pages times in the page. For example if number of items per pages is 7, then it is displaying 7 times the same content. What am doing is
In product_details controller
def index
#products=ProductDetail.where("department_id = ? and category_id = ?",1, 1).page(params[:page]).per(15)
end
In product_details/index.html.erb
<div id="product_details">
<%= render #products %>
</div>
<div id="paginator">
<%= paginate #products, :remote=>true %>
</div>
In product_details/index.js.erb
$('#product_details').html('<%= escape_javascript render (#products) %>');
$('#paginator').html('<%= escape_javascript(paginate(#products, :remote=>true).to_s)%>');
In product_details/_product_detail.html.erb
<div id="product_list">
<% #products.each do | product | %>
<div class="product_container">
<div class="product_box" >
<div id="future_image_container">
<div class="image_block" >
<%= image_tag(product.image_path, :alt => "product image", :height=>"215px") %>
</div>
<span id="future_price" style="text-decoration: line-through; color: #9e9c9c;">
<span style="color: black;">
<%= "$#{product.price}" %>
</span>
</span>
<div id="circle">
<p>
<% if(product.discount > 0) %>
<% new_price=((2.0*((product.price*product.discount)/100)).round)/2.0 %>
<% else %>
<% new_price=product.price %>
<% end %>
<%= "$#{new_price}"%>
</p>
</div>
</div>
<p class="future_product_name">
<%= product.name %>
</p>
<% #brands=Brand.where("id=?",product.brand_id)
#brands.each do |brand|
#brandname=brand.name
end
%>
<p class="future_product_name">
<%= "Brand : #{#brandname}" %>
</p>
</div>
</div>
<% end %>
</div>
Please help me to solve this problem
I noticed that when i use <%= render #products %> how many items i have taken per page that number of times it repeates. Therefore I solved this by the following code:
In product_details controller
def index
#products=ProductDetail.where("department_id = ? and category_id = ?",1, 1).page(params[:page]).per(15)
end
In product_details/home.html.erb
<div id="product_details">
<%= render 'index' %>
</div>
In product_details/index.js.erb
$('#product_details').html('<%= escape_javascript render ('index') %>');
In product_details/_index.html.erb
<div id="product_list">
<%= paginate #products, :remote=>true %>
<% #products.each do | product | %>
<div class="product_container">
<div class="product_box" >
<div id="future_image_container">
<div class="image_block" >
<%= image_tag(product.image_path, :alt => "product image", :height=>"215px") %>
</div>
<span id="future_price" style="text-decoration: line-through; color: #9e9c9c;">
<span style="color: black;">
<%= "$#{product.price}" %>
</span>
</span>
<div id="circle">
<p>
<% if(product.discount > 0) %>
<% new_price=((2.0*((product.price*product.discount)/100)).round)/2.0 %>
<% else %>
<% new_price=product.price %>
<% end %>
<%= "$#{new_price}"%>
</p>
</div>
</div>
<p class="future_product_name">
<%= product.name %>
</p>
<% #brands=Brand.where("id=?",product.brand_id)
#brands.each do |brand|
#brandname=brand.name
end
%>
<p class="future_product_name">
<%= "Brand : #{#brandname}" %>
</p>
</div>
</div>
<% end %>
</div>
Now no product repeates and ajax pagination is also working fine

Resources