param is missing or the value is empty: room - ruby-on-rails

i created an edit page to edit the room(model) and update the form to change the current name and current capacity to whatever we wish but i am getting an error
ActionController::ParameterMissing in RoomsController#edit
param is missing or the value is empty: room
rooms_controller.rb
class RoomsController < ApplicationController
before_action :set_room, only: %i[show edit update]
def index
#rooms = Room.all
end
def show
end
def new
#room = Room.new
end
def create
#room = Room.new(room_params)
respond_to do |format|
if #room.save
format.html { redirect_to room_url(#room), notice: "Room was created Successfully" }
else
format.html { render :new, status: :unprocessable_entity }
end
end
end
def edit
respond_to do |format|
if #room.update(room_params)
format.html { redirect_to room_url(#room), notice: "Room was successfully updated!" }
else
format.html { render :edit, status: :unprocessable_entity }
end
end
end
private
def set_room
#room = Room.find(params[:id])
end
def room_params
params.require(:room).permit(:name, :capacity)
end
end
edit.hml.erb
<h2>Edit Room</h2>
<%= render "form", room: #room %>
_form.html.erb
<%= form_with(model: room) do |form| %>
<% if room.errors.any? %>
<div style="color: red">
<h2><%= pluralize(room.errors.count, "errors") %> Prohibited this Room from saving</h2>
<ul>
<% room.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name %>
</div>
<div>
<%= form.label :capacity, style: "display: block" %>
<%= form.number_field :capacity %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
i am using the same partial _form.html.erb for both new.html.erb and edit.html.erb , is it because of using same partial form for edit and new or there is some other reason?
new.html.erb
<h1>New Room</h1>
<%= render "form", room: #room %>

You're using the wrong action.
In Rails flavored REST the edit action responds to a GET /rooms/:id/edit request and just renders the form. It should also be idempotent. There is no room parameter since you're not responding to a form submission.
Updating the resource is done in the update method (PATCH /rooms/:id).
class RoomsController < ApplicationController
# ...
# you can actually completely omit this method
# Rails will implicitly render edit.html.erb anyways
# GET /rooms/1/edit
def edit
end
# PATCH /rooms/1
def update
# you don't need to use MimeResponds if you're only responding to HTML requests. KISS
if #room.update(room_params)
redirect_to #room, notice: "Room was successfully updated!"
else
render :edit, status: :unprocessable_entity
end
end
# ...
end

Related

How do I call services on my bookings form? Ruby on Rails

So I have clients page, services page and using a bookable gem. So currently when I click "Book" besides a customers name, a booking form pops up which allows me to choose a date and time and the length of the appointment. However, with this form I would like to also be able to choose the service that the appointment needs, so once a service has been added, a drop down list on the bookings form will show the services which have been added. (Hopefully that made sense)
Is there any chance someone could please help me with how I can do this?
Bookings (_form.html.erb)
<%= form_for([#client, #booking]) do |f| %>
<p>
<%= f.label 'start_time', 'Start time' %>
<%= f.datetime_select :start_time, { minute_step: 15 } %>
</p>
<p>
<%= f.label 'length', 'Length of booking in hours' %>
<%= f.number_field 'length', min: 1 %>
</p>
<%= f.submit 'Submit' %>
<% end %>
Services (index.html.erb)
<h1>Services <%= link_to "+ New", new_service_path %></h1>
<table>
<div class="row">
<div class="hidden-xs col-sm-3">
<h3>Name</h3>
</div>
<div class="hidden-xs col-sm-3">
<h3>Description</h3>
</div>
<div class="hidden-xs col-sm-3">
<h3>Price</h3>
</div>
<div class="hidden-xs col-sm-3">
<h3>Service Duration</h3>
</div>
</div>
<tbody>
<% #services.each do |service| %>
<tr>
<td><%= service.name %></td>
<td class="tb1"><%= service.description %></td>
<td class="tb2"><%= service.price %></td>
<td class="tb3"><%= service.duration %></td>
<td><%= link_to 'Show', service %></td>
<td><%= link_to 'Edit', edit_service_path(service) %></td>
<td><%= link_to 'Destroy', service, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
bookings_controller.rb
class BookingsController < ApplicationController
respond_to :html, :xml, :json
before_action :find_client
def index
#bookings = Booking.where("client_id = ? AND end_time >= ?", #client.id, Time.now).order(:start_time)
respond_with #bookings
end
def new
#booking = Booking.new(client_id: #client.id)
end
def create
#booking = Booking.new(params[:booking].permit(:client_id, :start_time, :length))
#booking.client = #client
if #booking.save
redirect_to client_bookings_path(#client, method: :get)
else
render 'new'
end
end
def show
#booking = Booking.find(params[:id])
end
def destroy
#booking = Booking.find(params[:id]).destroy
if #booking.destroy
flash[:notice] = "Booking: #{#booking.start_time.strftime('%e %b %Y %H:%M%p')} to #{#booking.end_time.strftime('%e %b %Y %H:%M%p')} deleted"
redirect_to client_bookings_path(#client)
else
render 'index'
end
end
def edit
#booking = Booking.find(params[:id])
end
def update
#booking = Booking.find(params[:id])
# #booking.clients = #clients
if #booking.update(params[:booking].permit(:client_id, :start_time, :length))
flash[:notice] = 'Your booking was updated succesfully'
if request.xhr?
render json: {status: :success}.to_json
else
redirect_to client_bookings_path(#client)
end
else
render 'edit'
end
end
private
def save booking
if #booking.save
flash[:notice] = 'booking added'
redirect_to client_booking_path(#client, #booking)
else
render 'new'
end
end
def find_client
if params[:client_id]
#client = Client.find_by_id(params[:client_id])
end
end
end
services_controller.rb
class ServicesController < ApplicationController
before_action :set_service, only: [:show, :edit, :update, :destroy]
# GET /services
# GET /services.json
def index
#services = Service.all
end
# GET /services/1
# GET /services/1.json
def show
end
# GET /services/new
def new
#service = Service.new
end
# GET /services/1/edit
def edit
end
# POST /services
# POST /services.json
def create
#service = Service.new(service_params)
respond_to do |format|
if #service.save
format.html { redirect_to #service, notice: 'Service was successfully created.' }
format.json { render :show, status: :created, location: #service }
else
format.html { render :new }
format.json { render json: #service.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /services/1
# PATCH/PUT /services/1.json
def update
respond_to do |format|
if #service.update(service_params)
format.html { redirect_to #service, notice: 'Service was successfully updated.' }
format.json { render :show, status: :ok, location: #service }
else
format.html { render :edit }
format.json { render json: #service.errors, status: :unprocessable_entity }
end
end
end
# DELETE /services/1
# DELETE /services/1.json
def destroy
#service.destroy
respond_to do |format|
format.html { redirect_to services_url, notice: 'Service was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_service
#service = Service.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def service_params
params.require(:service).permit(:name, :description, :price, :duration)
end
end
So as far as i can tell, what you want is on the bookings form (ie the new action) you'd like to display a drop-down that has all of the services so it can be chosen. Then when they submit this, they add that service to their booking?
Well, firstly - you'd need to add service_id as a column to your bookings table and the association in the Booking class.
I'd recommend using the specialised migration for associations: references eg:
class AddServiceToBookings < ActiveRecord::Migration[5.0]
def change
add_reference :bookings, :service, foreign_key: true
end
end
It's a good idea to then add the association to your booking class:
class Booking < ActiveRecord::Base
belongs_to :service
Then you can show the collection of services in the service-drop-down in the form using collection_select:
<%= form_for([#client, #booking]) do |f| %>
<p>
<%= f.label 'start_time', 'Start time' %>
<%= f.datetime_select :start_time, { minute_step: 15 } %>
</p>
<p>
<%= f.label 'length', 'Length of booking in hours' %>
<%= f.number_field 'length', min: 1 %>
</p>
<p>
<%= f.label 'service_id', 'Service' %>
<%= f.collection_select :service_id, Service.all, :id, :name %>
</p>
<%= f.submit 'Submit' %>
<% end %>
Then you'll need to allow the service_id in your permit/require section of your bookings controller:
#booking = Booking.new(params[:booking].permit(:client_id, :service_id, :start_time, :length))
And you might need some more tweaking here and there, but this is the gist of it.

nested resource only working on edit not new, rails 5.0.0.1

I have the following resources defined:
resources :buildings do
resources :buildings_regular_hours
end
My models are are follows:
class Building < ApplicationRecord
has_many :building_regular_hours
def to_s
name
end
end
class BuildingsRegularHours < ApplicationRecord
belongs_to :building
end
I am attempting to create a form to allow for creating and editing of BuildingRegularHours. Currently the form I have will display on #edit, but will not display on #new.
new.html.erb:
<%= render 'form', buildings_regular_hour: #buildings_regular_hour, building: #building %>
edit.html.erb:
<h1>Editing Buildings Regular Hour</h1>
<%= render 'form', buildings_regular_hour: #buildings_regular_hour, building: #building %>
<%= link_to 'Back', building_buildings_regular_hour_path(#building) %>
_form.html.erb:
<%= form_for([building,buildings_regular_hour]) do |f| %>
<% if buildings_regular_hour.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(buildings_regular_hour.errors.count, "error") %> prohibited this buildings_regular_hour from being saved:</h2>
<ul>
<% buildings_regular_hour.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :building_id %>
<%= f.text_field :building_id %>
</div>
<div class="field">
<%= f.label :start_date %>
<%= f.date_select :start_date %>
</div>
<div class="field">
<%= f.label :end_date %>
<%= f.date_select :end_date %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
buildings_regular_hours_controller.rb:
class BuildingsRegularHoursController < ApplicationController
before_action :set_buildings_regular_hour, only: [:show, :edit, :update, :destroy]
before_action :set_building
# GET /buildings_regular_hours
# GET /buildings_regular_hours.json
def index
#buildings_regular_hours = BuildingsRegularHours.all
end
# GET /buildings_regular_hours/1
# GET /buildings_regular_hours/1.json
def show
end
# GET /buildings_regular_hours/new
def new
#buildings_regular_hour = BuildingsRegularHours.new
end
# GET /buildings_regular_hours/1/edit
def edit
end
# POST /buildings_regular_hours
# POST /buildings_regular_hours.json
def create
#buildings_regular_hour = BuildingsRegularHours.new(buildings_regular_hour_params)
respond_to do |format|
if #buildings_regular_hour.save
format.html { redirect_to #buildings_regular_hour, notice: 'Buildings regular hours was successfully created.' }
format.json { render :show, status: :created, location: #buildings_regular_hour }
else
format.html { render :new }
format.json { render json: #buildings_regular_hour.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /buildings_regular_hours/1
# PATCH/PUT /buildings_regular_hours/1.json
def update
respond_to do |format|
if #buildings_regular_hour.update(buildings_regular_hour_params)
format.html { redirect_to #buildings_regular_hour, notice: 'Buildings regular hours was successfully updated.' }
format.json { render :show, status: :ok, location: #buildings_regular_hour }
else
format.html { render :edit }
format.json { render json: #buildings_regular_hour.errors, status: :unprocessable_entity }
end
end
end
# DELETE /buildings_regular_hours/1
# DELETE /buildings_regular_hours/1.json
def destroy
#buildings_regular_hour.destroy
respond_to do |format|
format.html { redirect_to buildings_regular_hours_index_url, notice: 'Buildings regular hours was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_buildings_regular_hour
#buildings_regular_hour = BuildingsRegularHours.find(params[:id])
end
def set_building
#building = Building.find(params[:building_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def buildings_regular_hour_params
params.require(:buildings_regular_hour).permit(:building_id, :start_date, :end_date, :sunday_id, :monday_id, :tuesday_id, :wednesday_id, :thursday_id, :friday_id, :saturday_id)
end
end
Having added a BuildingRegularHours via console, I attempted the #edit action, and it works just fine, displaying the form as expected. However, when I attempt the #new action, I receive the following error:
Showing /Users/shawn/Documents/uga/library_hours/app/views/buildings_regular_hours/_form.html.erb where line #1 raised:
undefined method `building_buildings_regular_hours_index_path` for #<#<Class:0x007fe9589e2890>:0x007fe95f9bbb30>
Did you mean? building_buildings_regular_hours_path
building_buildings_regular_hour_path
building_buildings_regular_hours_url
building_buildings_regular_hour_url
Extracted source (around line #1):
1 <%= form_for([building,buildings_regular_hour]) do |f| %>
2 <% if buildings_regular_hour.errors.any? %>
3 <div id="error_explanation">
4 <h2><%= pluralize(buildings_regular_hour.errors.count, "error") %> prohibited this buildings_regular_hour from being saved:</h2>
5
6 <ul>
Trace of template inclusion: app/views/buildings_regular_hours/new.html.erb
I note that I have properly nested the resources in the form_for tag, that both #building and #building_regular_hour are set by the controller, and that I am calling the form in exactly the same way for both #edit and #new. This is all I've had to do previously to make nested resources work, so I'm at a bit of a loss as what to do next.
Please note I have not attempted to make the form work yet - I know there is work to be done there. I just am trying to get #new to display the form.
You need to correct the association
class Building < ApplicationRecord
has_many :buildings_regular_hours
def to_s
name
end
end
class BuildingsRegularHour < ApplicationRecord
belongs_to :building
end
Your model name should always be singular BuildingsRegularHour or it will create issues with routes and associations

Unable to assign a parent id to nested comments, using Ancestry gem (ruby on rails)

I am creating nested comments (like you find on Reddit). I am able to create parent comments, but when I try to create a child comment, it simply renders as a parent comment.
In my rails console, the "ancestry" field comes back "nil".
This is my comments controller:
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!
def show
#comment = Comment.find(params[:id])
end
def new
#link = Link.find(params[:link_id])
#comment = Comment.new(:parent_id => params[:parent_id])
#comments = Comment.all
end
def create
#link = Link.find(params[:link_id])
#parent = Link.find(params[:link_id]) if params[:link_id]
#parent = Comment.find(params[:comment_id]) if params[:comment_id]
#comment = #parent.comments.new(comment_params)
#comment.user = current_user
respond_to do |format|
if #comment.save
format.html { redirect_to #link, notice: 'Comment was successfully created.' }
format.json { render json: #comment, status: :created, location: #comment }
else
format.html { render action: "new" }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
def destroy
#comment.destroy
respond_to do |format|
format.html { redirect_to :back, notice: 'Comment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_comment
#comment = Comment.find(params[:id])
end
def comment_params
params.require(:comment).permit(:link_id, :body, :user_id)
end
end
Here is my _comment_form partial
<%= div_for(comment) do %>
<div class="comments_wrapper clearfix">
<div class="pull-left">
<p class="lead"><%= comment.body %></p>
<p><small>Submitted <strong><%= time_ago_in_words(comment.created_at) %> ago</strong> by <%= comment.user.email %></small></p>
<div id="reply" style="display:none;">
<%= form_for [#comment = Comment.new(:parent_id => params[:parent_id])] do |f| %>
<%= f.hidden_field :parent_id %>
<%= f.text_area :body %> <br>
<%= f.submit %>
<% end %>
</div>
</div>
<div class="actions btn-group pull-right">
<button onClick="$('#reply').show()" class="btn btn-sm btn-default">Reply</button>
<% if comment.user == current_user -%>
<%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-sm btn-default" %>
<% end %>
</div>
</div>
<% end %>
These are my routes
Rails.application.routes.draw do
resources :comments
devise_for :users
devise_for :installs
resources :links do
member do
put "like", to: "links#upvote"
put "dislike", to: "links#downvote"
end
resources :comments
end
root to: "links#index"
end
Had this problem before; the answer is here:
Ancestry gem in Rails and Mutli Nesting
The problem with ancestry (this is why we changed back to acts_as_tree) is that you have to define all the ancestors in the ancestry column (as opposed to just the parent_id column of acts_as_tree).
Thus, when you call the .children of an object (where you've literally just populated ancestry with top-level parents) is a list of children for that parent (no others).
What you need is to reference the entire ancestry line. This is quite tricky, but can be achieved using the code below:
#app/views/links/index.html.erb
<%= render #link.comments if #post.comments.any? %>
#app/views/links/_comment.html.erb
<%= comment.title %>
<%= render "form", locals: {link: #link} %>
<%= render comment.children if comment.has_children? # > adds recursion (multi level nesting) %>
#app/views/links/_form.html.erb
<%= form_for link.comments.new do |c| %>
<%= c.text_field :body %>
<%= c.submit %>
<% end %>
The controller is as follows:
#app/controllers/comments_controller.rb
class CommentsController < ApplicationController
def create
#link = Link.find params[:link_id]
#comment = #link.comments.new ancesrtry: parent(params[:parent_id])
end
private
def parent(param)
parents = Comment.find(param).pluck(:parent)
"#{parents}/#{param}" #-> ruby automatically returns last line
end
end
This should set the correct path for you, and the partials should give you the appropriate recursion required for multi level nesting.

Rails link_to path

I am trying to make an app in Rails 4.
I have 3 models: Project, Project_Question, Project_Answer
The associations are:
Project:
has_many :project_questions
and accepts nested attributes for project questions.
Project Question:
belongs_to :project
has_one :project_answer
and accepts nested attributes for Project Answers.
My routes are nested as follows:
resources :projects do
resources :project_questions do
resources :project_answers
end
end
In my Project Questions partial, I want a link to answer the question. I've tried:
<%= link_to 'Answer this question', new_project_project_question_project_answer_path(:project_question_id => project_question.id) %>
I have a route with that name in my routes file, but I'm getting this error message:
undefined local variable or method `project_question' for #<#<Class:0x0000010742b9d8>:0x0000010f810b68>
What should go in the brackets?
View:
<div class="containerfluid">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<% #project.project_questions.each do |singleQuestion| %>
<div class="categorytitle">
<%= singleQuestion.title %>
</div>
<div class="generaltext">
<%= singleQuestion.try(:content) %>
</div>
<span class="editproject">
<% if current_user.id == #project.creator_id %>
<%= link_to 'Answer this question', new_project_project_questions_project_answer_path(:project_question_id => project_question.id) %>
<% end %>
</span>
<% end %>
</div>
</div>
</div>
Project Question controller:
class ProjectQuestionsController < ApplicationController
before_action :set_project_question, only: [:show, :edit, :update, :destroy]
# GET /project_questions
# GET /project_questions.json
def index
#project_questions = ProjectQuestion.all
end
# GET /project_questions/1
# GET /project_questions/1.json
def show
end
# GET /project_questions/new
def new
#project_question = ProjectQuestion.new
#project = Project.find(params[:project_id])
# #project_id = params[:project_id]
#project_question.project_answers[0] = ProjectAnswer.new
end
# GET /project_questions/1/edit
def edit
end
# POST /project_questions
# POST /project_questions.json
def create
#project_question = ProjectQuestion.new(project_question_params)
#project_question.project_id = project_question_params[:project_id]
respond_to do |format|
if #project_question.save
format.html { redirect_to project_url(Project.find(project_question_params[:project_id])), notice: 'Project question was successfully created.' }
format.json { render action: 'show', status: :created, location: #project_question }
else
format.html { render action: 'new' }
format.json { render json: #project_question.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /project_questions/1
# PATCH/PUT /project_questions/1.json
def update
respond_to do |format|
if #project_question.update(project_question_params)
format.html { redirect_to #project_question, notice: 'Project question was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #project_question.errors, status: :unprocessable_entity }
end
end
end
# DELETE /project_questions/1
# DELETE /project_questions/1.json
def destroy
#project_question.destroy
respond_to do |format|
format.html { redirect_to project_questions_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project_question
#project_question = ProjectQuestion.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_question_params
params[:project_question].permit(:id, :title, :content, :project_id, :user_id,
project_answer_atttibutes: [:id, :answer, :project_question_id, :user_id]
)
end
end
When you run rake routes, you will find this one
new_project_project_question_project_answer GET /projects/:project_id/project_questions/:project_question_id/project_answers/new(.:format) project_answers#new
That means it requires :project_id and :project_question_id as keys.
This should work
<%= link_to 'Answer this question', new_project_project_question_project_answer_path(:project_id => #project.id, :project_question_id => singleQuestion.id) %>
Notice new_project_project_question_project_answer_path not new_project_project_questions_project_answer_path
Your link_to should be something below
<%= link_to 'Answer this question', new_project_project_questions_project_answer_path(:project_id => #project.id, :project_question_id => singleQuestion.id) %>
View will look like below
<div class="containerfluid">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<% #project.project_questions.each do |singleQuestion| %>
<div class="categorytitle">
<%= singleQuestion.title %>
</div>
<div class="generaltext">
<%= singleQuestion.try(:content) %>
</div>
<span class="editproject">
<% if current_user.id == #project.creator_id %>
<%= link_to 'Answer this question', new_project_project_questions_project_answer_path(:project_question_id => singleQuestion.id) %>
<% end %>
</span>
<% end %>
</div>
</div>
</div>
Check that params
def project_question_params
params[:project_question].permit(:id, :title, :content, :project_id, :user_id,
project_answer_atttibutes: [:id, :answer, :project_question_id, :user_id]
)
end
There is project_id
and you did not pass it in link_to
so it thwos new errormissing required keys: [:project_id]

Basic Rails - how do automatically assign a new database entry to an associated entry it belongs to?

I'd like to automatically associate a new database entry with the database entry it belongs to without having to make a choice while on the form as the user can only come from the category page, so that once you're in a category and you decide to make a new entry within that category, the newly created entry is automatically within that category upon submission. Can anyone offer any help?
My models are as follows:
class Category < ActiveRecord::Base
has_many :guides
end
class Guide < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :ratings
def average_rating
average = ratings.inject(0.0){ |sum, el| sum + el.value }.to_f / ratings.size
average.round(2)
end
end
The link to create the new guide for the category is pretty standard, though I thought that adding an instance variable might automatically associate the entry with the category though it doesn't:
<%= link_to 'New Guide', new_guide_path(#category) %>
Here is the controller for the guide:
class GuidesController < ApplicationController
before_action :set_guide, only: [:show, :edit, :update, :destroy]
# GET /guides
# GET /guides.json
def index
#guides = Guide.all
end
# GET /guides/1
# GET /guides/1.json
def show
end
# GET /guides/new
def new
#guide = Guide.new
end
# GET /guides/1/edit
def edit
end
# POST /guides
# POST /guides.json
def create
#guide = Guide.new(guide_params)
respond_to do |format|
if #guide.save
format.html { redirect_to #guide, notice: 'Guide was successfully created.' }
format.json { render :show, status: :created, location: #guide }
else
format.html { render :new }
format.json { render json: #guide.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /guides/1
# PATCH/PUT /guides/1.json
def update
respond_to do |format|
if #guide.update(guide_params)
format.html { redirect_to #guide, notice: 'Guide was successfully updated.' }
format.json { render :show, status: :ok, location: #guide }
else
format.html { render :edit }
format.json { render json: #guide.errors, status: :unprocessable_entity }
end
end
end
# DELETE /guides/1
# DELETE /guides/1.json
def destroy
#guide.destroy
respond_to do |format|
format.html { redirect_to guides_url, notice: 'Guide was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_guide
#guide = Guide.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def guide_params
params.require(:guide).permit(:name, :category_id, :user_id, :stepOneText, :stepOnePhoto, :stepTwoText, :stepTwoPhoto, :stepThreeText, :stepThreePhoto)
end
end
Form is pretty standard too, is there anything I should put in here to automatically assign it to the category entry it belongs to?
<%= form_for(#guide) do |f| %>
<% if #guide.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#guide.errors.count, "error") %> prohibited this guide from being saved:</h2>
<ul>
<% #guide.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :stepOneText %>
<%= f.text_field :stepOneText %>
</div>
<div class="field">
<%= f.label :stepOnePhoto %>
<%= f.text_field :stepOnePhoto %>
</div>
<div class="field">
<%= f.label :stepTwoText %>
<%= f.text_field :stepTwoText %>
</div>
<div class="field">
<%= f.label :stepTwoPhoto %>
<%= f.text_field :stepTwoPhoto %>
</div>
<div class="field">
<%= f.label :stepThreeText %>
<%= f.text_field :stepThreeText %>
</div>
<div class="field">
<%= f.label :stepThreePhoto %>
<%= f.text_field :stepThreePhoto %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Seems to me that you could go for something like a nested route here :
resources :categories do
resources :guides
end
and then use your new route
<%= link_to 'New Guide', new_category_guide_path(#category) %>
This should make it easier to get the guide's category back when getting the form back.
Assuming that you have a column on your guide table that stores category ID, and your routes are nested as has been recommended, you should be able to add
#guide.category_id = #category.id
To your guides controller create action. And in your form change the first line to
<%= form_for[#category, #guide] do |f| %>
Now this should work
<%= link_to 'new guide', new_category_guide_path(#category) %>
And the current category should be assigned to your guide when it's created.

Resources