NoMethodError in PostsController#create undefined method `text' for #<Post:0x007f99088407f8> - ruby-on-rails

I've been having trouble getting past this error. It says that it 'text' is undefined.
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save # (this line is highlighted in the error)
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: #post }
else
format.html { render action: 'new' }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
This worked fine when I had originally 'rails generate' the scaffold with title:string body:text.
I've been stuck on this problem all day. Suggestions would be awesome.

Your view is accessing the attribute text, but it should be body

Related

"undefined method `title' for nil:NilClass" error in Ruby on Rails

I'm building a video website and I've been getting this error when I try to add a title to my static page where users can watch a video:
watch.html.erb
<div class="panel panel-default">
<div class="panel-body">
<div class="col-md-4">
<p>You're watching:</p>
<h1><%= #movie.title %></h1>
</div>
</div>
</div>
And here's my movies_controller.rb file:
class MoviesController < ApplicationController
before_action :set_movie, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def watch
end
def index
#movies = Movie.all
end
def show
#reviews = Review.where(movie_id: #movie.id).order("created_at DESC")
end
def new
#movie = current_user.movies.build
end
def edit
end
def create
#movie = current_user.movies.build(movie_params)
respond_to do |format|
if #movie.save
format.html { redirect_to #movie, notice: 'Movie was successfully created.' }
format.json { render :show, status: :created, location: #movie }
else
format.html { render :new }
format.json { render json: #movie.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #movie.update(movie_params)
format.html { redirect_to #movie, notice: 'Movie was successfully updated.' }
format.json { render :show, status: :ok, location: #movie }
else
format.html { render :edit }
format.json { render json: #movie.errors, status: :unprocessable_entity }
end
end
end
def destroy
#movie.destroy
respond_to do |format|
format.html { redirect_to movies_url, notice: 'Movie was successfully destroyed.' }
format.json { head :no_content }
end
end
def set_movie
#movie = Movie.find(params[:id])
end
private
def movie_params
params.require(:movie).permit(:title, :description, :movie_length, :director, :rating, :image)
end
end
My watch method is above "private" in my controller file, which I'm told is one of the primary reasons why this error shows up. But every time I try to go to the page, the error still appears. Can someone tell me what I'm doing wrong?
The error message is very clear: #movie is nil. Nowhere in your watch action do you try to define that variable as anything other than nil, and your before_action callback that populates #movie very clearly does not include watch in the list of actions for which it runs.

NameError at /events path - Undefined Local Variable or Method

I'm quite new to rails and I'm using grape to build an API rails app.
I've been getting this error message after I click on the 'Create Event' button :-
NameError at /events
undefined local variable or method `event_params' for #<EventsController:0x00000008f76400>
The error points to the controller file for events,
class EventsController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_user_from_token!
before_action :set_event, only: [:show, :edit, :update, :destroy]
def index
#events = Event.all
if user_signed_in?
if current_user.is_admin?
#events = Event.all
respond_to do |format|
format.html
format.json { render json: #events }
end
else
#events = current_user.events
respond_to do |format|
format.html
format.json { render json: #events }
end
end
else
render json: {}, status: :unauthorized
end
end
def new
#event = Event.new
end
def edit
end
def create
#event = current_user.events.new(event_params)
respond_to do |format|
if #event.save
format.html { redirect_to #event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: #event }
else
format.html { render :show, notice: 'You are not allowed to view this.' }
format.json { render json: {}, status: :unauthorized}
end
end
end
And here's the event_params method,
def event_params
params.require(:event).permit(:event_name, :event_description, :event_date, :event_time)
end
The log file shows,
Started POST "/events" for 127.0.0.1 at 2015-08-10 14:38:16 +0800
Processing by EventsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"yRVn4uuxpMVejs/gsG004PIi2JRaWnovGBm7TweGgkf2XpTLzjOZNGanCRVVKSPAV6JzNRWmDzRRbq/dC2KOKQ==", "event"=>{"event_name"=>"ddwq", "event_description"=>"rqreqreqr", "event_date(1i)"=>"2015", "event_date(2i)"=>"8", "event_date(3i)"=>"10", "event_time(1i)"=>"2015", "event_time(2i)"=>"8", "event_time(3i)"=>"10", "event_time(4i)"=>"06", "event_time(5i)"=>"38"}, "commit"=>"Create Event"}
[1m[36mUser Load (1.6ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1[0m [["id", 1]]
Completed 500 Internal Server Error in 34ms
NameError - undefined local variable or method `event_params' for #<EventsController:0x000000082a6540>:
app/controllers/events_controller.rb:37:in `create'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
...
...
bin/rails:4:in `<main>'
Started POST "/__better_errors/beb9cf3b65f0e36a/variables" for 127.0.0.1 at 2015-08-10 14:38:16 +0800
def create
#event = current_user.events.new(event_params)
respond_to do |format|
if #event.save
format.html { redirect_to #event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: #event }
else
respond_to do |format|
format.html { render :show, notice: 'You are not allowed to view this.' }
format.json { render json: {}, status: :unauthorized}
end
end ##You have missed the end here
And here's the event_params method,
def event_params
params.require(:event).permit(:event_name, :event_description, :event_date, :event_time)
end
Try build instead of new
replace
#event = current_user.events.new(event_params)
by
#event = current_user.events.build(event_params)`
The error message indicates that, event_params method is not available for the EventsController. I think you mistakenly put the event_params inside another method or missed an end to end the create method. It should be an instance method of your EventsController class.
This code should work as it is:
class EventsController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_user_from_token!
before_action :set_event, only: [:show, :edit, :update, :destroy]
def index
#events = Event.all
if user_signed_in?
if current_user.is_admin?
#events = Event.all
respond_to do |format|
format.html
format.json { render json: #events }
end
else
#events = current_user.events
respond_to do |format|
format.html
format.json { render json: #events }
end
end
else
render json: {}, status: :unauthorized
end
end
def new
#event = Event.new
end
def edit
end
def create
#event = current_user.events.new(event_params)
respond_to do |format|
if #event.save
format.html { redirect_to #event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: #event }
else
format.html { render :show, notice: 'You are not allowed to view this.' }
format.json { render json: {}, status: :unauthorized}
end
end
end
private
def event_params
params.require(:event).permit(:event_name, :event_description, :event_date, :event_time)
end
end
So, the error was a missing 'end' for show method which I didn't post that part.
I completely missed that part because the error was misleading and I assume that it has something to do with the event parameters since the error points there.

how do I write create method in the controller for a submission form that uses a join table?

I'm trying to create a form that allow called submits. I've got all the appropriate MVC created. I've then created a model called questions that works and am using active admin to allow admin users to add new questions to the form as they see fit. When I test submitting the form I get this error
undefined method `each' for nil:NilClass
#submit = Submit.new(submit_params)
#submit.save
params[:submit][:question_ids].each do |question_id|
#question = Question.find(question_id)
#submit.questions << #question
end
Here's my submits controller:
class SubmitsController < ApplicationController
before_action :set_submit, only: [:show, :edit, :update, :destroy]
def index
#submits = Submit.all
end
def show
end
def new
#submit = Submit.new
#questions = Question.all
end
def edit
end
def create
#submit = Submit.new(submit_params)
#submit.save
params[:submit][:question_ids].each do |question_id|
#question = Question.find(question_id)
#submit.questions << #question
end
respond_to do |format|
if #submit.save
format.html { redirect_to #submit, notice: 'Application was successfully created.' }
format.json { render :show, status: :created, location: #submit }
else
format.html { render :new }
format.json { render json: #submit.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #submit.update(submit_params)
format.html { redirect_to #submit, notice: 'Application was successfully updated.' }
format.json { render :show, status: :ok, location: #submit }
else
format.html { render :edit }
format.json { render json: #submit.errors, status: :unprocessable_entity }
end
end
end
def destroy
#submit.destroy
respond_to do |format|
format.html { redirect_to submits_url, notice: 'Submit was successfully destroyed.' }
format.json { head :no_content }
end
end
Here's my Submit and Question model:
Submit:
class Submit < ActiveRecord::Base
has_and_belongs_to_many :questions
belongs_to :user
end
Question:
class Question < ActiveRecord::Base
has_and_belongs_to_many :submits
end
I'm sure it's some kind of syntax error in my controller but I don't know what. Still pretty new to using join tables. Any help/explanation would be very appreciated.
Thanks!
You don't need this
params[:submit][:question_ids].each do |question_id|
#question = Question.find(question_id)
#submit.questions << #question
end
I don't see your submit params but if you add question ids to the permitted parameters then rails will build the entry in the join table on it's own.
def submit_params
params.require(:submit).permit(:user_id, question_ids: [])
end

undefined method `destroy' for nil:NilClass(rails)

I get this error:
NoMethodError in CycleRoadsController#destroy
undefined method `destroy' for nil:NilClass(rails).
This is code from controller and theyt include a method 'destroy ':
respond_to do |format|
if #cycle_road.save
format.html { redirect_to #cycle_road, notice: 'Cycle road was successfully created.' }
format.json { render action: 'show', status: :created, location: #cycle_road }
else
format.html { render action: 'new' }
format.json { render json: #cycle_road.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #cycle_road.update(cycle_road_params)
format.html { redirect_to #cycle_road, notice: 'Cycle road was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #cycle_road.errors, status: :unprocessable_entity }
end
end
end
# DELETE /cycle_roads/1
# DELETE /cycle_roads/1.json
def destroy
#cycle_road.destroy
respond_to do |format|
format.html { redirect_to cycle_roads }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cycle_road
#cycle_road = CycleRoad.find(params[:id])
end
def cycle_road_params
params.require(:cycle_road).permit(:name, :begin, :finish, :km, :description)
end
end
Anybody know, what's wrong?
Set the instance variable #cycle_road before calling destroy on it.
Currently its nil as the error clearly says undefined method 'destroy' for nil:NilClass.
As per the shared controller code, you would need to add destroy action in the before_action callback of set_cycle_road
class CycleRoadController < ApplicationController
before_action :set_cycle_road, only: [:show, :edit, :update, :destroy]
## ... ^
## Add this
end
This callback would take care of setting the #cycle_road instance variable before calling destroy action.
As the previous person mentioned it looks like #cycle_road is nil. Make sure that you have a before action:
class CycleRoadController < ApplicationController
...
before_action :set_cycle_road
...
And ensure that it runs for your destroy method. ie.
before_action :set_cycle_road, only: [:show, :edit, :update, :destroy]

Undefined local variable in a partial

I'm new to rails and am having a bit of trouble. I am getting an
undefined local variable or method `answer'
error in my _answer.html.erb partial.
Here is my answers_controller.rb:
class AnswersController < ApplicationController
before_action :set_answer, only: [:show, :edit, :update, :destroy]
def index
#question = Question.find params[:question_id]
#question.answers
end
def show
end
def new
#question = Question.find params[:question_id]
end
def edit
end
def create
#question = Question.find(params[:question_id])
#answer = #question.answers.create(answer_params)
respond_to do |format|
if #answer.save
format.html { redirect_to #comment, notice: 'Answer was successfully created.' }
format.json { render action: 'show', status: :created, location: #answer }
else
format.html { render action: 'new' }
format.json { render json: #answer.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #answer.update(answer_params)
format.html { redirect_to #answer, notice: 'Answer was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #answer.errors, status: :unprocessable_entity }
end
end
end
def destroy
#answer.destroy
respond_to do |format|
format.html { redirect_to answers_url }
format.json { head :no_content }
end
end
and my _answer.html.erb file:
<%=div_for(answer) do %>
<div class="questioncontainer">
<p>
<%= answer.body %>
</p>
</div>
<% end %>
If it matters, my resources :answers is nested in resources :questions.
I appreciate any help!
Try using div_for(#answer) instead of answer. When you're communicating between controllers and views, you always do so with #variables. Maybe you should take some time and read this: http://guides.rubyonrails.org/layouts_and_rendering.html

Resources