Couldn't find ProjectSession with 'id'= - ruby-on-rails

Here i have a project to which i am adding a session and for a project session i am trying to add task.
i am able to create project and add project session for project but when i am trying to add task for session using project_sessions_id i am getting error Couldn't find ProjectSession with 'id'= and 'app/controllers/tasks_controller.rb:60:in set_project_session i am able to get the project session id also project_sessions/11 in the url but when i click 'create task' i am getting this error. how can i resolve this?
here's what i have done
ProjectSessionController:
class ProjectSessionsController < ApplicationController
before_action :set_project_session, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
before_action :set_project
def index
#project_sessions = ProjectSession.all
end
def show
#project_sessions = ProjectSession.where(project_id: #project.id).order("created_at DESC")
end
def new
#project_session = ProjectSession.new
end
def edit
end
def create
#project_session = ProjectSession.new(project_session_params)
#project_session.project_id = #project.id
#respond_to do |format|
if #project_session.save
redirect_to #project
#format.html { redirect_to #project_session, notice: 'Project session was successfully created.' }
#format.json { render :show, status: :created, location: #project_session }
else
format.html { render :new }
format.json { render json: #project_session.errors, status: :unprocessable_entity }
end
#end
end
def update
respond_to do |format|
if #project_session.update(project_session_params)
format.html { redirect_to #project_session, notice: 'Project session was successfully updated.' }
format.json { render :show, status: :ok, location: #project_session }
else
format.html { render :edit }
format.json { render json: #project_session.errors, status: :unprocessable_entity }
end
end
end
def destroy
#project_session.destroy
respond_to do |format|
format.html { redirect_to project_sessions_url, notice: 'Project session was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project_session
#project_session = ProjectSession.find(params[:id])
end
def set_project
#project = Project.find(params[:project_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_session_params
params.require(:project_session).permit(:session_date, :session_name, :session_description, :start_time, :end_time)
end
end
Task controller:
class TasksController < ApplicationController
before_action :set_task, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
before_action :set_project_session
def index
#tasks = Task.all
end
def show
end
def new
#task = Task.new
end
def edit
end
def create
#task = Task.new(task_params)
#task.session_id = #project_session.id
respond_to do |format|
if #task.save
redirect_to #project_session
else
format.html { render :new }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #task.update(task_params)
format.html { redirect_to #task, notice: 'Task was successfully updated.' }
format.json { render :show, status: :ok, location: #task }
else
format.html { render :edit }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
def destroy
#task.destroy
respond_to do |format|
format.html { redirect_to tasks_url, notice: 'Task was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_task
#task = Task.find(params[:id])
end
def set_project_session
#project_session = ProjectSession.find(params[:project_session_id])
end
def task_params
params.require(:task).permit(:name, :description)
end
end
routes:
Rails.application.routes.draw do
get 'hr_dashboard/index'
resources :roles
resources :project_sessions
devise_for :users
resources :tasks
resources :projects do
resources :project_sessions, except: [:show, :index]
end
resources :project_sessions do
resources :tasks, except: [:show, :index]
end
authenticated :user do
root 'admindashboard#index', as:"authenticated_root"
end
root 'welcome#index'
get 'userdashboard/index'
get 'admindashboard/index'
get 'welcome/index'
end
View for creating new task
<div class="container">
<h1>New Task</h1>
<%= form_for(#task) do |f| %>
<% if #task.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#task.errors.count, "error") %> prohibited this task from being saved:</h2>
<ul>
<% #task.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Back', tasks_path %>
</div>

I figured it out!
I had forgotten to add #project_session in
<%= form_for([#project_session, #task]) do |f| %>
just added that and it worked.

In your create action of Task controller, you have to add:
#task= #project_session.tasks.build(task_params)
Because right now, you're not telling the task, to build from the project_session (or with respect to the project_session) you're just telling it to create a new task.
#task = Task.new
And in the routes.rb file, you've prepared for that to happen by nesting the resources, so it's currently looking for the ID of a task that belongs_to project_session. But can't find any.
And also, in your form_for element when creating a ProjectSssion you have let that form know which route it should belong to - I guess you could say - since you still have the un-nested resources available:
resources :tasks
If project_sessions shouldn't be creatable without a parent, there's no reasons for keeping that, so you should just remove it.
Anyways, here's what the form_for should look like:
<%= form_for([#project_session, #task]) do |f| %>

Related

What does it mean this error First argument in form cannot contain nil or be empty in RoR

I have this error First argument in form cannot contain nil or be empty
In the next code and have no idea how to resolve. I checked other similar questions but no luck.
I also share the controller which seems the issue but still no idea how to fix it
<%= form_for #upload do |f| %>
<% if #upload.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#upload.errors.count, "error") %> prohibited this document from being saved:</h2>
<ul>
<% #upload.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.file_field :file %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Controller:
This what makes the thing in my app. But I don't see the issue.
class UploadsController < ApplicationController
before_action :upload_params, only: [:show, :edit, :update, :destroy]
def index
#uploads = Upload.all
end
def show
send_data(#upload.file_contents,
type: #upload.content_type,
filename: #upload.filename)
end
def new
#upload = Upload.new
end
def edit
end
def create
#upload = Upload.new(upload_params)
respond_to do |format|
if #upload.save
format.html { redirect_to upload_path, notice: 'Document was successfully created.' }
format.json { render :show, status: :created, location: #upload }
else
format.html { render :new, notice: 'Wrong file' }
format.json { render json: #upload.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #upload.update(upload_params)
format.html { redirect_to #upload, notice: 'Document was successfully updated.' }
format.json { render :show, status: :ok, location: #upload }
else
format.html { render :edit }
format.json { render json: #upload.errors, status: :unprocessable_entity }
end
end
end
def destroy
#upload.destroy
respond_to do |format|
format.html { redirect_to uploads_url, notice: 'Document was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_upload
#upload = Upload.find(params[:id])
end
def upload_params
params.require(:upload).permit(:file)
end
end
It's pretty obvious from the error I would say. The #upload object must not be nil. Please make sure you initiate that object before rendering this view (usually in the controller).
Looking at your controller code, please change the before_action to call set_upload.
before_action :set_upload, only: [:show, :edit, :update, :destroy]

Can't submit a Review into a Place - Rails Directory Website app

I am following a Rails tutorial where I have to create a directory website of places in which an user can submit a review.
Right now, here's the thing:
I can register an user and I am able to login. Once I am logged in, I also can create a new place into the database. I also can access the webpage of each place I have created.
I have a problem though. On each place page, I can't submit the review of the place.
All places have an address, a phone number, a website, a description and a map with the location of that place. On that same page I have a form to submit a review, however, once I click on the create review button nothing happens. I can't see the review on the page and when I go to the rails console I can definitely see that there's not a single review created.
Inside my app/views/reviews/_form.html.erb I have this code:
<div class="form-inputs">
<%= f.input :content, required: true %>
<%= f.hidden_field :place_id, required: true, value: #place_id %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Inside my app/views/reviews/_review.html.erb I have this code:
<p>
<%= gravatar_tag review.user.email, size: 20 %><%= review.content %> by <%= review.user.name %>
</p>
Inside my app/views/places/show.html.erb I have this code:
<div class="row">
<div class="col-md-3">
<h3><%= #place.name %></h3>
<p><strong>Adress</strong><%= #place.address %></p>
<p><strong>Phone</strong><%= #place.phone %></p>
<p><strong>Website</strong><%= #place.website %></p>
<p><strong>Description</strong><%= #place.description %></p>
<div id="map-canvas" style = "width:230px; height:230px"></div>
</div>
<div class="col-md-9">
<h3>Reviews by People</h3>
<% if current_user %>
<h5>New Review</h5>
<%= render 'reviews/form' %>
<h5>All Reviews</h5>
<%= render #reviews %>
<% end %>
</div>
</div>
On my places_controller.rb I have:
class PlacesController < ApplicationController
before_action :authenticate_user!, only: [:new, :edit, :create, :update, :destroy]
before_action :set_place, only: [:show, :edit, :update, :destroy]
# GET /places
# GET /places.json
def index
#places = Place.all
end
# GET /places/1
# GET /places/1.json
def show
#reviews = #place.reviews
#review = Review.new
end
# GET /places/new
def new
#place = Place.new
end
# GET /places/1/edit
def edit
end
# POST /places
# POST /places.json
def create
#place = current_user.places.new(place_params)
respond_to do |format|
if #place.save
format.html { redirect_to #place, notice: 'Place was successfully created.' }
format.json { render :show, status: :created, location: #place }
else
format.html { render :new }
format.json { render json: #place.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /places/1
# PATCH/PUT /places/1.json
def update
respond_to do |format|
if #place.update(place_params)
format.html { redirect_to #place, notice: 'Place was successfully updated.' }
format.json { render :show, status: :ok, location: #place }
else
format.html { render :edit }
format.json { render json: #place.errors, status: :unprocessable_entity }
end
end
end
# DELETE /places/1
# DELETE /places/1.json
def destroy
#place.destroy
respond_to do |format|
format.html { redirect_to places_url, notice: 'Place was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_place
#place = Place.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def place_params
params.require(:place).permit(:name, :address, :description, :phone, :website)
end
end
Also, so far... here is my reviews.controller.rb code:
class ReviewsController <ApplicationController
before_action :authenticate_user!
before_action :set_review, only: [:edit, :update, :destroy]
def edit
end
def create
#review = current_user.reviews.new(review_params)
respond_to do |format|
if #review.save
format.html { redirect_to place_path(#review.place) , notice: 'Review was successfully created.' }
else
format.html { render :new }
end
end
end
def update
respond_to do |format|
if #review.update(review_params)
format.html { redirect_to place_path(#review.place), notice: 'Review was successfully updated.' }
else
format.html { render :edit }
end
end
end
def destroy
#review.destroy
respond_to do |format|
format.html { redirect_to place_path(#review.place), notice: 'Review was successfully destroyed.' }
end
end
private
def set_review
#review = Review.find(params[:id])
end
def review_params
params.require(:review).permit(:content, :place_id)
end
end
Any insight of why I can't save reviews of a place?
Your form should your form be like this:-
<div class="form-inputs">
<%= f.input :content, required: true %>
<%= f.hidden_field :place_id, required: true, value: #place.id %>
</div>
You are setting instance variable #place in controller. So you need to use #place.id in your rendered views. You have not set #place_id anywhere

Missing required keys rails

I'm trying to create a form for task in my app.App has two entities:
Project belongs_to User
Task belongs_to Project
But I get this error in my view when I'm trying to create this (pretty basic) form
No route matches {:action=>"index", :controller=>"tasks"} missing required keys: [:project_id]
Here is a part of my view with this form
<div class="glyphicon glyphicon-plus col-xs-1 left_plus" ></div>
<div class="col-xs-10" >
<%= form_for [#project, #task],url: project_tasks_path do |f| %>
<%= f.input :body,class: 'form-control' %>
<%= f.submit 'Add task', class: 'btn' %>
<% end %>
And here is the project controller:
class ProjectsController < ApplicationController
before_action :load_project, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
#projects = current_user.projects unless current_user.nil?
#task = Task.find_by(params[:project_id])
end
def show
#task = #project.tasks.build
end
def new
#project = current_user.projects.new
end
def edit
end
def create
#project = current_user.projects.create(project_params)
if #project.save
redirect_to root_path
else
render :new
end
end
def update
if #project.update(project_params)
redirect_to #project
else
render :edit
end
end
def destroy
#project.destroy
redirect_to projects_path
end
private
def load_project
#project = Project.find(params[:id])
end
def project_params
params.require(:project).permit(:name, :user_id)
end
end
And the tasks controller:
class TasksController < ApplicationController
before_action :set_task, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
#tasks = Task.where(project_id: project_id)
end
def show
project = Project.find_by(id: params[:project_id])
#task = Task.new(project: project)
end
def new
project = Project.find_by(id: params[:project_id])
#task = Task.new(project: project)
end
def edit
project = Project.find_by(id: params[:project_id])
#task = Task.new(project: project)
end
def references
respond_to do |format|
if #task.valid?
format.html { redirect_to root_url }
format.json { render :show, status: :created, location: #task }
else
format.html { render :home_url }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
def create
#project = Project.find_by(id: params[:project_id])
#task = #project.tasks.create(task_params)
respond_to do |format|
if #task.valid?
format.html { redirect_to root_url }
format.json { render :show, status: :created, location: #task }
else
format.html { render :home_url }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #task.update(task_params)
format.html { redirect_to root_url }
format.json { render :home_url, status: :ok, location: #task }
else
format.html { render :root_url }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
def edit
end
def destroy
#task.destroy
respond_to do |format|
format.html { redirect_to root_url }
format.json { head :no_content }
end
end
private
def set_task
#task = Task.find(params[:id])
end
def task_params
params.require(:task).permit(:deadline, :body, :project_id)
end
end
The routes file:
devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'projects#index'
resources :projects do
resources :tasks
end
And my routes:
projects#index
project_tasks GET /projects/:project_id/tasks(.:format) tasks#index
POST /projects/:project_id/tasks(.:format) tasks#create
new_project_task GET /projects/:project_id/tasks/new(.:format) tasks#new
edit_project_task GET /projects/:project_id/tasks/:id/edit(.:format) tasks#edit
project_task GET /projects/:project_id/tasks/:id(.:format) tasks#show
PATCH /projects/:project_id/tasks/:id(.:format) tasks#update
PUT /projects/:project_id/tasks/:id(.:format) tasks#update
DELETE /projects/:project_id/tasks/:id(.:format) tasks#destroy
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project GET /projects/:id(.:format) projects#show
PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
Can somebody please help me to clarify where is the problem and what is the problem?
The problem is with your TasksController#index action. What is project_id there? For accessing a project's tasks, the project needs to exist in the first place. And not just that. To access any CRUD action on tasks, a project has to exist first.
Modify your TasksController as
class TasksController < ApplicationController
before_action :set_project
before_action :set_task, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
#tasks = #project.tasks
end
def show
#makes use of set_task
end
def new
#task = #project.tasks.new
end
def edit
end
def references
respond_to do |format|
if #task.valid?
format.html { redirect_to root_url }
format.json { render :show, status: :created, location: #task }
else
format.html { render :home_url }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
def create
#task = #project.tasks.create(task_params)
respond_to do |format|
if #task.valid?
format.html { redirect_to root_url }
format.json { render :show, status: :created, location: #task }
else
format.html { render :home_url }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #task.update(task_params)
format.html { redirect_to root_url }
format.json { render :home_url, status: :ok, location: #task }
else
format.html { render :root_url }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
def destroy
#task.destroy
respond_to do |format|
format.html { redirect_to root_url }
format.json { head :no_content }
end
end
private
def set_project
#project = current_user.projects.find(params[:project_id]
end
def set_task
#task = #project.tasks.find(params[:id])
end
def task_params
params.require(:task).permit(:deadline, :body, :project_id)
end
end
Since we have defined a before_action set_project, #project will be available to all methods. Note that set_projectfinds the project from the projects created by the current user.
In the ProjectsController#index, you won't actually get a value for params[:project_id]. Modify your index action to
def index
#projects = current_user.projects unless current_user.nil?
end
And I don't understand your show method. The build method is actually used to create a in-memory representation of an object. The show method of projects_controller can be used to display the project along with its tasks. If this is what you need, change you show action in ProjectsController to
def show
##project is available from load_project
#tasks = #project.tasks
end
You could also modify your load_project project as
def load_project
begin
#project = Project.find(params[:id]) #raises an exception if project not found
rescue ActiveRecord::RecordNotFound
redirect_to projects_path
end
end
To know more about rescuing exceptions, see Begin, Rescue and Ensure in Ruby?
For more, see http://blog.8thcolor.com/en/2011/08/nested-resources-with-independent-views-in-ruby-on-rails/
If the form to create a new task is in projects/index.html.erb you should make a #task variable available to the view; try to change the index action as follow:
def index
#projects = current_user.projects unless current_user.nil?
#task = Task.new
end
The problem is with the url for submitting the form. If you check your rake routes you'd see that all your task routes would be nested under projects, therefore in passing the url option, you should have something like:
<div class="glyphicon glyphicon-plus col-xs-1 left_plus" ></div>
<div class="col-xs-10" >
<%= form_for [#project, #task],url: project_tasks_path(#project) do |f| %>
<%= f.input :body,class: 'form-control' %>
<%= f.submit 'Add task', class: 'btn' %>
<% end %>
or even better, I think you should be able to do that without passing the url option:
<div class="glyphicon glyphicon-plus col-xs-1 left_plus" ></div>
<div class="col-xs-10" >
<%= form_for [#project, #task] do |f| %>
<%= f.input :body,class: 'form-control' %>
<%= f.submit 'Add task', class: 'btn' %>
<% end %>
UPDATE
def index
project = Project.find(params[:project_id])
#projects = ProjectsViewPresenter.new(project)
end
# presenters/projects_view_presenter.rb
class ProjectsViewPresenter
attr_reader :project
def initialize(project)
#project = project
end
def tasks
#tasks ||= project.tasks
end
def task
#task ||= tasks.new
end
end
Your form_for would now be like this:
<div class="glyphicon glyphicon-plus col-xs-1 left_plus" ></div>
<div class="col-xs-10" >
<%= form_for [#project.project, #project.task] do |f| %>
<%= f.input :body,class: 'form-control' %>
<%= f.submit 'Add task', class: 'btn' %>
<% end %>

Rails model form f.select do not assign selected value to model key

sorry for this question but I'm struggling with this issue for hours now and can't find the answer anywhere.
Here is the thing, I have a rails app with "Reservation" and "Space" models with the following relations:
class Reservation < ActiveRecord::Base
belongs_to :space
belongs_to :user
end
class Space < ActiveRecord::Base
belongs_to :condo
has_many :reservations
end
When the user creates a new Reservation, in the form he gets to choose from a dropdown (f.select) the spaces available for him. The f.select in the form look like this:
<div class="field">
<%= #user_spaces = current_user.condo.spaces
f.select :space_id,
options_from_collection_for_select(#user_spaces, :id, :name), :prompt => "Select space"
%>
</div>
That select it supose to assign a value to the key "space_id" in the Reservation that is being created (column's table is created). But when I check the last reservation in Rails console, space_id value is "nil". What am I doing wrong?
Thank you very much for your help
Reservation controller file:
class ReservationsController < ApplicationController
before_action :set_reservation, only: [:show, :edit, :update, :destroy]
# GET /reservations
# GET /reservations.json
def index
#reservations = Reservation.all
end
# GET /reservations/1
# GET /reservations/1.json
def show
end
# GET /reservations/new
def new
#reservation = Reservation.new
end
# GET /reservations/1/edit
def edit
end
# POST /reservations
# POST /reservations.json
def create
#reservation = Reservation.new(reservation_params)
#user = current_user.id
#reservation.user_id = #user
respond_to do |format|
if #reservation.save
format.html { redirect_to #reservation, notice: 'Reservation was successfully created.' }
format.json { render :show, status: :created, location: #reservation }
else
format.html { render :new }
format.json { render json: #reservation.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reservations/1
# PATCH/PUT /reservations/1.json
def update
respond_to do |format|
if #reservation.update(reservation_params)
format.html { redirect_to #reservation, notice: 'Reservation was successfully updated.' }
format.json { render :show, status: :ok, location: #reservation }
else
format.html { render :edit }
format.json { render json: #reservation.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reservations/1
# DELETE /reservations/1.json
def destroy
#reservation.destroy
respond_to do |format|
format.html { redirect_to reservations_url, notice: 'Reservation was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_reservation
#reservation = Reservation.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def reservation_params
params.require(:reservation).permit(:eventdate)
end
end
Space controller file:
class SpacesController < ApplicationController
before_action :set_space, only: [:show, :edit, :update, :destroy]
# GET /spaces
# GET /spaces.json
def index
#spaces = Space.all
end
# GET /spaces/1
# GET /spaces/1.json
def show
end
# GET /spaces/new
def new
#space = Space.new
end
# GET /spaces/1/edit
def edit
end
# POST /spaces
# POST /spaces.json
def create
#space = Space.new(space_params)
respond_to do |format|
if #space.save
format.html { redirect_to #space, notice: 'Space was successfully created.' }
format.json { render :show, status: :created, location: #space }
else
format.html { render :new }
format.json { render json: #space.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /spaces/1
# PATCH/PUT /spaces/1.json
def update
respond_to do |format|
if #space.update(space_params)
format.html { redirect_to #space, notice: 'Space was successfully updated.' }
format.json { render :show, status: :ok, location: #space }
else
format.html { render :edit }
format.json { render json: #space.errors, status: :unprocessable_entity }
end
end
end
# DELETE /spaces/1
# DELETE /spaces/1.json
def destroy
#space.destroy
respond_to do |format|
format.html { redirect_to spaces_url, notice: 'Space was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_space
#space = Space.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def space_params
params.require(:space).permit(:name)
end
end
And full Reservation Form:
<%= form_for(#reservation) do |f| %>
<% if #reservation.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#reservation.errors.count, "error") %> prohibited this reservation from being saved:</h2>
<ul>
<% #reservation.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :eventdate %><br>
<%= f.date_select :eventdate %>
</div>
<div class="field">
<%= #user = current_user.condo.spaces
f.select :space_id,
options_from_collection_for_select(#user, :id, :name), :prompt => "Select space"
%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
pretty sure you need to permit the space_id attribute in your strong params.
def reservation_params
params.require(:reservation).permit(:eventdate, :space_id)
end
whats happening is that when you go to create a reservation, youre passing in set of params, that is the output of reservation_params
#reservation = Reservation.new(reservation_params)
if space_id is not being permitted in your strong params, then it will be nil when created.
if this is not the issue, can you post what params are getting to the server, and what the output of reservation_params are.

Rails - using link_to on application.html.erb for nested resource not getting id

I have a sidebar on application.html.erb, and the links should go to /brands/[brand_id]/coupons
I used brand_coupons_path(#brand) but I get an error saying 'No route matches {:action=>"index", :brand_id=>nil, :controller=>"coupons"} missing required keys: [:brand_id]'
resources :brands do
resources :coupons, :sales
end
class Brand < ActiveRecord::Base
has_many :coupons
has_many :sales
accepts_nested_attributes_for :coupons
accepts_nested_attributes_for :sales
end
class Coupon < ActiveRecord::Base
belongs_to :brand
end
<div class="sidebar">
<ul class="sidebar-list">
<li><a class="sidebar-header">Most Popular Brands</a></li>
<% #brands.each do |brand| %>
<% if current_page?(:controller => 'coupons') %>
<li><%= link_to brand.name, brand_coupons_path(#brand), :class => "sidebar-link" %></li>
<% else %>
<li><%= link_to brand.name, brand_sales_path(#brand), :class => "sidebar-link" %></li>
<% end %>
<% end %>
</ul>
</div>
class CouponsController < ApplicationController
before_action :set_coupon, only: [:show, :edit, :update, :destroy]
# before_filter :load_brand
def new
#coupon = Coupon.new
end
def index
#coupons = Coupon.where(brand_id: params[:brand_id])
end
def show
end
def create
#coupon = Coupon.new(coupon_params)
respond_to do |format|
if #coupon.save
format.html { redirect_to '/', notice: 'Coupon was successfully created.' }
format.json { render :show, status: :created, location: #coupon }
else
format.html { render :new }
format.json { render json: #coupon.errors, status: :unprocessable_entity }
end
end
end
class BrandsController < ApplicationController
before_action :set_brand, only: [:show, :edit, :update, :destroy]
# GET /brands
# GET /brands.json
def index
#brands = Brand.all
end
# GET /brands/1
# GET /brands/1.json
def show
end
# GET /brands/new
def new
#brand = Brand.new
end
# GET /brands/1/edit
def edit
end
# POST /brands
# POST /brands.json
def create
#brand = Brand.new(brand_params)
respond_to do |format|
if #brand.save
format.html { redirect_to #brand, notice: 'Brand was successfully created.' }
format.json { render :show, status: :created, location: #brand }
else
format.html { render :new }
format.json { render json: #brand.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /brands/1
# PATCH/PUT /brands/1.json
def update
respond_to do |format|
if #brand.update(brand_params)
format.html { redirect_to #brand, notice: 'Brand was successfully updated.' }
format.json { render :show, status: :ok, location: #brand }
else
format.html { render :edit }
format.json { render json: #brand.errors, status: :unprocessable_entity }
end
end
end
# DELETE /brands/1
# DELETE /brands/1.json
def destroy
#brand.destroy
respond_to do |format|
format.html { redirect_to brands_url, notice: 'Brand was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_brand
#brand = Brand.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def brand_params
params.require(:brand).permit(:name, :logo)
end
end
You're passing an undefined #brand variable into your routes helpers, rather than the block variable brand. Change:
<%= link_to brand.name, brand_coupons_path(#brand), :class => "sidebar-link" %>
<%= link_to brand.name, brand_sales_path(#brand), :class => "sidebar-link" %>
to
<%= link_to brand.name, brand_coupons_path(brand), :class => "sidebar-link" %>
<%= link_to brand.name, brand_sales_path(brand), :class => "sidebar-link" %>
What does #brand contains? I think you should put an object to #brand variable.
Like this. #brand = Brand.find(params[:id])
Can we see the controller of your index?

Resources