I have an issue related to AJAX - ruby

I need to get the value after it is created, just below the section "NEW"
Below is the image I intended to do, and after the data was created I wanted it right there
phrases_term_controller.rb
class PhrasesTermsController < ApplicationController
before_action :authenticate_user!
before_action :set_term
def new
#phrases_term = PhrasesTerm.new
end
def create
#phrases_term = #term.phrases_terms.new(phrases_term_params)
if #phrases_term.save
redirect_to term_phrases_term_path(#term, #phrases_term), notice: "Phrases_Term was successfully created"
else
render "new"
end
end
def show
#phrases_term = PhrasesTerm.find(params[:id])
end
private
def phrases_term_params
params.require(:phrases_term).permit(:term_id, :phrase_id)
end
def set_term
#term = Term.find(params[:term_id])
end
end
View
phrases_term
new.html.erb
<h1><%= I18n.t("pages.phrases_term.new.title") %></h1>
<%= render 'form', phrases_term: #phrases_term %>
_form.html.erb
<%= form_with scope: :phrases_term, url: term_phrases_terms_path, local: true do |form| %>
<% if phrases_term.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(phrases_term.errors.count, "error") %> prohibited this phrase from being saved:</h2>
<ul>
<% phrases_term.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :term_id %> :
<%= form.number_field :id %>
</div>
<div class="dropdown">
<button class="dropbtn">Phrase</button>
<div class="dropdown-content">
<a><%= form.select :phrase_id, Phrase.all.collect { |p| [ p.id ] }, include_blank: true %></a>
</div>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
Term:
_form.html.erb
<%= form_with(model: term, local: true) do |form| %>
<% if term.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(term.errors.count, "error") %> prohibited this term from being saved:</h2>
<ul>
<% term.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :word %>
<%= form.text_field :word %>
</div>
<div class="field">
<%= form.label :meaning %>
<%= form.text_field :meaning %>
</div>
<div class="field">
<%= form.label :reading %>
<%= form.text_field :reading %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
I am not sure I have provided all the necessary information, if I want to add some more information, please comment below. ---Thank You.----

Related

link_to expecting keyword_end rails

i don't understand why but i still have a problem when following the ruby on rails tutorial. It's when you put the links on to navigate between the different page..
this is my view:
<h1>New Article</h1>
<%= form_for #article, url: articles_path do |f| %> <!<%= form_for :article, url: articles_path do |f| %> >
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<%= link_to 'Back', articles_path %>
and this is my controller
class ArticlesController < ApplicationController
def new
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new' #on utilise render car cela permet de recharger la requête d'article
#qui vient d'échouer contrairement à redirect_to qui lancerais une nouvelle requete
end
end
def index
#articles = Article.all
end
def edit
end
def show
#article = Article.find(params[:id])
end
def update
end
def destroy
end
end
private
def article_params
params.require(:article).permit(:title, :text)
end
if you need some over tell me.
So if you have any tips, thanks a lot
Its not the link that is expecting end keyword. The actual error is due to no end tag to the form.
Please update yours as below
<h1>New Article</h1>
<%= form_for #article, url: articles_path do |f| %> <!<%= form_for :article, url: articles_path do |f| %> >
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<% #article.errors.full_messages.each do |msg| %>
<ul>
<li><%= msg %></li>
</ul>
<% end %>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<%end%>
<%= link_to 'Back', articles_path %>
It seems you have not initialized your #article variable in the new action.
In your new action write it as #article = Article.newand it should work fine.
As you have not initialized #article, it gives you a nil value error in the form.
Thanks for your quick answer, and i did what you told me to do. In fact I missed the end in the form. But now a new error comes which points the "heading" line 2:
Error message:
"First argument in form cannot contain nil or be empty"
The new code
<h1>New Article</h1>
<%= form_for #article url: articles_path do |f| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<%end%>
<%= link_to 'Back', articles_path %>
Can you find the bug. I would like to improve myself in Ruby and Ror.
Thanks a lot

Spree: Deface getting error NameError in Spree::Home#index

I'm trying to change the look of the front page where the items are listed but I'm getting an error. Am I using deface correctly to change it? Its telling me that <%= link_to small_image(product, itemprop: "image"), url, itemprop: 'url' %> is giving me errors, if I removed then there are no pictures. What do I do?
update_products.rb
Deface::Override.new(:virtual_path =>"spree/shared/_products",
:name => "change site",
:replace =>"#products",
:text => '
<%
paginated_products = #searcher.retrieve_products if params.key?(:keywords)
paginated_products ||= products
%>
<% content_for :head do %>
<% if paginated_products.respond_to?(:num_pages) %>
<%= rel_next_prev_link_tags paginated_products %>
<% end %>
<% end %>
<div data-hook="products_search_results_heading">
<% if products.empty? %>
<div data-hook="products_search_results_heading_no_results_found">
<%= Spree.t(:no_products_found) %>
</div>
<% elsif params.key?(:keywords) %>
<div data-hook="products_search_results_heading_results_found">
<h6 class="search-results-title"><%= Spree.t(:search_results, keywords: h(params[:keywords])) %></h6>
</div>
<% end %>
</div>
<% if products.any? %>
<div id="products" class="row" data-hook>
<% products.each do |product| %>
<% url = spree.product_url(product, taxon_id: #taxon.try(:id)) %>
<div id="product_<%= product.id %>" class="col-md-3 col-sm-6 product-list-item" data-hook="products_list_item" itemscope itemtype="https://schema.org/Product">
<div class="panel panel-default">
<% cache(#taxon.present? ? [I18n.locale, current_currency, #taxon, product] : [I18n.locale, current_currency, product]) do %>
<div class="panel-body text-center product-body">
<br/>
<%= link_to small_image(product, itemprop: "image"), url, itemprop: 'url' %>
</div>
<div class="panel-footer text-center">
<span itemprop="offers" itemscope itemtype="https://schema.org/Offer">
<span class="price selling lead" itemprop="price"><%= display_price(product) %></span>
</span>
</div>
<% end %>
</div>
</div>
<% end %>
<% reset_cycle("classes") %>
</div>
<% end %>
<% if paginated_products.respond_to?(:num_pages) %>
<% end %>
')
one small quote issue for url <%= link_to small_image(product, itemprop: "image"), url, itemprop: "url" %> check this code :-
Deface::Override.new(:virtual_path =>"spree/shared/_products",
:name => "change site",
:replace =>"#products",
:text => '
<%
paginated_products = #searcher.retrieve_products if params.key?(:keywords)
paginated_products ||= products
%>
<% content_for :head do %>
<% if paginated_products.respond_to?(:num_pages) %>
<%= rel_next_prev_link_tags paginated_products %>
<% end %>
<% end %>
<div data-hook="products_search_results_heading">
<% if products.empty? %>
<div data-hook="products_search_results_heading_no_results_found">
<%= Spree.t(:no_products_found) %>
</div>
<% elsif params.key?(:keywords) %>
<div data-hook="products_search_results_heading_results_found">
<h6 class="search-results-title"><%= Spree.t(:search_results, keywords: h(params[:keywords])) %></h6>
</div>
<% end %>
</div>
<% if products.any? %>
<div id="products" class="row" data-hook>
<% products.each do |product| %>
<% url = spree.product_url(product, taxon_id: #taxon.try(:id)) %>
<div id="product_<%= product.id %>" class="col-md-3 col-sm-6 product-list-item" data-hook="products_list_item" itemscope itemtype="https://schema.org/Product">
<div class="panel panel-default">
<% cache(#taxon.present? ? [I18n.locale, current_currency, #taxon, product] : [I18n.locale, current_currency, product]) do %>
<div class="panel-body text-center product-body">
<br/>
<%= link_to small_image(product, itemprop: "image"), url, itemprop: "url" %>
</div>
<div class="panel-footer text-center">
<span itemprop="offers" itemscope itemtype="https://schema.org/Offer">
<span class="price selling lead" itemprop="price"><%= display_price(product) %></span>
</span>
</div>
<% end %>
</div>
</div>
<% end %>
<% reset_cycle("classes") %>
</div>
<% end %>
<% if paginated_products.respond_to?(:num_pages) %>
<% end %>
')

Rails 4.2 Generator ERB Template methods and variables, where do they come from?

In the ERB for scaffold generators in Rails 4.2 from where do the variables / methods come from in the template?
Things like singular_table_name and attributes in the below '_form.html.erb' template. Are they methods? variables? where can I find a list of what is available for use in this template?
I tried using some ActiveRecord class methods for example (reflections) and they were not available.
If it is only a set list of methods, not drawn from the AR class, is there a way to pass instance variables from the generators like you would from a controller to a view?
Originally from .rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/generators/erb/scaffold/templates/_form.html.erb
I now placed this file (which works) in rails_app/lib/templates/erb/scaffold
<%%= form_for(#<%= singular_table_name %>) do |f| %>
<%% if #<%= singular_table_name %>.errors.any? %>
<div id="error_explanation">
<h2><%%= pluralize(#<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</h2>
<ul>
<%% #<%= singular_table_name %>.errors.full_messages.each do |message| %>
<li><%%= message %></li>
<%% end %>
</ul>
</div>
<%% end %>
<% attributes.each do |attribute| -%>
<div class="field">
<% if attribute.password_digest? -%>
<%%= f.label :password %><br>
<%%= f.password_field :password %>
</div>
<div class="field">
<%%= f.label :password_confirmation %><br>
<%%= f.password_field :password_confirmation %>
<% else -%>
<%%= f.label :<%= attribute.column_name %> %><br>
<%%= f.<%= attribute.field_type %> :<%= attribute.column_name %> %>
<% end -%>
</div>
<% end -%>
<div class="actions">
<%%= f.submit %>
</div>
<%% end %>

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.

How to DRY up some form code

Having a hard time with this because of the numerous objects that get passed around... how would you write a partial or whatever to DRY this up?
inventory/new.html.erb
<% Categorylist.includes(:itemlists).where(itemlists: { inventory_list: true }).each do |n| %>
<div class="col-xs-4">
<div class="form-group" style="font-size:75%">
<h5> <%= n.name %> </h5>
<% n.itemlists.each do |i| %>
<%= number_field_tag "inventory[#{i.id}]", :quantity, min: 0, value: #inventory_params ? #inventory_params[i] : 0, class: "shrink_num_field" %>
<%= label_tag i.name, i.name %>
</br>
<% end %>
</div>
</div>
<% end %>
request/new.html.erb
<% Categorylist.includes(:itemlists).where(itemlists: { request_list: true }).each do |n| %>
<div class="col-xs-4">
<div class="form-group" style="font-size:75%">
<h5> <%= n.name %> </h5>
<% n.itemlists.each do |i| %>
<%= number_field_tag "borrow[][#{i.id}]", :quantity, min: 0, value: #borrowparams ? #borrowparams[i] : 0, class: "shrink_num_field" %>
<%= label_tag i.name, i.name %>
</br>
<% end %>
</div>
</div>
<% end %>
Specifically, I'm having a hard time getting the partial to differentiate between inventory_list vs. request_list in the first line.

Resources