Not able to update data in database form - ruby-on-rails

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

Related

Rails mailer: create object and access the id of the object inside a mailer view template

I'm creating and object inside a controller:
def create
#item = Item.new(item_params)
if #item.save
respond_to do |format|
format.html { redirect_to index_path, notice: "Created"}
format.json { render :'shows/show', status: :created, location: #item }
end
ModelMailer.delay.new_post(#user)
else
format.html { render :new }
format.json { render json: #item.errors, status: :unprocessable_entity }
end
end
end
this is the mailer method:
def new_post(user)
#user = User.find(user.id)
attachments.inline["logo.png"] = File.read("#{Rails.root}/app/assets/images/logo.png")
mail(:to => #follow.email,
:subject => "Created a new post")
end
I would like to add the item.id thats is created to the mailer so I can access it in the email view template. Any ideas on how to implement this?
Just pass it. I don't actually see where you save the record so presumably your create example is incomplete, but once you have that working you can do...
ModelMailer.new_post(#user, #item.id).deliver_later
Then on the mailer
def new_post(user, item_id)
#item_id = item_id
The #item_id will be available in the view.
And to fix your controller create method (which is still wrong), it should be...
def create
#item = Item.new(item_params)
if #item.save
respond_to do |format|
format.html { redirect_to index_path, notice: "Created"}
format.json { render :'shows/show', status: :created, location: #item }
ModelMailer.delay.new_post(#user, #item.id)
end
else
respond_to do |format|
format.html { render :new }
format.json { render json: #item.errors, status: :unprocessable_entity }
end
end
end

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.

Unknown reason why I'm getting a: undefined method `map' for nil:NilClass error

I'm getting an undefined method `map' for nil:NilClass when I attempt to save my forum.
Here is my jobs_controller.rb:
def new
#job_categories = JobCategory.all.map{|c| [ c.title, c.id ] }
#job = Job.new
end
def edit
#job_categories = JobCategory.all.map{|c| [ c.title, c.id ] }
#job = Job.find(params[:id])
end
def create
#job = Job.new(job_params)
respond_to do |format|
if #job.save
format.html { redirect_to #job, notice: 'Job was successfully created.' }
format.json { render :show, status: :created, location: #job }
else
format.html { render :new }
format.json { render json: #job.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #job.update(job_params)
format.html { redirect_to #job, notice: 'Job was successfully
updated.' }
format.json { render :show, status: :ok, location: #job }
else
format.html { render :edit }
format.json { render json: #job.errors, status: :unprocessable_entity }
end
end
end
Here is my jobs/_form.html.erb:
<div class="field-group">
<p>Jobs Category</p>
<%= form.select(:job_category_id, options_for_select(#job_categories)) %>
My jobs_categories has it's own controller and model also.
If you need any other information feel free to ask.
My error is happening when I try to submit my form.
#job_categories variable is missing form create and update actions. It causes errors when form renders after an unsuccesfull save - options_for_select tries to call .map on undeclared variable.

Allow admin to post as user in Rails app

I'm building a rails app where users can log on and see a table of their SAT test scores. Users "has_many" scores and Scores "belongs_to" users. Currently it is set up so that the user can post their own scores. What I want is for an admin to post the scores and the user will just see the table on their show page. The "admin" is just a boolean field in users that I set to true for the admins.
Here is the scores controller:
class ScoresController < ApplicationController
def index
#scores = Score.all
end
def show
#score = Score.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #score }
format.js
end
end
def new
#score = Score.new
end
def create
#score = current_user.scores.new(params[:score])
#user = current_user
respond_to do |format|
if #score.save
format.html { redirect_to #score.user, notice: 'Score was successfully created.' }
format.json { render json: #score, status: :created, location: #score }
else
format.html { render action: 'new' }
format.json { render json: #score.errors, status: :unprocessable_entity }
end
end
end
def update
#score = Score.find(params[:id])
respond_to do |format|
if #score.update(params[:score])
format.html { redirect_to #score.user, notice: 'Score was successfully updated.' }
format.json { render action: 'show', status: :ok, location: #score }
else
format.html { render action: 'edit' }
format.json { render json: #score.errors, status: :unprocessable_entity }
end
end
end
def edit
#score = Score.find(params[:id])
end
def destroy
#score = Score.find(params[:id])
if #score.present?
#score.destroy
end
redirect_to #score.user
end
end
I know I'd have to change the scores controller so that it didn't rely on current_user to create and edit scores. I'm just not sure how to implement that. Let me know if you need more info! Thanks.
First, you'll need to add a select tag in your view to select which user you want to post as:
- if current_user.is_admin?
= f.select :user_id, options_for_select(User.all.map{ |u| [u.username, u.id] })
- else
= f.hidden_field :user_id, value: current_user.id
Then, on the server-side, we will double-check that current_user is an admin to allow the creation of a Score for another User:
def create
#score = Score.new(params[:score])
if current_user.id != #score.user_id # Someone is trying to create a Score for someone else!
#score.errors.add(:user_id, "You shall not create Score for other users than you, you evil hacker!") unless current_user.is_admin?
end
respond_to do |format|
if #score.save
format.html { redirect_to #score.user, notice: 'Score was successfully created.' }
format.json { render json: #score, status: :created, location: #score }
else
format.html { render action: 'new' }
format.json { render json: #score.errors, status: :unprocessable_entity }
end
end
end
I omitted the part #user = current_user because usually current_user is a helper method than can be accessed directly in the views, so instead of using #user in the create view, use current_user instead.

Wicked gem can't pass place_id

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?

Resources