I am in a dilemma, I'm new and I wonder if you can pass variables to .footer driver, and bootstrap use, chiro display data in a label.
This is the code of the controller:
class JuegosController < ApplicationController
load_and_authorize_resource param_method: :allowed_params, except: [:index, :show]
def index
#juego = Juego.all
end
def show
#juego = Juego.find(params[:id])
end
def new
#juego = Juego.new
end
def create
#juego = Juego.new(allowed_params)
if #juego.save
redirect_to juegos_path
else
render 'new'
end
end
def edit
#juego = Juego.find(params[:id])
end
def update
#juego = Juego.find(params[:id])
if #juego.update_attributes(allowed_params)
redirect_to juegos_path
else
redender 'new'
end
end
def destroy
#juego = Juego.find(params[:id])
#juego.destroy
redirect_to juegos_path
end
private
def allowed_params
params.require(:juego).permit(:id_juego, :nombre, :descripcion, :numero_jugadores, :imagen )
end
end
y este es el codigo del .footer :
<footer class="footer" id="footer-new">
<div class="container-fluid">
<div class="row">
<div class="col-md-3">
<h4>ToyApp</h4>
<p>
<%= link_to 'Inicio', page_path('home') %><br>
<%= link_to 'Acerca de', page_path('about') %><br>
<%= link_to 'Contacto', page_path('contact') %><br>
<%= link_to 'Ayuda', page_path('help') %><br>
<%= link_to 'Términos', page_path('terms') %>
</p>
</div>
<div class="col-md-3">
<h4>Juegos</h4>
<p>
</p>
</div>
<div class="col-md-6" id="bottom-logo-block">
<img src="assets/images/a.png" alt="" width="80" height="80">
<p>
es una marca registrada. Esta prohibido su uso<br>
</p>
<%= link_to 'Políticas de privacidad', page_path('privacy') %>
</div>
</div>
<div class="row">
<div class="col-md-12">
<br>
<p>
xxxxxxxx xxxxxxxxx xxxxxx
</p>
</div>
</div>
</footer>
My question is: how to display the name of a label or h1?
I tried with this but is not work :c
<h1><%= #juego.nombre %></h1>
Help me please, I am newbie.
If the footer is used across the application then you can look at using
content_for / yield
in the footer:
<h1><%= yield :footer_title %></h1>
in the view you can then set the title by controller / action something like
<% content_for :footer_title { #juego.nombre } %>
or
<% content_for :footer_title do %>
Another title
<% end %>
It depends on the context in which the footer is being rendered. If the footer is being rendered within the context of a controller action in which #juego is defined then you should be able to call methods on the object. Two things though.
1) You are defining #juego differently in the different actions. In most of them it refers to a single object, whereas in the index method it refers to a collection of #juego objects. In this context, calling #juego.nombre will almost certainly fail.
2) This is bad practice. If your footer is global (i.e. if it is being shared between different contexts), then you should not be presuming that variable specific to a certain controller is always defined.
Related
Rails 5.1.4, ruby:2.3.7
I have a nested form for a parent model and children model with a validation on the uniqueness of the label in the child model in scope of the parent one. The issue is when I submit the form with identical names in both forms non of them get wrapped in the field_with_errors class. I need the html tag to be wrapped in that class so I can subsequently render errors on the form using bootstrap 4 invalid-feedback and invalid css classes.
How can I render the form with the field_with_errors div wrapping the text_field for the dashboard_label?
The models are as follows:
class Project < ApplicationRecord
has_many :study_media_files
end
class StudyMediaFile < ApplicationRecord
belongs_to :project
validates :dashboard_label, uniqueness: { scope: :project }
end
The controller code is as follows:
class DsasController < ApplicationController
def show
#project = Mrcore::Project.find params[:id]
end
def update
#project = Mrcore::Project.find params[:id]
if(#project.update project_params)
redirect_to #project, notice: 'updated successfully'
else
render 'show'
end
end
private
def project_params
params.require(:project).permit(study_media_files_attributes: %i[id dashboard_label show])
end
end
The view:
<%= f.fields_for :study_media_files, #project.study_media_files.non_zero_duration do |media_form| %>
<div class="row ">
<div class="col-4 small">
<%= media_form.object.affdex_movie_id %>
</div>
<div class="col-5">
<%= media_form.text_field :dashboard_label, class: 'input-sm col-lg form-control', required: true %>
</div>
<div class="col-2 float-right">
<%= media_form.check_box :show, { class: 'form-check-input ml-2' } %>
</div>
</div>
<% end %>
It seems that for some reason the object's dashboard_label gets reset to its original value.
The issue is that the view with the fields for function loads the data from the database, I guess and overrides the errors that are transient on the project object, using a condition in the view code as below solved the issue.
<%= f.fields_for :study_media_files do |media_form| %>
<% if media_form.object.duration > 0 %>
<div class="row ">
<div class="col-4 small">
<%= media_form.object.affdex_movie_id %>
</div>
<div class="col-5">
<%= media_form.text_field :dashboard_label, class: 'input-sm col-lg form-control' %>
</div>
<div class="col-2 float-right">
<%= media_form.check_box :show, { class: 'form-check-input ml-2' } %>
</div>
</div>
<% end %>
<% end %>
Essentially moving the collection on the fields_for block to a condition inside the block.
I'm actually making an ecommerce web app which has a User, Category, Book and Comment models. Everything is working nice, but when I try to comment in one of the book, it gives a 400 error. I really need you to help me out. https://github.com/felixpro/Book-app this is my repository.
This is my CommentsController
class CommentsController < ApplicationController
before_action :authenticate_user!
def create
book = Book.find(params[:comment][:book_id])
comment = book.comments.build(comment_params)
comment.user = current_user
if comment.save
redirect_to book_path(#book)
end
end
private
def comment_params
params.require(:comment).permit(:body)
end
end
This is the comment view partial,
<% if signed_in? %>
<div class="card bg-light new-comment">
<div class="card-body">
<p class="font-weight-bold">Deja tu comentario:</p>
<%= form_for #book.comments.build do |f| %>
<%= f.hidden_field :book_id, value: #book.id %>
<div class="form-group">
<%= f.text_area :body, rows: 4, class: "form-control" %>
</div>
<div class="text-right">
<%= f.submit "Comentar", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
<% else %>
<div class="card bg-light mt-5">
<div class="card-body">
<p class="card-text text-center lead"><%= link_to "Regístrate", new_user_registration_path %> o <%= link_to "Ingresa", new_user_session_path %> para comentar</p>
</div>
</div>
<% end %>
Here are the routes
Rails.application.routes.draw do
devise_for :users
root 'books#index'
resources :books
resources :comments, only: [:create]
end
The error say
This is a pictures showing the error message
The error you mentioned is linked to the fact that you have a special invisible character (non-breaking space) at line 9 and 14 in your CommentsController. This is why you get the
NameError (undefined local variable or method `' for ...)
This often happens when you hit an additional key at the same time you hit the space bar (cmd + space bar on MacOS). Delete those empty lines and type the enter key again to clear the character.
Then the other answer is right, you'll have have to update your book variable name.
You have referred to #book when the variable is a local book. Use # at the beginning of the line 6:
#book = Book.find(params[:comment][:book_id])
This is the form that I made from Clients_indexI am using Elastic Search to find a client's name. I created a search for but as soon as you click search it takes me to a second page.
It takes me here
Here is my view for Clients/_index.html.erb
<label for="existing-ads-client-name" class="big">CLIENT INFORMATION</label><br>
<div class="existing-ads-client-name">
<%=form_tag clients_path,class: 'search-client', method: :get do |f| %>
<div id="client-name">
<%=text_field_tag :name, params[:name] %>
</div>
<%=button_tag "Search", class: 'btn btn-default', name: nil %>
<% end %>
</div>
I am rendering it from AdGroups/index.html.haml
<div class="module existing-ads">
<div class="first-column">
<div class="field-content">
=render 'clients/index'
And here is my Clients controller
class ClientsController < ApplicationController
def index
#clients = Client.search((params[:name].present? ? params[:name]: '*')).records
render :partial => 'clients/index', locals: {clients: #clients}
end
end
Any idea why?
You can see here that it seems like the raw contents of my DB are being printed to the page. I can't see anywhere in my code why there would be the raw output of the db printed to the view. Here is the code for the index view:
<div class="main">
<div="messages">
<%=#messages.each do |t|%>
<h2 class="subject"><%=t.subject%></h2>
<p class="content"><%=t.content%></p>
<% end %>
<%=link_to "Create Message", edit_path%>
</div>
</div>
The Create Form/View:
<div class="formWrapper">
<%= form_for #messages do |t|%>
<div class ="Inputs">
<%=t.text_field :subject%><br>
<%=t.text_area :content%>
<div class="submit">
<%=t.submit "Submit"%>
</div>
<%end%>
</div>
</div>
The Controller:
class MessagesController < ApplicationController
def index
#messages=Message.all
end
def new
#messages=Message.new
end
def create
#messages = Message.new(message_params)
if #messages.save
redirect_to '/'
else
render 'new'
end
end
private
def message_params
params.require(:message).permit(:content, :subject)
end
end
you don't need the = here: <%=#messages.each do |t|%>, the equals sign is telling erb to show every message on the view.
<% %>
Will execute Ruby code with no effect on the html page being rendered. The output will be thrown away.
<%= %>
Will execute Ruby code and insert the output of that code in place of the <%= %>
example...
<% puts "almost" %> nothing to see here
would render as
nothing to see here
however
<%= puts "almost" %> nothing to see here
would render as
almost nothing to see here
Look at <% %>(without equal) in ruby erb means?
I've got a controller method:
def latest_blogs
#latest_blogs = BlogEntry.all.order('dato DESC').limit(4)
end
A root html.erb file which acts as my home page:
<div class="body-box">
<div class="body-content">
<%= render :partial => 'blog_list' %>
</div>
</div>
The blog_list partial:
<div class="blog-list">
<%= latest_blogs.each do |blog| %>
<%= render :partial => "blog_preview", locals: {blog: blog} %>
<% end %>
</div>
And the blog_preview partial which is just a stub for now:
<div class="blog-preview">
<%= blog.title %>
</div>
My routes.rb entries:
resources :blog_entries
root :to => 'home#blog_home', :as => 'root'
How can I tell my app when it loads the root page to present latest_blogs? Thanks.
I would start with: this method should not be in the controller. Put it in a model instead
class BlogEntry < ActiveDirectory::Base
def self.latest (n = 4)
all.order('dato desc').limit(n)
end
Then just use it as:
BlogEntry.latest
Note that this method will also be available on relations:
user.blog_entries.latest(1) #=> returns last user blog entry
You can declare the latest_blog as helper method. Please try the following:
class BlogsController < ApplicationController
def latest_blogs
#latest_blogs = BlogEntry.all.order('dato DESC').limit(4)
end
helper_method :latest_blogs
end
Is latest_blogs your actual controller action, or is it just some method you're exposing to the view? If it's just some method you're exposing then you should make it a helper method.
def latest_blogs
#latest_blogs ||= BlogEntry.all.order('dato DESC').limit(4)
end
helper_method :latest_blogs
Note that I changed the = to ||= in here. I think that's better in this case so that if you happen to call the helper multiple times then it won't re-evaluable it repeatedly.
And FYI, you can also clean up your markup code a little bit. Your root html file could do:
<div class="body-box">
<div class="body-content">
<div class="blog-list">
<%= render partial: 'blog_preview', collection: latest_blogs %>
</div>
</div>
</div>
Then _blog_preview.html.erb:
<div class="blog-preview">
<%= blog_preview.title %>
</div>