How to write partial ERB template in Sinatra? [closed] - ruby

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How to refactor this code? I write on Sinatra I need to separate into a separate file
<div class="row">
<div class="col-xs-3">
<% #user.posts.each do |post| %>
<%= post.title %><br>
<p><%= post.body %></p>
<% unless post.comment.blank? %>
<% post.comment.each do |comment| %>
<p><%= comment.body %></p>
<% unless comment.comment.blank? %>
<% comment.comment.each do |comment2| %>
<p><%= comment2.body %></p>
<% unless comment2.comment.blank? %>
<% comment2.comment.each do |comment3| %>
<p><%= comment3.body %></p>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
</div>
</div>
my model looped comment
def up
create_table :comments do |t|
t.string :body
t.integer :post_id
t.integer :comment_id
t.timestamps
end
end
This is my migration model comment

From the documentation:
Use it as follows to render the mypartial.haml(1) or the
admin/mypartial.haml(2) partials, or with a collection (3) & (4):
<%= partial(:mypartial) %> <!--(1)-->
<%= partial(:'admin/mypartial') %> <!--(2)-->
<%= partial(:object, :collection => #objects) %> <!--(3)-->
<%= partial(:'admin/object', :collection => #objects) %> <!--(4)-->
In (1) & (2), the partial will be rendered plain from their files,
with no local variables (specify them with a hash passed into
:locals). In (3) & (4), the partials will be rendered, populating the
local variable object with each of the objects from the collection.
So your code should be:
<div class="row">
<div class="col-xs-3">
<% #user.posts.each do |post| %>
<%= post.title %><br>
<p><%= post.body %></p>
<%= partial(:comment, :collection => post.comment) %>
<% end %>
</div>
</div>
with the new file comment.erb:
<p><%= comment.body %></p>
<%= partial(:comment, :collection => comment.comment) %>

Related

I have an issue related to AJAX

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.----

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

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 `model_name' for Array:Class, Partial views

I am using partial view that is not working, in review#new action, I have a from and I am calling the post partial view :
<%= form_for #review do |f| %>
<div>
<%= field_with_error #review, :content do %>
<%= f.label :content %><br />
<%= f.text_area :content %>
<% end -%>
</div>
<%= field_with_error #review, :score do %>
<%= f.label :score %>
<%= f.text_field :score %>
<% end -%>
<p><%= submit_tag 'Post Review'%> </p>
<% end %>
</div>
<ul >
<%= render :partial => "post", :locals => {:review => #reviews} %>
</ul>
in Review#new controller :
def new
#review = Review.new(:restaurant_id => params[:restaurant_id])
#reviews = Review.all
end
and review#_post looks like:
<%= content_tag_for(:li, review) do %>
<p ><%= "#{review.first_name}" %></p>
<p ><%= review.content %></p>
<span >Posted at <%= review.created_at %> ago.
(<%= link_to 'Delete', post, :confirm => 'Are you sure?', :method => :delete %>)</span>
<% end %>
It gives an error:
undefined method `model_name' for Array:Class
I think I am doing sth wrong with the partial view, Thanks you in advance :)
In your view you have:
<ul >
<%= render :partial => "post", :locals => {:review => #reviews} %>
</ul>
You're passing #reviews here which is an array, not the model #review. Delete the s at the end of #reviews here and it should work. :)
EDIT: If you want to render a list of all the reviews in #reviews, you could do something like this:
<ul >
<%= render :partial => "post", :collection => #reviews %>
</ul>
Check the documentation on partials for more options on how to do this.

Fields_for Nested Model Rails 3.2.2

I have some nested models in my Rails application.
i have an article hat has mny properties.
class Article < ActiveRecord::Base
has_many :properties, :dependent => :destroy
accepts_nested_attributes_for :properties
end
class Property < ActiveRecord::Base
belongs_to :article
end
And now i want to edit this in my View so I editet the controler
# GET /articles/new
# GET /articles/new.json
def new
#article = Article.new
3.times { #article.properties.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: #article }
end
end
And also edited the View and the _format.html.erb
<%= form_for(#article) 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 %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<% f.fields_for :properties do |prop| %>
<div class="field">
<%= prop.label :name %><br />
<%= prop.text_field :name %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
But there is no way to show up. If i want to create a new Model, i can not see any input fields for the properties.
What have i do wrong?
You're missing an = in your fields_for line. That is, it should be:
<%= f.fields_for :properties do |prop| %>

Resources