Nothing happens when submitting the form - ruby-on-rails

<div id="main-create-task"
<div>
<div id="newtask-form>"
<%= form_with model: [#categories,#categories.tasks.build] do |form| %>
<% if task.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(task.errors.count, "error") %> prohibited this task from being saved:</h2>
<ul>
<% task.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
<% end %>
</div>
<div id="create-task">
<div>
<h1>New Task</h1>
</div>
<div>
<%= form.label :Task_name %>
<%= form.text_field :taskname%>
</div>
<br>
<div>
<%= form.label :Description %>
<%= form.text_area :taskbody%>
</div>
<br>
<div>
<%= form.label :Time %>
<%= form.text_field :tasktime%>
</div>
<br>
<%=form.submit "Submit" %>
</div>
</div>
<% end %>
</div>
</div>
</div>
This is my task controller .
def new
#categories = current_user.categories.find(params[:category_id])
#task = Task.new
end
def edit
#categories = Category.find(params[:category_id])
#task = Task.find(params[:id])
end
def create
#category = current_user.categories.find(params[:category_id])
#task = #category.tasks.build(task_params)
#task.user_id = current_user.id
if #task.save
redirect_to categories_path
else
redirect_to category_path
end
I run this code and it does not give me any error . When i click submit , nothing happens . no error its just really nothing happen .
I see about its being handle by javascript . I didn't touch anything in Javascript and we dont use javascript for now . Need assistance , presentation will be later

Ruby on Rails comes with JS. By default form_with attaches the data-remote attribute submitting the form via an XMLHTTPRequest in the background if an Unobtrusive JavaScript driver, like rails-ujs, is used. Source.
To disable remote submits use local: true:
<%= form_with model: [#categories,#categories.tasks.build], local: true do |form| %>

Related

Rails errors not showing?

So im learning rails, and following the rails getting started guide (I've actually done the "blog app" on the Udemy rails course, but im making sure I can write it from scratch first before moving on).
Anyways, i've gotten Delete/Create running, but I was adding validation...and while the validation works my errors aren't showing up.
Right now my pages are super simple:
new.html.erb
<% if #user.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved
</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_with scope: :user, url: users_path, local: true do |form| %>
<p>
<%= form.label :username %><br>
<%= form.text_field :username %>
</p>
<p>
<%= form.label :name %><br>
<%= form.text_field :name %>
</p>
<p>
<%= form.label :age %><br>
<%= form.number_field :age %>
</p>
<p><%= form.submit %></p>
<% end %>
users_controller
class UsersController < ApplicationController
def index
#users = User.all
end
def new
#user = User.new
end
def edit
#user = User.find(params[:id])
end
def create
#user = User.new(params.require(:user).permit(:username,:name,:age))
if #user.save
redirect_to users_path
else
render 'new'
end
end
def update
end
def destroy
#user = User.find(params[:id])
#user.destroy
redirect_to users_path
end
end
So the weird thing if I go into my network tab in dev tools I can see this show up in the response tab:
<div id="error_explanation">
<h2>
1 error prohibited this user from being saved
</h2>
<ul>
<li>Username has already been taken</li>
</ul>
</div>
But it doesn't show up in "elements" in Chrome dev tools. I've restarted rails....so Im really not sure why the elements are not showing up. I DO have bootstrap 4.00 beta installed, but not sure why that would matter. This is rails 5.1.4 btw.
This is because you are not getting the same #user in the <% if #user.errors.any? %>
Try this :
<%= form_with scope: :user, url: users_path, local: true do |form| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved
</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= form.label :username %><br>
<%= form.text_field :username %>
</p>
<p>
<%= form.label :name %><br>
<%= form.text_field :name %>
</p>
<p>
<%= form.label :age %><br>
<%= form.number_field :age %>
</p>
<p><%= form.submit %></p>
<% end %>

(Ruby on Rails) Trying To Edit A Post Using Partials But Having Problems

I'm making a bloglike application. I want to make it so that blog posts are editable if clicked.
This is my partial that displays all the posts of a user and makes them clickable
<% if #feed_items.any? %>
<% #feed_items.in_groups_of(3, false).each do |feeds| %>
<div class="row">
<% feeds.each do |feed| %>
<div class="col-md-4">
<ol class="posts">
<%= link_to edit_post_path(feed) do %>
<%= render feed %>
<% end %>
</ol>
</div>
<% end %>
</div>
<% end %>
<% end %>
This is the Edit view that it redirects to when a post is clicked:
<% if logged_in? %>
<div class="row">
<%= render 'shared/post_form' %>
</div>
<% end %>
And this is the '_post_form' partial which is the layout for an editable post:
<%= form_for(#post) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new post..." %>
</div>
<%= f.submit "Post", class: "btn btn-primary" %>
<% end %>
When I click a post to edit it it does redirect however it gives me the error: "First argument in form cannot contain nil or be empty"
Any suggestions on how to resolve this?
In your controller's edit method you have to set #post like,
def edit
#post = Post.find(params[:id])
end

How to set up Edit for Remote Form in Rails?

Webpage View
So in the image button above, I have a button called "Mark Item as Done". Inside that Item table, I have a date: due_date, string: task_title, text: description, and a boolean: done.
Now I put the format.js in the todoitems_controller.rb because I figured that would be the best place to put it because I'm calling their edit link
todolist_todoitems GET /todolists/:todolist_id/todoitems(.:format) todoitems#index
POST /todolists/:todolist_id/todoitems(.:format) todoitems#create
new_todolist_todoitem GET /todolists/:todolist_id/todoitems/new(.:format) todoitems#new
edit_todolist_todoitem GET /todolists/:todolist_id/todoitems/:id/edit(.:format) todoitems#edit
todolist_todoitem GET /todolists/:todolist_id/todoitems/:id(.:format) todoitems#show
PATCH /todolists/:todolist_id/todoitems/:id(.:format) todoitems#update
PUT /todolists/:todolist_id/todoitems/:id(.:format) todoitems#update
DELETE /todolists/:todolist_id/todoitems/:id(.:format) todoitems#destroy
todolists GET /todolists(.:format) todolists#index
POST /todolists(.:format) todolists#create
new_todolist GET /todolists/new(.:format) todolists#new
edit_todolist GET /todolists/:id/edit(.:format) todolists#edit
todolist GET /todolists/:id(.:format) todolists#show
PATCH /todolists/:id(.:format) todolists#update
PUT /todolists/:id(.:format) todolists#update
DELETE /todolists/:id(.:format) todolists#destroy
root GET / todolists#index
Now, should I also be doing something to the def edit in the todoitems_controller.rb as well or just def update? And putting remote: true in the Todolists/_form.html.erb file, is it the best place to put it there or should I put it in Todoitems/_form.html.erb file? And after doing all these, what is the next step?
This is what I've managed to accomplish so far and this is my first time working with remote forms. I'm just struggling to get this started because the lectures are confusing. If there's anything else missing, I'm happy to provide more info about this! Any help would be appreciated!
todolists/show.html.erb - Look at button_to. That's the part I need help
<p>
<% #paginate_items.each do |item| %>
<div class="list">
<form class="oneLine">
<a class="notDue">
<%= item.due_date %>
</a>
<a class="linkResults">
<%= link_to "#{item.task_title}", [#todolist, item], style: "font-weight: bold;" %>
<%= button_to "Mark Item as Done", remote: true %><br/> <br/>
</a>
</form>
<% end %>
todoitems_controller.rb - I already put the render.js in the update.
def update
respond_to do |format|
if #todoitem.update(todoitem_params)
format.html { redirect_to #todolist, notice: 'Item was successfully updated.' }
format.json { render :show, status: :ok, location: #todoitem }
render.js
else
format.html { render :edit }
format.json { render json: #todoitem.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_todoitem
#todoitem = Todoitem.find(params[:id])
end
def set_todolist
#todolist = Todolist.find(params[:todolist_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def todoitem_params
params.require(:todoitem).permit(:due_date, :task_title, :description, :done, :todolist_id)
end
todolists/_form.html.erb - I already put the remote: true
<body class="page">
<%= form_for(#todolist, remote: true) do |f| %>
<% if #todolist.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#todolist.errors.count, "error") %> prohibited this todolist from being saved:</h2>
<ul>
<% #todolist.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :list_name %><br>
<%= f.text_field :list_name %>
</div>
<br/>
<div class="field">
<%= f.label :list_due_date %><br>
<%= f.text_field :list_due_date, class: 'dateSelect' %>
</div>
<br/>
<div class="actions">
<%= f.submit %>
</div>
<br/>
<% end %>
</body>
todoitems/_form.html.erb
<body class="page">
<%= form_for([#todolist, #todoitem], remote: true) do |f| %>
<% if #todoitem.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#todoitem.errors.count, "error") %> prohibited this todoitem from being saved:</h2>
<ul>
<% #todoitem.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :due_date %><br>
<%= f.text_field :due_date, class: 'dateSelect' %>
</div>
<br/>
<div class="field">
<%= f.label :task_title %><br>
<%= f.text_field :task_title %>
</div>
<br/>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description, size: "40x10" %>
</div>
<br/>
<% if !#todoitem.new_record? %>
<div class="field">
<%= f.label :done, 'Task Completed' %>
<%= f.check_box :done %>
</div>
<br/>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<br/>
<% end %>
</body>
You haven't provided all the parameters to button_to to make the form work. This helper method generates a form and you need to provide the path for the form to POST to. Have a look at the docs for the button_to helper to see what you're missing.
<%= button_to "Mark Item as Done", todolist_todoitem_path(#todolist, #todoitem), method: patch, remote_true %>
(I'm not sure what your routes are so I made it up.)
When you're updating a resource you need to provide the method (either patch or put is for updating). You might want to read up on routing as well. Initially, they can be a little confusing, but they are at the heart of an interactive rails application so understanding them is key.

How to redirect_to(:back) two times?

def update
if #note.update_attributes(note_params)
redirect_to :back, notice: "Note was updated."
else
render :edit
end
end
Is there a way to redirect back twice?
Here you go:
This is where the link for editing goes:
<p id="notice"><%= notice %></p>
<% url = "#{request.protocol}#{request.host_with_port}#{request.fullpath}" %>
<%= link_to 'Create New Page and Return Here', edit_page_path(1, :url => Base64.encode64(url) ) %>
<br>
After submit your url will be something like this:
http://localhost:3000/pages/1/edit?url=aHR0cDovL2xvY2FsaG9zdDozMDAwL2R1bW1pZXM%3D%0A
In the edit form:
I called it pages/_form.html.erb, Pass the URL as a hidden params.
<%= form_for(#page) do |f| %>
<% if #page.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#page.errors.count, "error") %> prohibited this page from being saved:</h2>
<ul>
<% #page.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :permalink %><br>
<%= f.text_field :permalink %>
</div>
<%= hidden_field_tag :url, params[:url].to_s %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
In controller that you have update method, in this case pages_controller.rb, simply Base64 it back and redirect the user:
def update
redirection = nil
if params[:url].present?
redirection = Base64.decode64(params[:url].to_s)
end
if #page.update(page_params)
if redirection.present?
path = redirection
else
path = #page
end
redirect_to path, notice: 'All Done.'
else
render :edit
end
end
Now user updates the form and redirected back to the first show or index page or any page that she is coming from.
Hope this help.
PS: You might want to clean it up a bit and pass the url from the controller, and put some checks on it. So you don't define any var at the view level. In the above code I just tried to solve this issue not really a design pattern oriented :)

Passing an ID to controller show method in Rails

I have the following view page in my test_app:
<p id="notice"><%= notice %></p>
<p>
<strong>Box count:</strong>
<%= #job.box_count %>
</p>
<p>
<strong>Install date:</strong>
<%= #job.install_date %>
</p>
<%= link_to "Back", :back %>
I have the following ruby code in my jobs_controller show method:
def show
#job = Job.find(params[:job_id])
end
The parameters being passed through to the view are ("customer_id" and "id" for the job) as follows:
{"customer_id"=>"1", "id"=>"23"}
When I load the view for any job, I get the following error:
Couldn't find Job without an ID
I must have an error in my show method, but I am unsure exactly what I am doing wrong.
Any ideas?
It looks as though the issue is that my form is not saving the data to the table, here is my form:
<%= form_for([#customer, #job]) do |f| %>
<% if #job.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#job.errors.count, "error") %> prohibited this job from being saved:</h2>
<ul>
<% #job.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :box_count %><br>
<%= f.number_field :box_count %>
</div>
<div class="field">
<%= f.label :install_date %><br>
<%= f.text_field :install_date %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Any idea why the data is not being saved?
You should be using :id, not :job_id:
def show
#job = Job.find(params[:id])
end

Resources