Why mi object is creating without submition? - javascript

I came across a problem in my web app.
I would like to make a mvc to allow the user create a push from the post (like a ranking or commments). But actualy, the push is creating automatically when the user show the post. So I want this one is created if the user submit the data pushed.
Can you explain me where I make a mistake ?
My code
Show/post view :
<strong>Title:</strong>
<%= #post.title %>
</p>
<p>
<strong>Image:</strong>
<%= image_tag #post.image.url(:medium) %>
</p>
<p>
<strong>Prix:</strong>
<%= #post.prix %>
</p>
<p>
<strong>Description:</strong>
<%= #post.description %>
</p>
<%- if current_user.admin? %>
<div class="btn-group" role="group" aria-label="...">
<%= link_to 'Modifier', edit_post_path(#post), class: "btn btn-warning"%>
<%= link_to 'Supprimer', #post, method: :delete, data: { confirm: 'Confirmer la suppression du plat ?' }, class: "btn btn-danger" %>
<%end%>
<%=link_to "Home", posts_path, class: "btn btn-info"%>
</div>
<div class= "col-md-6">
<div class="push">
<%= render 'pushs/form' %>
</div>
</div>
Push/form (into the views/posts/show) :
<%= simple_form_for([#post, #post.pushs.build]) do |f| %>
<div class="row">
<div class="col-md-12">
<div class="form-group text-center">
<h4>Combien êtes vous ?</h4>
<%= f.text_field :quantity %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group text-center">
<h4>Ou êtes vous ?</h4>
<%= f.text_field :adress2 %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group text-center">
<%= f.button :submit %>
</div>
</div>
</div>
<% end %>
the Push controller :
class PushsController < ApplicationController
before_action :authenticate_user!
before_action :set_post
before_action :set_push, only: [:edit, :update, :destroy]
def create
#push = #post.pushs.create(push_params)
#push.user_id = current_user.id
if #push.save
flash[:success] = 'Votre commande est enregisté !'
redirect_to root_path
else
flash[:alert] = "Check the push form, something went horribly wrong."
redirect_to root_path
end
end
def update
if #push.update(push_params)
flash[:success] = 'Votre commande est enregisté !'
redirect_to root_path
else
flash[:alert] = "Check the push form, something went horribly wrong."
render 'edit'
end
end
def destroy
#push = #post.pushs.find(params[:id])
#push.destroy
flash[:success] = "Push deleted :("
redirect_to root_path
end
private
def push_params
params.require(:push).permit(:user_id, :quantity, :prix2, :time, :adress2, :latitude2, :longitude2, :taked, :taked_by, :taked_at, :validated)
end
def set_post
#post = Post.find(params[:post_id])
end
def set_push
#post = Post.find(params[:post_id])
#push = #post.pushs.find(params[:id])
end
end
& the post controller :
class PostsController < ApplicationController
before_action :authenticate_user!
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :post_admin, only: [:new, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
def post_admin
if current_user.admin?
else
flash[:alert] = "That post doesn't belong to you!"
redirect_to root_path
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:user_id, :title, :prix, :image, :description, :user_id)
end
end
Thanks, and obviously if you have any suggestions, I am eager to learn
Bye

Related

Is there a way to add new values to a collection_select field in a form in Rails 6?

In my blog app, there are posts and each post can optionally have tags. When the user is on the new or edit post form, they currently have the means to select or deselect tags to associate with the post. There is a many-to-many relationship between the post and tag models and I am using bootstrap, bootstrap_form and bootstrap-select. This all works seemingly very well. The problem is, when the user is assigning tags to their post, these tags currently have to already exist (otherwise they would have to abort their post and go add tags...bad user experience). I am trying to devise a way to give the user the option to create new tags and/or select already-existing tags and apply them to the post, all on the same post form in the collection_select field, all at the same time.
Apparently I'm asking the wrong questions on google...wouldn't this be a common need, already solved?
I am asking for guidance in providing an 'add new tag' functionality to an otherwise functional collection_select field. How best to go about this?
Posts Controller:
class PostsController < ApplicationController
before_action :set_post, only: %i[edit update interim destroy]
# GET /posts
# GET /posts.json
def index
if user_signed_in? && current_user.admin_role
if params[:tag]
#posts = Post.tagged_with(params[:tag]).all.order('updated_at DESC').page params[:page]
else
#posts = Post.all.order('updated_at DESC').page params[:page]
end
else
if params[:tag]
#posts = Post.tagged_with(params[:tag]).where(published: true).order('updated_at DESC').page params[:page]
else
#posts = Post.where(published: true).order('updated_at DESC').page params[:page]
end
end
end
# GET /posts/new
def new
#post = current_user.posts.build
#categories = Category.pluck(:name, :id)
end
# GET /posts/1/edit
def edit
#categories = Category.pluck(:name, :id)
#cat = #post.category_id
end
# POST /posts
# POST /posts.json
def create
#post = current_user.posts.create(post_params)
respond_to do |format|
if #post.save
if params[:interim]
format.html { redirect_to edit_post_path(#post), notice: 'Post was successfully created.' }
format.json { redirect_to edit_post_path(#post), status: :created, location: #post, notice: 'Post was successfully created.' }
elsif params[:complete]
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
end
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
if params[:interim]
format.html { redirect_to edit_post_path(#post), notice: 'Post was successfully updated.' }
format.json { redirect_to edit_post_path(#post), status: :ok, location: #post, notice: 'Post was successfully updated.' }
elsif params[:complete]
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
end
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_post
#post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :content, :user_id, :published, :category_id, :tag_list, :tag, { tag_ids: [] }, :tag_ids,
comment_attributes: [:id, :title, :user_id, :content, :post_id, :parent_id, :ancestry, :commentable, :commentable_id, :commentable_type])
end
end
No tags controller (not needed thus far)
Post Model:
class Post < ApplicationRecord
has_many :taggings
has_many :tags, through: :taggings
has_rich_text :content
include PgSearch::Model
multisearchable :against => [:title, :content]
def self.published(post)
post.published
end
def self.tagged_with(name)
Tag.find_by!(name: name).posts
end
def self.tag_counts
Tag.select('tags.*, count(taggings.tag_id) as count').joins(:taggings).group('taggings.tag_id')
end
def tag_list
tags.map(&:name).join(', ')
end
def tag_list=(names)
self.tags = names.split(',').map do |n|
Tag.where(name: n.strip).first_or_create!
end
end
end
Tag Model:
class Tag < ApplicationRecord
has_many :taggings
has_many :posts, through: :taggings
end
Tagging Model:
class Tagging < ApplicationRecord
belongs_to :tag
belongs_to :post
end
Post Form Partial (the actual new and edit views do nothing but render this form):
<%= bootstrap_form_for #post, local: true, html: { class: 'form-horizontal' } do |f| %>
<%= f.text_field :title %>
<%= f.rich_text_area :content, control_class: 'trix-content-edit' %>
<%= f.collection_select :category_id, Category.all, :id, :name %>
<%= f.form_group :published, label: { text: "Publication Status" } do %>
<%= f.radio_button :published, true, label: "Published" %>
<%= f.radio_button :published, false, label: "Draft" %>
<% end %>
<%= f.collection_select :tag_ids, Tag.order(:name), :id, :name, {label: 'Tags', include_blank: true}, {class: 'selectpicker show-tick', multiple: 'multiple', title: 'Make your selection...', 'data-live-search': 'true', 'data-actions-box': 'true'} %>
<br><br>
<%= f.submit "Save and Resume Editing", name: "interim", class: "btn btn-primary" %>
<%= f.submit "Save and Quit", name: "complete", class: "btn btn-primary" %>
<br><br>
<% end %>
No forms for tag at this time.

Act as Votable for nested Resources

I'm a newbie on rails and have created a listings and reviews app, (yelp type) and I want users to vote on the reviews, I have installed act as votable and configured as below but I seem to be missing something... any ideas why am getting that error?
Below is my routes.rb
Rails.application.routes.draw do
devise_for :users
resources :listings do
resources :reviews, except: [:show, :index, :upvote, :downvote] do
resources :user do
put "upvote", to: "reviews#upvote"
put "downvote", to: "reviews#downvote"
end
end
end
get 'pages/about'
get 'pages/how'
get 'pages/faqs'
get 'pages/contact'
get 'pages/privacy'
get 'pages/tos'
get 'pages/guidelines'
root 'listings#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Here is my show.html.erb, I will turn it into a partial when it works
<div class="row">
<div class="col-md-3">
<%= image_tag #listing.image_url (:medium) %>
<h3><%= #listing.name %></h3>
<div class="star-rating" data-score= <%= #avg_rating %> ></div>
<p class="small"><%= "#{#reviews.length} reviews" %></p>
<address>
<strong>Address:</strong>
<%= #listing.address %><br>
<strong>Phone:</strong>
<%= #listing.phone %><br>
<strong>Email:</strong>
<%= mail_to #listing.email %> <br>
<strong>Website:</strong>
<%= link_to #listing.website, #listing %>
</address>
<hr>
<p>
<strong>About:</strong>
<p><%= h(#listing.description).gsub(/\n/, '<br/>').html_safe %></p>
</p>
</div>
<div class="col-md-9">
<%= link_to "Haiya, Post a Review ", new_listing_review_path(#listing), class: "btn btn-info" %> <br><br>
<table class="table">
<thead>
<tr>
<th class="col-md-3"></th>
<th class="col-md-9"></th>
</tr>
</thead>
<tbody>
<% if #reviews.blank? %>
<tr>
<p>No reviews yet. Be the first to write one!</p>
<% else %>
<% #reviews.each do |review| %>
<td>
<h4> <%= "#{review.user.first_name.capitalize} #{review.user.last_name.capitalize[0]}." %></h4>
<p class = "small"><%= time_ago_in_words(review.created_at) %> ago </p>
</td>
<td>
<div class="star-rating" data-score= <%= review.rating %> ></div>
<p><%= h(review.comment).gsub(/\n/, '<br/>').html_safe %></p>
<%= link_to 'UP', listing_review_user_upvote_path(#listing, review), method: :put %>
<!-- Check if user is signed in, to enable edit and update -->
<% if user_signed_in? %>
<% if (review.user == current_user) || (current_user.admin?) %>
<%= link_to "Edit", edit_listing_review_path(#listing, review), class: "text-left" %>
<%= link_to "Delete", listing_review_path(#listing, review), method: :delete, class: "text-left" %>
<% end %>
<% end %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
</div>
</div>
<% if user_signed_in? && current_user.admin? %>
<%= link_to 'Edit', edit_listing_path(#listing), class: "btn btn-link" %> |
<% end %>
<%= link_to 'Back', listings_path, class: "btn btn-link" %>
<!--The script for star ratings. -->
<script>
$('.star-rating').raty({
path: 'https://s3.eu-west-2.amazonaws.com/bebuwaphotos/uploads/stars',
readOnly: true,
score: function() {
return $(this).attr('data-score');
}
});
</script>
Here is my reviews controller
class ReviewsController < ApplicationController
before_action :authenticate_user!
before_action :set_listing
before_action :set_review, only: [:show, :edit, :update, :destroy, :upvote, :downvote]
before_action :check_user, only: [:edit, :update, :destroy]
# GET /reviews/new
def new
#review = Review.new
end
# GET /reviews/1/edit
def edit
end
#Added the def upvote and downvote
def upvote
#review = Review.find(params[:id])
#review.upvote_from current_user
redirect_to :back
end
def downvote
#review = Review.find(params[:id])
#review.downvote_from current_user
redirect_to :back
end
# POST /reviews
# POST /reviews.json
def create
#review = Review.new(review_params)
#review.user_id = current_user.id
#review.listing_id = #listing.id
respond_to do |format|
if #review.save
format.html { redirect_to #listing, notice: 'Your review was successfully posted.' }
format.json { render :show, status: :created, location: #review }
else
format.html { render :new }
format.json { render json: #review.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reviews/1
# PATCH/PUT /reviews/1.json
def update
respond_to do |format|
if #review.update(review_params)
format.html { redirect_to root_url, notice: 'Your Review was successfully updated.' }
format.json { render :show, status: :ok, location: #review }
else
format.html { render :edit }
format.json { render json: #review.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reviews/1
# DELETE /reviews/1.json
def destroy
#review.destroy
respond_to do |format|
format.html { redirect_to listing_path(#listing), notice: 'Your Review was successfully destroyed :(.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_review
#review = Review.find(params[:id])
end
#Check user
def check_user
unless (#review.user == current_user) || (current_user.admin?)
redirect_to root_url, alert: "Sorry, this review belongs to someone else, you can only edit reviews you have posted."
end
end
#set listing
def set_listing
#listing = Listing.find(params[:listing_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def review_params
params.require(:review).permit(:rating, :comment)
end
end
And my model :
class Review < ApplicationRecord
belongs_to :user
belongs_to :listing
validates :rating, :comment, presence: true
validates :rating, numericality: {
only_integer: true,
greater_than_or_equal_to: 1,
less_than_or_equal_to: 5,
message: "You have to select at least 1 star if not more stars.."
}
acts_as_votable
end
Finally here is my error
Please explain like you would to a newbie.. or a 5 year old .
Thank you Eshan, followed your answer and got the following error.
You are doing well as a newbie.
The first thing you do when you get an error related to routes, you run rails routes in commandline, so you can see your routes in front of you :
Problem:
You are having a problem with listing_review_user_upvote path, which leads to /listings/:listing_id/reviews/:review_id/user/:user_id/upvote(.:format) URI pattern as you see above.
This route needs three params: listing_id, review_id, and user_id, but you call it this way in your view:
listing_review_user_upvote_path(#listing, review).
You don't send user_id at all, meanwhile you don't even need it since you use current_user in your controller's action.
Solution:
I suggest you to update your routes to be:
resources :listings do
resources :reviews, except: [:show, :index] do
put "upvote", to: "reviews#upvote"
put "downvote", to: "reviews#downvote"
resources :user
end
end
And to use it in your view this way, with ids instead of objects :
listing_review_user_upvote_path(#listing.id, review.id)
These two steps will resolve your problem hopefully.
Finally, except option is used with resources to avoid generating some of its CRUD routes like: update, index, edit, you don't need to use it with your custom routes like: upvote and downvote.
Update:
You should use the correct param in your controller, review_id rather than id, change review = Review.find(params[:id]) to review = Review.find(params[:review_id])
I suggest you to use find_by(id: params[:review_id rather than find(params[:review_id because find method raises an exception if it couldn't find the object in db.

Display a nested form

I would like to display my orders like the ingredients are displayed just above. And I don't understand why is not ??
Post : Ingredients nested
Onlines :Orders nested
Views/posts/show:
<p>
<strong>Title:</strong>
<%= #post.title %>
</p>
<p> Posted by : <%= link_to #post.user.pseudo, profile_path(#post.user.pseudo) %>, <%= time_ago_in_words(#post.created_at) %> ago </p>
<p>
<strong>Image:</strong>
<%= image_tag #post.image.url(:medium) %>
</p>
<p>
<strong>Description:</strong>
<%= #post.description %>
</p>
<div class="btn-group" role="group" aria-label="...">
<%= link_to '- Pusher - ', new_post_online_path(#post), data: { confirm: 'Confirmer la mise en ligne de #{#title}?' }, class: "btn btn-primary " %>
<div class="col-md-12">
<h3>Ingrédients :</h3>
<div id="ingredients">
<ul>
<%- #post.ingredients.each do |ingredient| %>
<li>
<%= ingredient.name %>
</li>
<%end%>
</ul>
</div>
</div>
<br>
<div class="col-md-9">
<h3>Parts :</h3>
<div id="ingredients">
<ul>
<%- #post.onlines.orders.each do |order| %>
<li>
<%= order.id %>
</li>
<%end%>
</ul>
</div>
</div>
<%= link_to 'Patcher', edit_post_online_path(#post, #post.onlines.last), class: "btn btn-warning"%>
</div>
post controller:
class PostsController < ApplicationController
before_action :authenticate_user!
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :owned_post, only: [:edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.push_posts
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
#post = current_user.posts.build
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
#post = current_user.posts.build(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:user_id, :title, :description, :image, ingredients_attributes: [:id, :name, :_destroy])
end
def owned_post
unless current_user == #post.user
flash[:alert] = "That post doesn't belong to you!"
redirect_to root_path
end
end
def set_online
#onlines = Online.find_by(params[:id])
end
end
Onlines controller:
class OnlinesController < ApplicationController
before_action :authenticate_user!
before_action :set_post
before_action :owned_online, only: [:new, :update]
before_action :set_online
def new
#online = current_user.onlines.build
#online.post_id = #post.id
#online.user_id = current_user.id
end
def edit
end
def taked
#online.orders.update(taked: false)
end
def create
if Online.where(post_id: params[:post_id]).any?
#online = Online.where(post_id: params[:post_id]).last.update_attributes(push: false)
end
#online = #post.onlines.create(online_params)
if #online.save
if #online.portion <= 0
#online.update(push: false)
flash[:success] = 'Veuillez indiquer le nombre de parts disponibles '
redirect_to root_path
else
#online.update(pushed_at: Time.zone.now)
#online.update(push: true)
flash[:success] = 'Votre post est en ligne !'
redirect_to root_path
end
else
render 'new'
end
end
def update
if #onlines.update(online_params)
if #online.push == false
if #online.portion <= 0
#online.update(push: false)
flash[:success] = 'Veuillez indiquer le nombre de parts disponibles '
redirect_to root_path
else
#online.update(push: true)
flash[:success] = 'Votre post a bien été pushé !'
redirect_to root_path
end
end
else
#user.errors.full_messages
flash[:error] = #user.errors.full_messages
render :edit
end
end
private
def online_params
params.require(:online).permit(:user_id, :post_id, :prix, :portion, :push, :pushed_at, orders_attributes: [:id, :taked, :taked_at, :taked_by, :validated_at, :validated_by, :_destroy])
end
def owned_online
#post = Post.find(params[:post_id])
unless current_user == #post.user
flash[:alert] = "That post doesn't belong to you!"
redirect_to :back
end
end
def set_post
#post = Post.find_by(params[:post_id])
end
def set_online
#post = Post.find(params[:post_id])
#online = Online.find_by(params[:id])
end
end
models
online :
class Online < ActiveRecord::Base
belongs_to :post
belongs_to :user
has_many :orders
accepts_nested_attributes_for :orders, allow_destroy: true
scope :push, ->{ where(push: true).order("pushed_at DESC") }
end
orders :
class Order < ActiveRecord::Base
belongs_to :online
belongs_to :user
end
and post :
class Post < ActiveRecord::Base
belongs_to :user
has_many :onlines, dependent: :destroy
scope :push_posts, lambda { joins(:onlines).merge(Online.push) }
has_many :ingredients, dependent: :destroy
accepts_nested_attributes_for :ingredients, reject_if: :all_blank, allow_destroy: true
has_many :comments
validates :image, presence: true
has_attached_file :image, styles: { medium: "300x300#"}, default_url: "/images/:style/missing.png"
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end
So if you have the solution above, I'll take it !!
Thanks
Your Posts have many Onlines. Each Online has many Orders. So, you should iterate through each order of each online in your post.
<ul>
<%- #post.onlines.each do |online| %>
<%- online.orders.each do |order| %>
<li>
<%= order.id %>
</li>
<%end%>
<%end%>
</ul>

Rails: Capturing error messages in JavaScript

I am using ajax for creating post that belongs to a particular topic where I render the form in post index page. Each post can have many tags and I am also using devise authentication and CanCanCan authorization.
I need to capture the error message of the post submit and show it in the browser through this JavaScript template Create.js.erb with custom error messages instead of doing in post form.
Below is the code:
Post controller
class PostsController < ApplicationController
load_and_authorize_resource
before_action :set_post, only: [:show, :edit, :update, :destroy, :update_status]
skip_before_action :verify_authenticity_token
# GET /posts
# GET /posts.json
def index
if params[:topic_id].present?
#topic = Topic.find(params[:topic_id])
#posts = #topic.posts.paginate(page: params[:page], per_page: 10)
#post = #topic.posts.new
else
#posts = Post.eager_load(:topic, :user).paginate(page: params[:page], per_page: 10)
end
#tags =Tag.all
end
# GET /posts/1
# GET /posts/1.json
def show
#tags = #posts.tags
end
def update_status
current_user.posts<<(#posts)
end
# GET /posts/new
def new
#topic = Topic.find(params[:topic_id])
#posts = #topic.posts.new
#tags =Tag.all
end
# GET /posts/1/edit
def edit
#tags = #posts.tags
end
# POST /posts
# POST /posts.json
def create
#topic = Topic.find(params[:topic_id])
#posts = #topic.posts.create(post_params)
respond_to do |format|
#posts.user_id = current_user.id
if #posts.save
format.html { redirect_to topic_posts_path(#topic), notice: 'Post was successfully created.' }
format.js
format.json { render :show, status: :created, location: #posts }
else
format.html { render :new }
format.json { render json: #posts.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
#tags = #posts.tags
respond_to do |format|
if params[:rate].to_i>0
#posts.ratings.create(:star => params[:rate])
format.html { redirect_to post_path(#posts), notice: 'Rating was successfully updated.' }
elsif #posts.update(post_params)
format.html { redirect_to post_path(#posts), notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #posts }
else
format.html { render :edit }
format.json { render json: #posts.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#posts.destroy
respond_to do |format|
format.html { redirect_to topic_posts_url(#posts.topic_id), notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#posts = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:image, :name, :message, :topic_id, {tag_ids:[]}, :rate, :user_id)
end
protected
def json_request?
request.format.json?
end
end
post form
<%= form_for [#topic, #post], remote: true do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<div class="field">
<%= f.label :Image %><br>
<%= f.file_field :image %>
</div>
<div class="field">
<%= f.label :Name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :Message %><br>
<%= f.text_area :message %>
</div>
<% if #tags %>
<% #tags.each do |tag| %>
<div>
<%= check_box_tag "post[tag_ids][]", tag.id, #post.tags.include?(tag) %>
<%= tag.name %>
</div>
<% end %>
<% end %>
<br><br>
<%= link_to 'Create Tag', tags_path %>
<br><br>
<%= f.submit %>
<% end %>
create.js.erb
$("#post_table").append("<%= j render #posts %>");
alert("Post created")
index.html.erb
<p id="notice"><%= notice %></p>
<div id='ajax_loader' style="position: absolute; left: 50%; top: 50%; display: none;">
<%= image_tag "ajax-loader.gif" %>
</div>
<script>
$(document).ajaxStop(function(){
$("#ajax_loader").hide();
});
$(document).ajaxStart(function(){
$("#ajax_loader").show();
});
</script>
<%= will_paginate %>
<h1>Listing Posts</h1>
<table id = "post_table">
<thead>
<tr>
<th><th>Name</th></th>
<th><th>Author</th></th>
<th><th>Message</th></th>
<th><th>Status</th></th>
<th colspan="4"></th>
</tr>
</thead>
<tbody>
<%= render #posts %>
</tbody>
</table>
<br>
<% if #topic %>
<%= link_to 'New Post', "#", id: "new_post" %>|
<section id = "new_post_section">
<%= render 'form' %>
</section>
<%= link_to 'Back to Topics', topic_path(#topic) %>
<% else %>
<% link_to 'New Post', new_post_path %>
<% end %>
<%= will_paginate %>
Post model
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :topic
has_many :comments
has_and_belongs_to_many :tags
has_many :ratings
validates_presence_of :name, :presence => true
validates_length_of :name, maximum: 5
has_attached_file :image
#validates_attachment_presence :image, :presence => true
validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png']
validates_attachment_size :image, :in => 0..100.kilobytes
end
Please help me.
Modify your the create action in the posts_controller.rb to something like this (notice that format.js was added to the else clause of the respond_to block):
# POST /posts
# POST /posts.json
def create
#topic = Topic.find(params[:topic_id])
#posts = #topic.posts.create(post_params)
respond_to do |format|
#posts.user_id = current_user.id
if #posts.save
format.html { redirect_to topic_posts_path(#topic), notice: 'Post was successfully created.' }
format.js
format.json { render :show, status: :created, location: #posts }
else
format.html { render :new }
format.js # call create.js.erb on save errors
format.json { render json: #posts.errors, status: :unprocessable_entity }
end
end
end
Then, in create.js.erb you can check for errors and handle them however you'd like. Example:
<% if #post.errors.any? %>
alert("ERROR(S): <%= j #post.errors.full_messages.join('; ') %>")
<% else %>
$("#post_table").append("<%= j render #posts %>");
alert("Post created");
<% end %>

Couldn't get popup modal dialog with rails application

I have 2 model Post and Comment, where Post has-many Comments and Comment belongs_to Post.
Every thing working fine with creation of posts and comments for that posts.
Now I have a requirement where when I click on "create new comment" in posts/show page, I want to show the comments/_form in modal.
comments_controller.rb:
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
# GET /comments
# GET /comments.json
def index
#comments = Comment.all
respond_with(#comments)
end
# GET /comments/1
# GET /comments/1.json
def show
respond_with(#comments)
end
# GET /comments/new
def new
#post = Post.find(params[:post_id])
#comment = #post.comments.build
end
# GET /comments/1/edit
def edit
#post = Post.find(params[:post_id])
end
# POST /comments
# POST /comments.json
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create(comment_params)
respond_to do |format|
if #comment.save
format.html { redirect_to(#comment, :notice => 'Article was successfully created.') }
format.xml { render :xml => #comment, :status => :created, :location => #comment }
format.js
else
format.html { render :action => "new" }
format.xml { render :xml => #comment.errors, :status => :unprocessable_entity }
format.js
end
end
end
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
def update
#post = Post.find(params[:post_id])
#comment.update(comment_params)
redirect_to post_path(#post)
end
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
#comment.destroy
respond_to do |format|
format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
#comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:commenter, :description)
end
end
posts/show.html.erb:
<%= link_to 'New Coment', new_post_comment_path(#post), :id => 'create_comment' %>
comments/new.html.erb:
<div id="content">
<h1>New Comment</h1>
<%= render 'form' %>
</div>
comments/_form.html.erb:
<%= form_for([#post, #comment], :remote => true) do |f| %>
<div class="field">
<%= f.label :commenter %><br>
<%= f.text_field :commenter %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
comments/create.js.erb:
<%- if #comment.errors.any? %>
console.log('Error');
$('#dialog-form').html('<%= escape_javascript(render('form')) %>');
<%- else %>
console.log('Created');
$('#dialog-form').dialog('close');
$('#dialog-form').remove();
$('table').append('<%= escape_javascript(render(#comment)) %>');
<%- end %>
I don't know where I went wrong. When I click on "create comment" it is redirecting to new page instead of pop-up model and even couldn't create comment.
Please help.
Create a partial for showing comment form.
Render this partial to post show page as hidden.
show hidden partial.
Example:
<%= link_to 'Show Modal', "#", data: {toggle: "modal", target: "#modal"} %>
hidden partial
<div class="modal hide fade" id="modal" title="My modal">
/* your comment form */
/* render comment form */
</div>
Just make sure target is your hidden partial id.

Categories

Resources