Redirecting submit button to the same page - ruby-on-rails

could someone show me a way of making my button redirect to the same page that the submission took place. Here is what my form looks like:
<%= form_for Event.new do |f| %>
<%= f.text_field :partycode %>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
Thanks in advance :)

This is a weird question, but I'm assuming you want the same view (i.e. page).
What you want to do is render the form again.
This is accomplished by overriding which view is rendered by your controller action.
def new # renders new.(format) by default
#event = Event.new
end
def create
#event = Event.new(event_params)
if #event.save
# ...
else
# ...
end
render 'new' # overrides default behaviour
end
Then assuming you have the following views:
# app/views/events/_form.html.erb
<%= form_for #event do |f| %>
<%= f.text_field :partycode %>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
# app/views/events/new.html.erb
<%= render 'form' %>

Related

How can I make follow form work with a list

I have a relationship model that follow users back n forth
now it works very well when I use it in users#show in my view just like this screenshot
now this works good but what I have is a list of users.. and with that list I want to render similar type of follow form next to every item in list
here is the screenshot for reference
but of course this doesn't work because in my relationship controller I am finding user/amitian to follow with their id in params[:id] as in show page i have it but in my list page i don't
here is the code in controller for reference
def create
#amitian = Amitian.find(params[:followed_id])
current_amitian.follow(#amitian)
respond_to do |format|
format.html { redirect_to #amitian }
format.js
end
end
def destroy
#amitian = Relationship.find(params[:id]).followed
current_amitian.unfollow(#amitian)
respond_to do |format|
format.html { redirect_to #amitian }
format.js
end
end
is there any solution for this so that I can render a follow_form in my listpage
it doesn't matter even if i have to render a new follow_form.. it just i want follow mechanism to work even from list page rather than only from show page of every user.
here is the show form in my show page..
show_form
<%if amitian_signed_in?%>
<% if #amitian != current_amitian %>
<div id="follow_form">
<% if current_amitian.following?(#amitian) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
<%end%>
follow_form
<%if amitian_signed_in?%>
<%= form_for(current_amitian.active_relationships.build , remote: true ) do |f| %>
<div><%= hidden_field_tag :followed_id, #amitian.id %></div>
<%= f.submit "Follow", class: "btn btn-primary form-control" %>
<% end %>
<%end%>
unfollow form
<%if amitian_signed_in?%>
<%= form_for(current_amitian.active_relationships.find_by(followed_id: #amitian.id), remote: true,
html: { method: :delete }) do |f| %>
<%= f.submit "Unfollow", class: "btn btn-danger form-control" %>
<% end %>
<%end %>

Creating a rails form to build comments in a second part of the application (modal)

New question at the bottom.
Photos are shown at example.com/photos/:id
User photos are paginated at example.com/users/2
I want to create a form that can be displayed at example.com/users/2 to build comments. I can currently build comments at example.com/photos/:id. However, the instance variable at example.com/users/2 is different than example.com/photos/:id and will not work with CommentsController > def create
PhotosController
def show
#photo = Photo.find(params[:id])
end
CommentsController
def create
#photo = Photo.find(params[:photo_id])
#comment = #photo.comments.build(comment_params)
#comment.save
redirect_to photo_path(#photo)
end
UsersController
def show
#user = User.find(params[:id])
#photos = #user.photos.order('created_at desc').paginate(page: params[:page], per_page: 12)
end
Each users photos are paginated in an instagram like way, displaying a thumb in rows that is clicked to display a modal.
Users > show.html.erb
.
.
<% #photos.in_groups_of(3, false).each do |group| %>
<% group.each do |photo| %>
<a data-toggle="modal" href=<%="#"+"#{photo.id}"%>>
<div class="col-xs-4">
<div class="thumb">
<%= image_tag(photo.picture.thumb.url, class: "img-responsive") %>
.
.
.
<%= form_for([photo.id, photo.id.comments.build]) do |f| %>
<%= f.label :content %><br>
<%= f.text_area :content %>
<%= f.submit %>
<% end %></s>
The form above does not work, Heroku error message:
ActionView::Template::Error (undefined method `comments' for 23:Fixnum):
23 is the :id of my last(first) photo.
Edit: This was answered below, to be changed to:
<%= form_for([photo, Comment.new]) do |f| %>
<%= f.label :content %><br>
<%= f.text_area :content %>
<%= f.submit %>
<% end %>
I also changed CommentsController from redirect_to photo_path(#photo) to redirect_to :back however this is closing my modal where the form is located. What code do I need after #comment.save to keep the modal open with the new comment visible?
Change the form to
<%= form_for([photo, Comment.new]) do |f| %>

Rails - 2 forms one model

I'm struggling with this one, basically I want two forms to submit to the one model. When the first form is complete, the second one is rendered which will contain the remaining information which the model requires. Ideally I would like to combine the two returned hashes and save them together at once. If there is a more sensible way of doing this please let me know. Thanks in advance.
app/controllers/books_controller.rb
class BooksController < ApplicationController
def new
#book = #library.books.build
#user = current_user
end
def create
#book = #library.books.build(params[:book])
if #book.total_pages.nil?
#book_first_page = #book
render 'new'
else
#book.update_attributes(#book_first_page)
if #book.save
redirect_to root_path
else
render 'new'
end
end
end
end
app/views/books/new.html.erb
<% if #book.title.nil? %>
<%= form_for [:library, #book] do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group input-group-sm">
<%= f.label :title, 'What is the title of the book?' %>
<%= f.text_area :title, class: "form-control" %>
</div>
<%= f.submit "Submit Title", class: "btn btn-large btn-primary" %>
<% end %>
<% else %>
<%= form_for [:library, #book] do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group input-group-sm">
<%= f.label :total_pages, "How many pages?:" %>
<%= f.text_field :total_pages, class: "form-control" %>
</div>
<%= f.submit "Submit Number of Pages", class: "btn btn-large btn-primary" %>
<% end %>
You might also use a single form for all your data, but initially showing only the first portion of the form, and then employ javascript to hide the first part and show the second part when the first part is complete.
You should consider using accepts_nested_attributes_for
For example, see http://railscasts.com/episodes/196-nested-model-form-revised
why you need to update in two form. use just one form

Multiple instances of one form_for on a single page? Rails

Thank you in advance,
I have a simple Document model and form_for to create a new document.
All I would like to do is have multiple - say 3 - forms on one page which will create three separate model instances. Either seperate 'submit' buttons or one main one
new.html.erb:
<% 3.times do %>
<%= render 'documents/new_document_form' %>
<% end %>
_new_document_form.html.erb
<%= form_for #document do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.file_field :document_file %>
<%= f.submit "Save Changes", class: "btn btn-large btn-primary" %>
<% end %>
documents_controller.rb
def new
#document = current_account.documents.build if signed_in?
end
def create
#document = current_account.documents.build(params[:document])
if #document.save
flash[:success] = "Successful document upload!"
redirect_to documents_path
else
flash[:error] = "Please try again"
render 'new'
end
end
I could use some help understanding:
how to create multiple unique instances of the forms
pass the input successfully to the controller
any more info!

Rails partial not rendering through ajax call

I am trying to render a partial through ajax when a user clicks on button destroying a relationship between two products. The destroy action works, but I can not get the rendering of the partial to work.
The partial is rendered on the edit page for a product:
edit.html.erb
<div id="relationship_form">
<%= render "relationships", product: #product %>
</div>
_relationships.html.erb
<%= render "unrelate", relationship: relationship %>
_unrelate.html.erb
<%= form_for(relationship,html: { method: :delete },remote: true) do |f| %>
<%= f.submit "Remove", class: "btn btn-small" %>
<% end %>
relationships_controller.html.erb
def destroy
...
respond_to do |format|
format.html { redirect_to edit_product_path }
format.js
end
end
destroy.js.erb
$("#relationship_form").html("<%= escape_javascript(render :partial => 'products/relationships', :locals => {:product => #product}) %>");
I have checked that the ajax call goes through to the destroy.js.erb, by inserting an alert, so there must be an error in the javascript?
Update:
I got it to work by moving the form in the _unrelate partial to the _relationship partial. Still not really clear why it made it work.
_relationships.html.erb
<%= form_for(relationship,html: { method: :delete },remote: true) do |f| %>
<%= f.submit "Remove", class: "btn btn-small" %>
<% end %>

Resources