In ruby on rails i want to show user's articles. Here is my articles_controller.rb
class ArticlesController < ApplicationController
before_action :set_article, only: %i[ show edit update destroy ]
before_action :authenticate_user!, except: [:index, :show ]
before_action :correct_user, only: [:edit, :update, :destroy]
# GET /articles or /articles.json
def index
#articles = Article.all
end
def myarticles
#articles = Article.all
end
# GET /articles/1 or /articles/1.json
def show
end
# GET /articles/new
def new
##article = Article.new
#article = current_user.article.build
end
# GET /articles/1/edit
def edit
end
# POST /articles or /articles.json
def create
##article = Article.new(article_params)
#article = current_user.article.build(article_params)
respond_to do |format|
if #article.save
format.html { redirect_to #article, notice: "Article was successfully created." }
format.json { render :show, status: :created, location: #article }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #article.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /articles/1 or /articles/1.json
def update
respond_to do |format|
if #article.update(article_params)
format.html { redirect_to #article, notice: "Article was successfully updated." }
format.json { render :show, status: :ok, location: #article }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #article.errors, status: :unprocessable_entity }
end
end
end
# DELETE /articles/1 or /articles/1.json
def destroy
#article.destroy
respond_to do |format|
format.html { redirect_to articles_url, notice: "Article was successfully destroyed." }
format.json { head :no_content }
end
end
def correct_user
#article = current_user.article.find_by(id: params[:id])
#redirect_to friends_path, notice: "Not Authorized To Edit This Friend" if #friend.nil?
redirect_to articles_path, notice: "Not Authorized To Edit This Article" if #article.nil?
end
private
# Use callbacks to share common setup or constraints between actions.
def set_article
#article = Article.find(params[:id])
end
# Only allow a list of trusted parameters through.
def article_params
params.require(:article).permit(:title, :description, :paragraph, :image, :content, :user_id)
#params.require(:article).permit(:title, :description, :paragraph, :image, :content) eskisi
end
end
in views/articles i have myarticles.html.erb
and in routes i have get 'articles/myarticles'
in myarticles.html.erb i have the same code for show.html.erb(actually i want to show the articles that belongs to that user but i will add that later):
<p id="notice"><%= notice %></p>
<div style="justify-content: center;display: flex;" >
<div style="max-width:680px;width: 100%;" >
<%# <div class="container-{680px}"> %>
<br>
<h1>
<%= #article.title %>
</h1>
<p>
<%= #article.description %>
</p>
<%# <p style="justify-content: center;display: flex;" > %>
<p style="flex-direction: column;display: flex;" >
<%if #article.image.attached? %>
<%=image_tag #article.image ,class:"img-fluid"%>
<% end %>
</p>
<p>
<%= #article.content %>
</p>
<%#
<p>
<%= #article.paragraph %>
<%# </p> %>
<br>
<%= link_to 'Edit', edit_article_path(#article) ,class:"btn btn-secondary"%> |
<%= link_to 'Back', articles_path ,class:"btn btn-secondary"%>
</div>
</div>
But i got this message:
NoMethodError in Articles#myarticles
Showing C:/Users/oem/Desktop/blog/app/views/articles/myarticles.html.erb where line #8 raised:
undefined method `title' for nil:NilClass
<h1>
<%= #article.title %>
</h1>
In your controller actionmyarticles you declared #articles. But in the view you are using #article. #article is available in show where is declared with the before_action callback, but not in my articles.
You probably want to show a list of articles in myarticles view so you can do something like:
<%= #articles.each do |article| %>
<h1>
<%= article.title %>
</h1>
...and so on
<% end %>
Related
I'm trying to create a stock market app and I have a button that is breaking my web app. My issue is with this bit of embedded ruby on my
show.html.erb page:
<%= render #stock %>
<div>
<%= link_to "Edit this stock", edit_stock_path(#stock) %> |
<%= link_to "Back to stocks", stocks_path %>
<%= button_to "Delete this stock", #stock, method: :delete %>
</div>
This#stock.destroy line is needed for removing entries for a user. The error rails throws:
Showing C:/stockmarketapp/app/views/stocks/show.html.erb where line #9 raised:
undefined method `destroy' for #<StockQuote::Stock:0x000001f7fc0aa780 #attribution="Data provided for free by IEX (https://iexcloud.io).",
#response_code=200, #avg_total_volume=2261555,
#calculation_price="close", #change=-0.36, #change_percent=-0.00331,
#close=108.41, #close_source="official", #close_time=1654286400058,
#company_name="BlackRock Institutional Trust Company N.A. - iShares Core High Dividend ETF", #currency="USD", #delayed_price=108.4,
#delayed_price_time=1654286390674, #extended_change=0, #extended_change_percent=0, #extended_price=108.41,
#extended_price_time=1654300800004, #high=108.93, #high_source="15
minute delayed price", #high_time=1654286395170, #iex_ask_price=nil,
#iex_ask_size=nil, #iex_bid_price=nil, #iex_bid_size=nil,
#iex_close=108.43, #iex_close_time=1654286330444,
#iex_last_updated=nil, #iex_market_percent=nil, #iex_open=108.81,
#iex_open_time=1654263316934, #iex_realtime_price=nil,
#iex_realtime_size=nil, #iex_volume=nil,
#last_trade_time=1654286394157, #latest_price=108.41,
#latest_source="Close", #latest_time="June 3, 2022",
#latest_update=1654286400058, #latest_volume=876363, #low=108.25,
#low_source="15 minute delayed price", #low_time=1654273215082,
#market_cap=13133871500, #odd_lot_delayed_price=108.395,
#odd_lot_delayed_price_time=1654286390033, #open=108.33,
#open_time=1654263000128, #open_source="official", #pe_ratio=nil,
#previous_close=108.77, #previous_volume=1062893,
#primary_exchange="NYSE ARCA", #symbol="HDV", #volume=876363,
#week52_high=110.91, #week52_low=91.09,
#ytd_change=0.07801100501710605, #is_us_market_open=false>
Extracted source (around line #9):78910
<%= link_to "Edit this stock", edit_stock_path(#stock) %> |
<%= link_to "Back to stocks", stocks_path %>
<%= button_to "Delete this stock", #stock.destroy, method: :delete %>
</div>
Rails.root: C:/stockmarketapp
Application Trace | Framework Trace | Full Trace
app/views/stocks/show.html.erb:9
These are my files related to #stock and #stock.destroy:
class StocksController < ApplicationController
before_action :set_stock, only: %i[ show edit update destroy ]
before_action :correct_user, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
#api = StockQuote::Stock.new(api_key: "pk_153e93f4bce64c3d87e7d4d3510153ed")
# GET /stocks or /stocks.json
def index
#stocks = Stock.all
end
# GET /stocks/1 or /stocks/1.json
def show
#stocks = Stock.all
end
# GET /stocks/new
def new
#stock = Stock.new
end
# GET /stocks/1/edit
def edit
end
# POST /stocks or /stocks.json
def create
#stock = Stock.new(stock_params)
respond_to do |format|
if #stock.save
format.html { redirect_to stock_url(#stock), notice: "Stock was successfully created." }
format.json { render :show, status: :created, location: #stock }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #stock.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /stocks/1 or /stocks/1.json
def update
respond_to do |format|
if #stock.update(stock_params)
format.html { redirect_to stock_url(#stock), notice: "Stock was successfully updated." }
format.json { render :show, status: :ok, location: #stock }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #stock.errors, status: :unprocessable_entity }
end
end
end
# DELETE /stocks/1 or /stocks/1.json
def destroy
#stock.destroy
respond_to do |format|
format.html { redirect_to stocks_url, notice: "Stock was successfully destroyed." }
format.json { head :no_content }
end
end
def correct_user
#ticker = current_user.stocks.find_by(id: params[:id])
redirect_to stocks_path, notice: "Not Autherized to edit this stock" if #ticker.nil?
end
private
# Use callbacks to share common setup or constraints between actions.
def set_stock
#stock = Stock.find(params[:id])
end
# Only allow a list of trusted parameters through.
def stock_params
params.require(:stock).permit(:ticker, :user_id)
end
end
Stock partial:
<% #stock= StockQuote::Stock.quote(stock.ticker) %>
<div id="<%= dom_id stock %>">
<p>
<strong>Ticker:</strong>
<%= #stock.symbol %>
</p>
<p>
<strong>Open</strong>
$<%= #stock.open %>
</p>
<p>
<strong>Price</strong>
$<%= #stock.latest_price %>
</p>
<p>
<strong>Close</strong>
$<%= #stock.close %>
</p>
<p>
<strong>Company Name</strong>
<%= #stock.company_name %>
</p>
<p>
<strong>Primary Exchange</strong>
<%= #stock.primary_exchange %>
</p>
<p>
<strong>Volume</strong>
<%= number_with_delimiter(#stock.volume, :delimiter => ',') %>
</p>
<p>
<strong>Market Cap</strong>
$<%= number_with_delimiter(#stock.market_cap, :delimiter => ',') %>
</p>
</div>
Github Repo: https://github.com/Maikkeru/Stockmarketapp
I assume that am wielding the stock object incorrectly but I don't know where that slipup is occurring. Thanks for your time.
I get this error when trying to show <%= #schedule.title %>, but when I do the same with <%= #lesson.title %> it is working fine for #lesson.
![undefined method `title' for nil:NilClass
]1
The flow is like this.
- A user signs up and creates a clinic, the clinic belongs to the user.
- A clinic has many practitioners and they belongs to the clinic.
- A practitioner has many lessons and schedules, and they belongs to the practitioner.
When I'm on the lesson show page, there is a link to a booking page. It's on this booking page that the error occours.
I know it's saying nil, but both lessons and schedules have been created for that specific practitioner.
Anybody knows why this is happening? I don't understand why I can access #lesson but not #schedule. Any help would be much appreciated.
routes.rb
resources :clinics do
resources :practitioners do
resources :lessons, :lesson_payments, :schedules do
resources :bookings do
end
end
end
end
schedules_controller.rb
class SchedulesController < ApplicationController
before_action :set_schedule, only: [:show, :edit, :update, :destroy]
# GET /schedules
# GET /schedules.json
def index
#schedules = Schedule.all
end
# GET /schedules/1
# GET /schedules/1.json
def show
end
# GET /schedules/new
def new
#schedule = Schedule.new
end
# GET /schedules/1/edit
def edit
end
# POST /schedules
# POST /schedules.json
def create
#schedule = Schedule.new(schedule_params)
respond_to do |format|
if #schedule.save
format.html { redirect_to clinic_practitioner_schedule_path(id: #schedule.id), notice: 'Schedule was successfully created.' }
format.json { render :show, status: :created, location: #schedule }
else
format.html { render :new }
format.json { render json: #schedule.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /schedules/1
# PATCH/PUT /schedules/1.json
def update
respond_to do |format|
if #schedule.update(schedule_params)
format.html { redirect_to clinic_practitioner_schedule_path(#schedule), notice: 'Schedule was successfully updated.' }
format.json { render :show, status: :ok, location: #schedule }
else
format.html { render :edit }
format.json { render json: #schedule.errors, status: :unprocessable_entity }
end
end
end
# DELETE /schedules/1
# DELETE /schedules/1.json
def destroy
#schedule.destroy
respond_to do |format|
format.html { redirect_to clinic_practitioner_schedules_url, notice: 'Schedule was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_schedule
#schedule = Schedule.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def schedule_params
params.require(:schedule).permit(:title, :start, :end, :practitioner_id, :account_id)
end
end
bookings_controller.rb
class BookingsController < ApplicationController
before_action :set_lesson, only: [:new]
def new
#account = Account.new
#user = User.new
#patient = Patient.new
#booking = Booking.new
#lesson_payment = LessonPayment.new
#schedule = Schedule.find_by_id(params[:id])
end
def create
create_patient_charge
create_patient_account
#user = User.new(user_params)
#user.account_id = #account.id
respond_to do |format|
if #user.save
create_patient_profile
create_patient_booking
create_patient_lesson_payment
auto_login(#user)
UserMailer.new_signup_booking_admin(#user, #booking).deliver_later
UserMailer.new_signup_booking_client(#user, #booking).deliver_later
format.html { redirect_to dashboard_url, notice: 'Your account was successfully created.' }
format.json { render :show, status: :created, location: #user }
else
format.html { redirect_back fallback_location: root_path, alert: 'An error occurred while sending this request.' }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
private
def set_lesson
#lesson = Lesson.find(params[:lesson_id])
end
def user_params
params.require(:user).permit(:email, :password, :time_zone)
end
def create_patient_account
#account = Account.new()
#account.status = 'active'
#account.account = 'prefix-' + SecureRandom.hex(4)
#account.account_type = 'client'
#account.save
end
def create_patient_profile
#patient = Patient.new()
#patient.firstname = params[:user][:patient][:patient_first_name]
#patient.lastname = params[:user][:patient][:patient_last_name]
#patient.phone = params[:user][:patient][:patient_phone]
#patient.user_id = #user.id
#patient.account_id = #user.account_id
#patient.save
end
def create_patient_booking
#lesson = Lesson.find(params[:user][:booking][:lesson_id])
#booking = Booking.new()
#booking.lesson_id = params[:user][:booking][:lesson_id]
#booking.schedule_id = params[:user][:booking][:schedule_id]
#booking.patient_id = #patient.id
#booking.account_id = #user.account_id
#booking.title = #lesson.title
#booking.cost = #lesson.cost
#booking.status = 'Booked'
#booking.save
#schedule = Schedule.find(params[:user][:booking][:schedule_id])
#booking.practitioner_id = #schedule.practitioner_id
#booking.start = #schedule.start
#booking.refunded = 0
#booking.save
#schedule.title = 'Booked'
#schedule.save
end
def create_patient_lesson_payment
#lesson_payment = LessonPayment.new()
#lesson_payment.status = 'Paid'
#lesson_payment.date = Date.today
#lesson_payment.cost = #lesson.cost
#lesson_payment.service = #lesson.title
#lesson_payment.booking_id = #booking.id
#lesson_payment.account_id = #user.account_id
#lesson_payment.save
end
end
This is where I link to the booking page
show.html.erb
<p id="notice"><%= notice %></p>
<%= link_to new_clinic_practitioner_lesson_booking_path(:lesson_id => #lesson.id), class: "course-btn" do %><i class="fa fa-calendar-plus-o"></i> Book This Lesson<% end %>
<p>
<strong>Image:</strong>
<%= #lesson.image %>
</p>
<p>
<strong>Title:</strong>
<%= #lesson.title %>
</p>
<p>
<strong>Duration:</strong>
<%= #lesson.duration %>
</p>
<p>
<strong>Cost:</strong>
<%= #lesson.cost %>
</p>
<p>
<strong>Category:</strong>
<%= #lesson.category %>
</p>
<p>
<strong>Language:</strong>
<%= #lesson.language %>
</p>
<p>
<strong>Level:</strong>
<%= #lesson.level %>
</p>
<p>
<strong>Description:</strong>
<%= #lesson.description %>
</p>
<p>
<strong>Practitioner:</strong>
<%= #lesson.practitioner_id %>
</p>
<p>
<strong>Account:</strong>
<%= #lesson.account_id %>
</p>
<%= link_to 'Edit', edit_clinic_practitioner_lesson_path(#lesson) %> |
<%= link_to 'Back', clinic_practitioner_lessons_path %>
new.html.erb
<h1>New Booking</h1>
<%= render 'form', booking: #booking %>
<%= link_to 'Back', clinic_practitioner_lesson_bookings_path %>
_form.html.erb
<table class="table table-bordered">
<thead>
<tr>
<th>Slots In The User's Time Zone</th>
<th>Price</th>
<th>Service Provider</th>
<th>Booking Button</th>
</tr>
</thead>
<tbody>
<tr>
<td><%= #lesson.title %></td>
<td><%= #schedule.title %></td>
</tr>
</tbody>
</table>
I am noticing that in you are initializing #schedule like this in your bookings_controller new method:
#schedule = Schedule.find_by_id(params[:id])
But, you don't have any params[:id] in your parameters that are:
{'clinic_id'=>'50','practitioner_id'=>'27','lesson_id'=>'15'}
This is why your #schedule is nil
Assuming, there is a has_many association between practitioner and schedule, and you want to display the title of first schedule of the practitioner, you can do it it like this:
#schedule = Schedule.find_by_practitioner_id(params[:practitioner_id])
I work with Device and just added the 'wicked' gem to my Rails app. In my RegistrationsController I have defined the following:
class Users::RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
super
end
def update
super
end
protected
def after_sign_up_path_for(resource)
user_steps_path
end
def after_update_path_for(resource)
new_user_profile_path(current_user.id)
end
end
Basically I wish my form to have one further step after sign up, which would be the address:
class UserStepsController < ApplicationController
include Wicked::Wizard
steps :address
def show
#user = current_user
render_wizard
end
end
After the User has given his address, I want him/her to be redirected to their profile, where they can also give the information about themselves. My thinking, this is an update action in my RegistrationsController. Or how do I redirect to the profile, after my multistep form is finilised, meaning the step address is done? Here is address.html.erb:
<%= form_for #user, url: wizard_path do |f| %>
<div class="field">
<%= f.label :street %>
<%= f.text_area :street %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
For now ActionContoller complains about my header routes and looks for id address and I don't get why... Here is my ProfilesContorller:
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
def show
#user = User.eager_load(:profile).find(params[:user_id])
#profile = #user.profile
#review = Review.new
#reviews = Review.where(profile: #profile)
end
def new
#user = current_user
#profile = Profile.new
end
def edit
#profile = #user.profile
end
def create
#user = current_user
#profile = #user.build_profile(profile_params)
respond_to do |format|
if #profile.save
format.html { redirect_to user_profile_path(current_user.id), notice: 'Profile was successfully created.' }
format.json { render :show, status: :created, location: #profile }
else
format.html { render :new, notice: 'Did not save' }
format.json { render json: #profile.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #profile.update(profile_params)
format.html { redirect_to user_profile_path(current_user.id), notice: 'Profile was successfully updated.' }
format.json { render :show, status: :ok, location: #profile }
else
format.html { render :edit }
format.json { render json: #profile.errors, status: :unprocessable_entity }
end
end
end
def destroy
#profile.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'Profile was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_profile
#profile = current_user.profile
end
def profile_params
params.permit(:about, :avatar)
end
end
And here is the error part in my header.html.erb
<% if current_user.profile == nil %>
<li><span class="bg-primary text-white rounded"><%= link_to 'Create profile', new_user_profile_path%></span></li>
<% else %>
<li><span class="bg-primary text-white rounded"><%= link_to 'My profile', user_profile_path(#user) %></span></li>
<% end %>
<li><span class="bg-primary text-white rounded"><%= link_to 'Log out', destroy_user_session_path, method: :delete %></span></li>
ActionController basically complains about the second line and the "new_user_profile_path". How did address got into profiles contoller -> new -> id and how do I procees with the error mentioned in the title. Thank you!
As per your Error, user_id is missing in your route
Try, new_user_profile_path(#user) in second line
So finally it should be <%= link_to 'Create profile', new_user_profile_path(#user)%>
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
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| %>