Rails 4: Access all CRUD from index.html.erb - html

I'm trying to create only a 1 page index site.
I Want to handle creating, deleting, editing all within my 1 index. (very basic)
However, I followed the beginners for getting started:
http://guides.rubyonrails.org/getting_started.html
Example of my article controller:
def index
#articles = Article.all #Create Articles and then add the for loop inside the index.html
#article = Article.new
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
end
end
def new
#article = Article.new
end
...
private
def article_params
params.require(:article).permit(:title, :text)
end
I Use #articles to populate all articles ever added. And then #article to for CRUD. Except I can't get full control to CRUD from the index.html page.
<form role="form" class="form-inline">
<div class="form-group">
<%= form_for #article do |f| %>
<div class="form-group">
<label for="textUserInputDescription"><%= f.label :title %><br/><%= f.text_field :title %>
</label>
<label for="textUserInputDescription"><%= f.label :text %><br/><%= f.text_area :text %> </label>
<button type="submit" class="btn btn-default"><%= f.submit %></button>
</div>
<% end %>
</div>
When I click Submit, It does not add to my database.
My Question is: How do I get access to CRUD all from within the index.html?
Thanks for your time, I'm still learning Rails/CRUD/ActiveRecord and following tutorials.

I changed f.submit to:
<%= button_to "Create", new_article_path(#article[:title], #article[:text]), method: :post %>

Related

Submitting multiple forms in one click

I was looking at the question below, which has a good answer for what I will ask, but I have a doubt. I cannot comment yet so that is why I am making another post.
How to submit multiple, duplicate forms from same page in Rails - preferably with one button
This is my view for the form
new.html.erb
<h1 class="page-title">Nuevo Precio</h1>
<hr class="title-division">
<div class="alta-form-container">
<%= form_tag "/precios" do %>
<% 2.times do %>
<%= render 'form', precio: #precio %>
<% end %>
<%= submit_tag "Submit", class: "btn btn-success" %>
<% end %>
<%= link_to 'Lista Precios Cliente', client_precios_path %> <br><%= link_to 'Ver', [#client, #precio] %> <br>
</div>
and my _form.html.erb
<div class="field">
<%= label_tag :precio, "Precio" %>
<%= text_field_tag "precios[][precio]" %>
</div>
<div class="field">
<%= text_field_tag "precios[][cant_acomodato]" %>
</div>
<div class="field">
<%= hidden_field_tag "precios[][client_id]" %>
</div>
When I submit the form I get an error that says
'No route matches [POST] "/precios"'
which I'm guessing is because on my new.html.erb I wrote form_tag "/precios" do
Any thoughts on what I should change or edit? thanks in advance
actually you have only one form here, because _form.html.erb will generate only inputs, that according to form below
<%= form_tag "/precios" do %>
that will generate
form action="/precios" method="post">
if you want leave it like that, you should add to your config/routes.rb
post '/precios', to: 'controller_name#method_name', as: :controller_name_method_name
your situation:
post '/precios', to: 'PreciosController#create', as: :precios_create
also check this
documentation

Rails showing ActiveRecord_Relation in my view

Well my problem is when i call my model Question and show in a .each do, my view show the ActiveRecord but i don't now how to hide this or the right way to show my questions without the ActiveRecord.
In my view the content show like this:
<%= #questions = Question.all.order(:id).reverse_order %>
<% #questions.each do |question| %>
<% if #course.id == 1 %>
<h5><%= link_to question.title, question , class: 'reply text-light text-decoration-none' %></h5>
<% end %>
<% end %>
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
You could use <% %> instead of <%= %>:
<% #questions = Question.all.order(id: :desc) %>
And it's better to put that to controller:
class QuestionsController
def index
#questions = Question.all.order(id: :desc)
end
end
I found a solution if i want to call a Model in view, just create a input type="hidden" like this:
<input type="hidden" value="<%= #questions = Question.all.order(:id).reverse_order %>">
You should use url_helpers provided by Rails. See Examples provided under link_to docs.
Also I would suggest to follow the recommendation at https://stackoverflow.com/a/66470651/936494 and in your view change following
<h5><%= link_to question.title, question , class: 'reply text-light text-decoration-none' %></h5>
to
<h5><%= link_to question.title, question_path(question), class: 'reply text-light text-decoration-none' %></h5>
And if you don't have url_helpers available, then the 2nd argument should be a URL to a resource in your application.
Hope that helps. Thanks.

How to route navbar search to a specific index view?

I'm trying to implement a navbar search and have attempted to follow these solutions https://stackoverflow.com/a/19929707/5101493 and
https://stackoverflow.com/a/52538766/5101493.
Unfortunately my navbar search only works when on the /businesses index page and not otherwise. After search submission the app stays on the same page instead of directing to /businesses.
How can I fix this? Here are my relevant files:
application_controller
before_action :set_global_search_variable
def set_global_search_variable
#p = Business.search(params[:q])
end
businesses_controller
def index
#businesses = #p.result(distinct: true).sort_by(&:name)
end
def search
index
render :index
end
routes
resources :businesses do
collection do
match 'search' => 'businesses#search', via: [:get, :post], as: :search
end
end
_header partial
<form class="form-inline" style='display:show'>
<%= search_form_for #p , url: search_businesses_path, :html => { method: :post } do |f| %>
<%= f.search_field :name_cont, {placeholder: " Find a business...", class: 'form-control mr-sm-2'} %>
<%= f.submit "Search", class: "btn look-red my-2 my-sm-0 navsearch" %>
<% end %>
</form>
You can't have a form inside another form. The outermost form is always routing to the current page.
Instead make the outermost form a div.
<div class="form-inline" style='display:show'>
<%= search_form_for #p , url: search_businesses_path, :html => { method: :post } do |f| %>
<%= f.search_field :name_cont, {placeholder: " Find a business...", class: 'form-control mr-sm-2'} %>
<%= f.submit "Search", class: "btn look-red my-2 my-sm-0 navsearch" %>
<% end %>
</div>

Rails simple-form with multiple instances of the same object

I am trying to create a rails form using simple form that uses nested resources. However, I want to be able to submit multiple instances of the associated resource. Example below will probably explain it better.
<div class="tab-pane active" id="reminder">
<%= simple_form_for #collection, html: {multipart: true}, url: collection_index_path do |m| %>
<%= render partial: "collection/tabs/reminder", locals: { :m => m } %>
</div>
-inside partial
<% 9.times do |j|%>
<div class="tab-pane" id="<%= j %>">
<%= m.simple_fields_for :reminder do |p| %>
<%= p.input :heading %>
<%= p.input :message %>
<% end %>
</div>
There is a tabbed pane in which the user can click through 9 tabs to set up to 9 reminders, all should be associated with a collection (collection model accepts nested attributes for reminder). However, the way I have it setup now, the controller only gets what was set in the last reminder in the params. Anyway ideas would be appreciated.
There must be some way to distinguish tabs before submitting to controller. And i think answer might be here.
i.e. it looks like this:
<% 9.times do |j|%>
<div class="tab-pane" id="<%= j %>">
<%= m.simple_fields_for :reminders do |p| %>
<%= p.input :heading %>
<%= p.input :message %>
<% end %>
</div>
<% end %>

<Rails> Page format falls apart when rendering

I created a sample app to play around with rails. I'm having trouble with the page format of my site when a page is rendered. The text fields moves out of place from it's previous position and the format is just ruined. Please see the site and just click the button and you'll see what I'm talking about.
Here is my controller:
class BooksController < ApplicationController
def new
#books = Book.new
end
def show
end
def create
#books = Book.new(book_params)
if #books.save
redirect_to new_book_path, :flash => { :success => "Book Added"}
else
render 'new'
end
end
def update
end
def edit
end
def destroy
end
private
def book_params
params.require(:book).permit(:title, :author, :genre, :no_of_items)
end
end
here my views/new.html.erb
<h1>Books#new</h1>
<p>Find me in app/views/books/new.html.erb</p>
<% flash.each do |key, value|%>
<div class="flash <%= key %>"><%= value %></div>
<% end %>
<div class="form-horizontal">
<div class="control-group row-fluid form-inline">
<%= form_for #books do |f| %>
<%= render 'shared/error_message' %>
<p>
<div class="fields">
<%= f.label :title, "Title:"%>
<%= f.text_field :title %><br/><br/>
</div>
<div class="fields">
<%= f.label :author, "Author:" %>
<%= f.text_field :author %><br/><br/>
</div>
<div class="fields">
<%= f.label :no_of_items, "No. of Items:" %>
<%= f.text_field :no_of_items%><br/><br/>
</div>
<div class="fields">
<%= f.label :genre, "Genre:" %>
<%= f.text_field :genre%><br/><br/>
</div>
<p>
<%= submit_tag "Add book"%>
</p>
</p>
<% end %>
</div>
</div>
Can anyone help me tell what's happening and a fix for this? Thanks.
ANSWER:
Hi everyone thanks for the advises but I found out through Rails 3: "field-with-errors" wrapper changes the page appearance. How to avoid this? thread how to fix this problem. I can't answer my question within 6 hours so I'll answer this after the restriction has ended.
Each label and input is being wrapped in an additional div element with the class field_with_errors.
You could give the field_with_errors class an display: inline-block; property, or try formatting your form like the example in the Bootstrap docs.
<form class="form-horizontal">
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls">
<input type="text" id="inputEmail" placeholder="Email">
</div>
</div>
</form>
http://getbootstrap.com/2.3.2/base-css.html#forms
There is some unnecessary formatting you've added to the new.html.erb. One of rails' strength is the minimal amount of work created with helpers such as form_for
<h1>Books#new</h1>
<p>Find me in app/views/books/new.html.erb</p>
<% flash.each do |key, value|%>
<div class="flash <%= key %>"><%= value %></div>
<% end %>
<div class="form-horizontal">
<div class="control-group row-fluid form-inline">
<%= form_for #books do |f| %>
<%= render 'shared/error_message' %>
<%= f.label :title, "Title:"%>
<%= f.text_field :title %>
<%= f.label :author, "Author:" %>
<%= f.text_field :author %>
<%= f.label :no_of_items, "No. of Items:" %>
<%= f.text_field :no_of_items %>
<%= f.label :genre, "Genre:" %>
<%= f.text_field :genre %>
<%= f.submit "Add book" %>
<% end %>
</div>
</div>
first you don't align your input fields correctly, i see some margin-left (for the first filed is 60px, for the second is 45px ...) so i suggest you to remove this css margin-left for your input and add this to your label:
label {
width:150px; /*Or however much space you need for the form’s labels*/
float:left;
}
this is a quick tutorial about how to align your fields
then after you submit your form with empty fields, rails will trigger some errors, and wrap each field (also label) which has error with div class="field_with_errors like this: <div class="field_with_errors"><input type="text"..... /></div> : this div will return your field to the next line so if you would that your fields still inline you just add this css to your css file :
.field_with_errors{
display: inline;
}
I found out through this thread how to fix this problem.