How to set up Edit for Remote Form in Rails? - ruby-on-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.

Related

Nothing happens when submitting the form

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

Missing Parameters in Ruby on Rails

The Seller Profile has one seller. I am trying to post the seller profile details and assigning the seller_id to the current_seller. I am however, running into this error. I don't understand why the error says missing parameter because it seems all the needed params have being provided.
Error is get is ActionController::ParameterMissing (param is missing or the value is empty: seller_profiles
def create
#seller_profile = SellerProfile.new(seller_profile_params)
#seller_profile.seller = current_seller
respond_to do |format|
if #seller_profile.save
format.html { redirect_to root_path, notice: 'Seller profile was successfully created.' }
def seller_profile_params
params.require(:seller_profile).permit(:first_name, :other_name, :last_name, :email)
end
<%= form_tag seller_seller_profiles_path do |form| %>
<% if seller_profile.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(seller_profile.errors.count, "error") %> prohibited this seller_profile from being saved:</h2>
<ul>
<% seller_profile.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= label_tag :first_name %>
<%= text_field_tag :first_name %>
</div>
<div class="field">
<%= label_tag :other_name %>
<%= text_field_tag :other_name %>
</div>
<div class="field">
<%= label_tag :last_name %>
<%= text_field_tag :last_name %>
</div>
<div class="field">
<%= label_tag :email %>
<%= text_field_tag :email %>
</div>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
resources :sellers, only: [:new, :create, :show, :index, :destroy] do
resources :seller_profiles
end
You should use the form_for or the form_with helpers instead of form_tag. Those helper methods will take care of adding the wrapping seller_profile key.
<%= form_for seller_profile do |form| %>
<% if seller_profile.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(seller_profile.errors.count, "error") %> prohibited this seller_profile from being saved:</h2>
<ul>
<% seller_profile.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :first_name %>
<%= form.text_field :first_name %>
</div>
<div class="field">
<%= form.label :other_name %>
<%= form.text_field :other_name %>
</div>
... replicate the same change for the rest of the fields ...
<div class="actions">
<%= form.submit %>
</div>
<% end %>
If the error is (param is missing or the value is empty: seller_profiles, it's because you require :seller_profile, not :seller_profiles in your params.require

(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

Weird redirection when updating datas

I generated a scaffold for a portfolio via the Rails command, rails g portfolio titre:string, thumbnail:string lien:string description:text. I also added FriendlyId to get a better URL, and that's about all. Here is the 'update' action.
def update
if #portfolio.update(portfolio_params)
redirect_to #portfolio, notice: 'Portfolio mis à jour.'
else
render :edit
end
end
However, when trying to update a project in my portfolio, the submit button tries to get to 'portfolio#update' via patch, but puts a '.' instead of a '/' which gives me No route matches [PATCH] "/portfolios.test-1"
For the route, I only have resources :portfolios
edit : added the form
<%= form_for #portfolio, url: portfolios_path(#portfolio) do |f| %>
<% if portfolio.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(portfolio.errors.count, "error") %> prohibited this portfolio from being saved:</h2>
<ul>
<% portfolio.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :titre %>
<%= f.text_field :titre %>
</div>
<div class="field">
<%= f.label :categorie %>
<%= f.text_field :categorie %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :thumbnail %>
<%= f.file_field :thumbnail %>
</div>
<div class="field">
<%= f.label :lien %>
<%= f.text_field :lien %>
</div>
<div class="actions">
<%= f.submit 'Enregistrer' %>
</div>
<% end %>
I didn't have any other possibilities that doing #portfolio, url: portfolios_path(#portfolio), otherwise Rails considered that '#portfolio' was nil
edit 2 : added the private 'set_portfolio' params
private
# Use callbacks to share common setup or constraints between actions.
def set_portfolio
#portfolio = Portfolio.friendly.find(params[:id])
end
What's wrong with my app ?
portfolios_path is the collection path.
change it portfolio_path should fix it.
<%= form_for #portfolio, url: portfolio_path(#portfolio) do |f| %>
I guess you are going to use this form for creating new portfolio as well, so change it to <%= form_for(#portfolio) %> should fix it and will also work for both cases. form_for will submit to correct path.

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 :)

Resources