How to remove respond_to block from scaffold_controller template - ruby-on-rails

how to customize scaffold generator #was following this link
class IdeasController < ApplicationController
before_action :set_idea, only: [:show, :edit, :update, :destroy]
# GET /ideas
# GET /ideas.json
def index
#ideas = Idea.all
end
# GET /ideas/1
# GET /ideas/1.json
def show
end
# GET /ideas/new
def new
#idea = Idea.new
end
# GET /ideas/1/edit
def edit
end
# POST /ideas
# POST /ideas.json
def create
#idea = Idea.new(idea_params)
respond_to do |format|
if #idea.save
format.html { redirect_to #idea, notice: 'Idea was successfully created.' }
format.json { render action: 'show', status: :created, location: #idea }
else
format.html { render action: 'new' }
format.json { render json: #idea.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /ideas/1
# PATCH/PUT /ideas/1.json
def update
respond_to do |format|
if #idea.update(idea_params)
format.html { redirect_to #idea, notice: 'Idea was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #idea.errors, status: :unprocessable_entity }
end
end
end
# DELETE /ideas/1
# DELETE /ideas/1.json
def destroy
#idea.destroy
respond_to do |format|
format.html { redirect_to ideas_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_idea
#idea = Idea.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def idea_params
params.require(:idea).permit(:name, :description, :picture)
end
end
How to remove all the respond_to code ?

Use respond_with to make your controllers cleaner. This apidoc and
this screencast will answer all your related questions.
Your controller methods will be as clean as this:
def update
#idea.update(idea_params)
respond_with #idea, notice: 'Idea was successfully updated.'
end
To apply that to default scaffold controller template, just copy the template content from github and put it into RAILS_ROOT/lib/templates/rails/scaffold_controller/controller.rb. Then apply the respond_with approach there.

Just do so like this.
For e.g.
respond_to do |format|
if #idea.save
format.html { redirect_to #idea, notice: 'Idea was successfully created.' }
format.json { render action: 'show', status: :created, location: #idea }
else
format.html { render action: 'new' }
format.json { render json: #idea.errors, status: :unprocessable_entity }
end
end
can be replaced with
if #idea.save
redirect_to #idea, notice: 'Idea was successfully created.'
else
render 'new
end

Related

Rails, update the product right after create one

I have the web app on RoR, but the issue is once the user upload the image, the product_id is not associated with the product_attachments. Not until I proceed to next form.
Following were my controller
ProductAttachmentsController:
class ProductAttachmentsController < ApplicationController
before_action :set_product_attachment, only: [:show, :edit, :update, :destroy]
def index
#product_attachments = ProductAttachment.all
end
def show
end
def new
#product_attachment = ProductAttachment.new
end
def create
#product_attachment = ProductAttachment.new(product_attachment_params)
respond_to do |format|
if #product_attachment.save
format.html { redirect_to #product_attachment, notice: 'Product attachment was successfully created.' }
format.json {render :json => #product_attachment}
else
format.html { render :new }
format.json { render json: #product_attachment.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #product_attachment.update(product_attachment_params)
format.html { redirect_to #product_attachment.product, notice: 'Product attachment was successfully updated.' }
format.json { render :show, status: :ok, location: #product_attachment }
else
format.html { render :edit }
format.json { render json: #product_attachment.errors, status: :unprocessable_entity }
end
end
end
def destroy
#product_attachment.destroy
respond_to do |format|
format.html { redirect_to product_attachments_url, notice: 'Product attachment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product_attachment
#product_attachment = ProductAttachment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_attachment_params
params.require(:product_attachment).permit(:product_id, :attachment)
end
end
How can I trick the create method, so it will create a product_id when I create product_attachment? Currently I need to proceed next step and it trigger update method to insert product_id in product_attachments table. Thanks!!
You can store the product attachment id in a session variable
if #product_attachment.save
session[:product_attachment] = #product_attachment.id ### HERE!
format.html { redirect_to #product_attachment, notice: 'Product attachment was successfully created.' }
format.json {render :json => #product_attachment}
else
format.html { render :new }
format.json { render json: #product_attachment.errors, status: :unprocessable_entity }
end
When you create the product (in the ProductsController ) recall the attachment and update the product_id
if #product.save
if session[:product_attachment]
ProductAttachment.find(session[:product_attachment]).update_attribute(:product_id, #product.id)
session[:product_attachment] = nil
end
...
end

PDF upload Ruby on Rails server - DB Postgres

Hello I am trying to store a PDF document in a PostgreSQL database using Ruby Rails.
Currently my code looks like this:
DB File:
'$20151126021922_create_pdf_creates.rb'
class CreatePdfCreates < ActiveRecord::Migration
def change
create_table :pdf_creates do |t|
t.binary :pdfload
t.timestamps null: false
end
end
end
Model:
'$pdf_create.rb'
class PdfCreate < ActiveRecord::Base
end
Controller:
'$pdf_creates_controller.rb'
class PdfCreatesController < ApplicationController
before_action :set_pdf_create, only: [:show, :edit, :update, :destroy]
# GET /pdf_creates
# GET /pdf_creates.json
def index
#pdf_creates = PdfCreate.all
end
# GET /pdf_creates/1
# GET /pdf_creates/1.json
def show
end
# GET /pdf_creates/new
def new
#pdf_create = PdfCreate.new
end
# GET /pdf_creates/1/edit
def edit
end
# POST /pdf_creates
# POST /pdf_creates.json
def newpdf
#pdf_create = PdfCreate.new(pdf_create_params)
respond_to do |format|
if #pdf_create.save
format.html { redirect_to #pdf_create, notice: 'Pdf create was successfully created.' }
format.json { render :show, status: :created, location: #pdf_create }
else
format.html { render :new }
format.json { render json: #pdf_create.errors, status: :unprocessable_entity }
end
end
end
def create
#pdf_create = PdfCreate.new(pdf_create_params)
#data = File.read(Rails.root + "tmp/consent(1).pdf")
#Document.create pdfload: data
respond_to do |format|
if #pdf_create.save
format.html { redirect_to #pdf_create, notice: 'Pdf create was successfully created.' }
format.json { render :show, status: :created, location: #pdf_create }
format.pdf { send_data #pdf_create.render}
else
format.html { render :new }
# format.json { render json: #pdf_create.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /pdf_creates/1
# PATCH/PUT /pdf_creates/1.json
def update
respond_to do |format|
if #pdf_create.update(pdf_create_params)
format.html { redirect_to #pdf_create, notice: 'Pdf create was successfully updated.' }
format.json { render :show, status: :ok, location: #pdf_create }
else
format.html { render :edit }
format.json { render json: #pdf_create.errors, status: :unprocessable_entity }
end
end
end
# DELETE /pdf_creates/1
# DELETE /pdf_creates/1.json
def destroy
#pdf_create.destroy
respond_to do |format|
format.html { redirect_to pdf_creates_url, notice: 'Pdf create was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pdf_create
#pdf_create = PdfCreate.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def pdf_create_params
params.require(:pdf_create).permit(:pdfload)
end
end
I send a pdf form and the result that is returned is me:
{"id":5,"pdfload":null,"created_at":"2015-11-26T03:24:37.457Z","updated_at":"2015-11-26T03:24:37.457Z"}
What is wrong? Tks
You're sending the whole newly created record, not the PDF data stored in it.
Try changing this:
format.pdf { send_data #pdf_create.render}
to this:
format.pdf { send_data #pdf_create.pdfload}

Why Doesn't Rails Scaffold Include Json Rendering for Edit in Controller?

This is just out of curiosity, here's a generated controller from running rails g scaffold Thing:
class ThingsController < ApplicationController
# GET /things
# GET /things.json
def index
#things = Thing.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #things }
end
end
# GET /things/1
# GET /things/1.json
def show
#thing = Thing.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #thing }
end
end
# GET /things/new
# GET /things/new.json
def new
#thing = Thing.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #thing }
end
end
# GET /things/1/edit
def edit
#thing = Thing.find(params[:id])
end
# POST /things
# POST /things.json
def create
#thing = Thing.new(params[:thing])
respond_to do |format|
if #thing.save
format.html { redirect_to #thing, notice: 'Thing was successfully created.' }
format.json { render json: #thing, status: :created, location: #thing }
else
format.html { render action: "new" }
format.json { render json: #thing.errors, status: :unprocessable_entity }
end
end
end
# PUT /things/1
# PUT /things/1.json
def update
#thing = Thing.find(params[:id])
respond_to do |format|
if #thing.update_attributes(params[:thing])
format.html { redirect_to #thing, notice: 'Thing was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #thing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /things/1
# DELETE /things/1.json
def destroy
#thing = Thing.find(params[:id])
#thing.destroy
respond_to do |format|
format.html { redirect_to things_url }
format.json { head :no_content }
end
end
end
Rails includes a format block in every action except for edit... Why is this? Theoretically another app pinging the server for json would still want to show whatever is being edited, right? It's easy enough to just add in, but I am curious why they chose to do it this way.
If you want to know what you are updating, you can do it via the show action.

Rails before_filter doesn't hit database

In my application, I want to only allow user with admin privilege to access this model. So I set up and before_filter to check if the user is an Admin. The problem with this approach is that, after the admin user passes the filter, s/he won't be able to get redirect to the action. Instead, only the views are rendered, which leads to the undefined method each' for nil:NilClass error. What am I doing wrong here?
class TidbitsController < ApplicationController
before_filter :is_admin?
layout "tidbits"
# GET /tidbits
# GET /tidbits.json
protected
def is_admin?
unless current_user.admin?
flash[:error] = "You are not authorized!"
redirect_to "/" and return
end
end
def index
#tidbits = Tidbit.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #tidbits }
end
end
# GET /tidbits/1
# GET /tidbits/1.json
def show
#tidbit = Tidbit.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #tidbit }
end
end
# GET /tidbits/new
# GET /tidbits/new.json
def new
#tidbit = Tidbit.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #tidbit }
end
end
# GET /tidbits/1/edit
def edit
#tidbit = Tidbit.find(params[:id])
end
# POST /tidbits
# POST /tidbits.json
def create
#tidbit = Tidbit.new(params[:tidbit])
respond_to do |format|
if #tidbit.save
format.html { redirect_to #tidbit, notice: 'Tidbit was successfully created.' }
format.json { render json: #tidbit, status: :created, location: #tidbit }
else
format.html { render action: "new" }
format.json { render json: #tidbit.errors, status: :unprocessable_entity }
end
end
end
# PUT /tidbits/1
# PUT /tidbits/1.json
def update
#tidbit = Tidbit.find(params[:id])
respond_to do |format|
if #tidbit.update_attributes(params[:tidbit])
format.html { redirect_to #tidbit, notice: 'Tidbit was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #tidbit.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tidbits/1
# DELETE /tidbits/1.json
def destroy
#tidbit = Tidbit.find(params[:id])
#tidbit.destroy
respond_to do |format|
format.html { redirect_to tidbits_url }
format.json { head :no_content }
end
end
end
in your example all your action methods are protected so maybe that's the problem?
I think you forgot to add the devise required callback filter
before_filter :authenticate_user!
before_filter :is_admin?

Rails has_many :through creating new and linking in controller

I have a website I am making that tracks a users companies through employments. I need to know what I am doing wrong because when I make a new user company the user doesn't know about it.
companies_controller.rb
class CompaniesController < ApplicationController
# GET /companies
# GET /companies.json
def index
#companies = current_user.companies
respond_to do |format|
format.html # index.html.erb
format.json { render json: #companies }
end
end
# GET /companies/1
# GET /companies/1.json
def show
#company = Company.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #company }
end
end
# GET /companies/new
# GET /companies/new.json
def new
#company = Company.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #company }
end
end
# GET /companies/1/edit
def edit
#company = Company.find(params[:id])
end
# POST /companies
# POST /companies.json
def create
#company = Company.new(params[:company])
current_user.employments.create!(company_id: #company.id)
respond_to do |format|
if #company.save
format.html { redirect_to #company, notice: 'Company was successfully created.' }
format.json { render json: #company, status: :created, location: #company }
else
format.html { render action: "new" }
format.json { render json: #company.errors, status: :unprocessable_entity }
end
end
end
# PUT /companies/1
# PUT /companies/1.json
def update
#company = Company.find(params[:id])
respond_to do |format|
if #company.update_attributes(params[:company])
format.html { redirect_to #company, notice: 'Company was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #company.errors, status: :unprocessable_entity }
end
end
end
# DELETE /companies/1
# DELETE /companies/1.json
def destroy
#company = Company.find(params[:id])
#company.destroy
respond_to do |format|
format.html { redirect_to companies_url }
format.json { head :no_content }
end
end
end
The problem is within your create action, specifically the line
current_user.employments.create!(company_id: #company.id)
this is executed before the company record is saved so it doesn't have an id (== nil). Just move that line after
if #company.save
and it should attach it to the current_user via the :through relationship.

Resources