Wicked gem can't pass place_id - ruby-on-rails

I am using wicked gem in order to make a multi step form. Although I am getting the error Couldn't find Place without an ID when I am trying to hit the continue button in the general step (first step of wicked). My places_controller code is
def new
#place = Place.new
respond_to do |format|
format.html
format.json { render json: #place }
end
end
def create
#place = Place.new(params[:place])
respond_to do |format|
if #place.save
session[:place_id]=#place.id
format.html { redirect_to place_steps_path :place_id => #place.id }
format.json { render json: #place, status: :created, location: #place }
else
format.html { render action: "new" }
format.json { render json: #place.errors, status: :unprocessable_entity }
end
end
end
My place_steps_controller has
class PlaceStepsController < ApplicationController
include Wicked::Wizard
steps :general, :explicit
def show
#place = Place.find(params[:place_id])
render_wizard
end
def update
#place = Place.find(params[:place_id])
#place.update_attributes (params[:place])
render_wizard #place
end
end
What am I doing wrong?

Related

Saving state of object if changes are made - Rails

Rails 7
I have the following:
controllers/orders_controller.rb
class OrdersController < ApplicationController
before_action :set_order, only: %i[ show edit update destroy ]
def index
#orders = Order.all
end
def show
end
def new
#order = Order.new
end
def edit
end
def create
#order = Order.new(order_params)
respond_to do |format|
if #order.save
format.html { redirect_to order_url(#order), notice: "Order was successfully created." }
format.json { render :show, status: :created, location: #order }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #order.update(order_params)
format.html { redirect_to order_url(#order), notice: "Order was successfully updated." }
format.json { render :show, status: :ok, location: #order }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
def destroy
#order.destroy
respond_to do |format|
format.html { redirect_to orders_url, notice: "Order was successfully destroyed." }
format.json { head :no_content }
end
end
private
def set_order
#order = Order.find(params[:id])
end
def order_params
params.require(:order).permit(:country, :sku, :abbreviation, :description)
end
end
models/order.rb
class Order < ApplicationRecord
end
What I would like to do, is create an OrderHistory Model, with the following fields:
order_id (int)
order_history (JSON)
When an update is made to the order, I would like the DB entry for the original order, saved as a JSON to OrderHistory.
What I am having trouble with, is creating a variable that contains the state of the existing order, when I enter the controller, without making it available to all app users. Any ideas?

Not able to update data in database form

I am getting error - undefined method collect for nil:NilClass, but I am able to render option list from another database table, and also able to save data in stage table but not able to update it.
I am rendering option list form responsibility table in stage form field responsibility option and saves that option into stage table.
stages_controller.rb
def index
redirect_to project_path(#project)
end
def show
end
def new
#stage = Stage.new
#responsibilities = #project.responsibilities
end
def edit
end
def create
#responsibilities = #project.responsibilities
#stage = #project.stages.build(stage_params)
respond_to do |format|
if #stage.save
format.html { redirect_to project_path(#project), notice: 'Stage was successfully created.' }
format.json { render :show, status: :created, location: #stage }
else
format.html { render :new }
format.json { render json: #stage.errors, status: :unprocessable_entity }
end
end
end
def update
#responsibilities = #project.responsibilities
respond_to do |format|
if #stage.update(stage_params)
format.html { redirect_to project_stages_url, notice: 'Stage was successfully updated.' }
format.json { render :show, status: :ok, location: #stage }
else
format.html { render :edit }
format.json { render json: #stage.errors, status: :unprocessable_entity }
end
end
end
def destroy
#stage.destroy
respond_to do |format|
format.html { redirect_to project_stages_url, notice: 'Stage was successfully deleted.' }
format.json { head :no_content }
end
end
private
def set_stage
#stage = Stage.find(params[:id])
end
def find_project
#project = Project.find(params[:project_id])
end
your edit method is empty, so #responsibility has no content (null), you can put some code for example (from your other method)
def edit
#project = Project.find(params[:id])
#responsibilities = #project.responsibilities
...
end
The issue here is that #responsibilities is not defined in your partial.
You should pass the local variable to the partial like this -
<%= render partial: "form", locals: {responsibilities: # responsibilities} %>
and then you can use responsibilities inside the form partial
More about passing variable to partials

How to pass id from create action to another action?

Ok I have two methods
def join_group
#user = User.find(current_user[:id])
#group = Group.find(params[:id])
respond_to do |format|
if #user.groups << #group
format.html {redirect_to #group}
format.js
else
format.html { render :index}
end
end
end
def create
#group = Group.new(group_params)
#group.created_by = current_user.id
respond_to do |format|
if #group.save
format.html { redirect_to #group }
format.js
format.json { render :show, status: :created, location: #group }
else
format.html { render :new }
format.json { render json: #group.errors, status: :unprocessable_entity }
end
end
end
And I want to make that after create action join_group is called. I was trying after_action :join_group, only: [:create] but i don't know how to pass id into join_group and I ended up with ActiveRecord::RecordNotFound (Couldn't find Group with 'id'=):
Instead of a completely separate action to add the user after you create it you could just set it up in the create method.
def create
#adds user to the group and builds it like you have it.
#group = current_user.groups.build(group_params.merge(created_by: current_user.id)
respond_to do |format|
if #group.save
format.html { redirect_to #group }
format.js
format.json { render :show, status: :created, location: #group }
else
format.html { render :new }
format.json { render json: #group.errors, status: :unprocessable_entity }
end
end
end
But if you do want it separate I would add it as a method on the group and just pass the User ID to it.

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