I've been searching and searching but I cannot find anything to help me with this. I am building an app that allows you to schedule a meeting in a room. The error I'm receiving is
undefined method 'room_id' for #<Room:0x007fa25cc51128>
Here is where the error is occuring in my application.html.erb:
<li><%= link_to "Schedule a Meeting", new_room_meeting_path(#user, #meeting, #room.room_id)%></li>
Here is my meetings controller:
class MeetingsController < ApplicationController
before_action :authenticate_user!
def new
#meeting = Meeting.new
#rooms = Room.all
#room = Room.find(params[:room_id])
end
def index
#meetings = Meeting.all
end
def show
#meeting = Meeting.find(params[:id])
#comments = #meeting.comments
#room = Room.find(params[:id])
end
def create
#user = User.find(params[:user_id])
#meeting = #user.meetings.create(meeting_params)
NotificationMailer.meeting_scheduled(#meeting).deliver_now
if #meeting.save
redirect_to root_path, flash: { notice: "Congratulations!!! Your meeting has been scheduled successfully!!!"}
else
render :new
end
end
private
def meeting_params
params.require(:meeting).permit(:name, :start_time, :end_time, :user_id, :room_id)
end
end
Here is my Meeting model:
require 'twilio-ruby'
class Meeting < ActiveRecord::Base
belongs_to :user
belongs_to :room
has_many :comments
validates_presence_of :user_id, :room_id, :name
def meeting_author_email
user.try(:email)
end
def self.send_reminder_text_message(body, phone)
#account_sid = ENV['twilio_account_sid']
#auth_token = ENV['twilio_auth_token']
#from_phone_number = ENV['twilio_phone_number']
#twilio_client = Twilio::REST::Client.new(#account_sid, #auth_token)
#twilio_client.account.messages.create( to: phone,
from: #from_phone_number,
body: body
)
end
def start_timestamp
(start_time - 6.hours).strftime('%b %e, %l:%M %p')
end
def end_timestamp
(end_time - 6.hours).strftime('%b %e, %l:%M %p')
end
end
The correct URI is: /rooms/:room_id/meetings/new(.:format)
I don't know what the problem is and it is really frustrating me. Any help would be greatly appreciated. I've searched over and over and have been unable to resolve this.
Thanks.
Your Room model doesn't have a column called room_id, but it does have a column called id. Every object in your application has an automatically generated id column as part of the "magic" of Rails. The room_id column that you seem to want belongs to the meetings table, but you indicated that it should come from your room object.
Replace the line:
Schedule a Meeting", new_room_meeting_path(#user, #meeting, #room.room_id)%></li>
with:
Schedule a Meeting", new_room_meeting_path(#user, #meeting, #meeting.room_id)%></li>
Since you stated the URI is "/rooms/:room_id/meetings/new(.:format)", the first part of the fix is to use the :id attribute instead of the: room_id attribute on your #room object, and second to remove the unnecessary objects from your route. You only need the id attribute for a valid URI:
<li><%= "Schedule a Meeting", new_room_meeting_path(#room.id)%></li>
Turns out I was calling the wrong URI.. The correct one was:
<li><%= link_to "Schedule a Meeting", new_user_meeting_path(current_user) %></li>
Is what did the trick. I also made the mistake of not dealing with seed data properly which really threw me for a loop. So once again, THANKS again to all who helped me out. I was working on an old project so I had a lot to go over.
Related
I have two models. First is Taxirecord and second is Carpark. Each Taxirecord may have its own Carpark. I have a problem with passing taxirecord_id to Carpark record. I have route
car_new GET /taxidetail/:taxirecord_id/carpark/new(.:format) carparks#new
And i want to pass :taxirecord_id, which is id of taxirecord that im editing, to my create controller.
My carpark model:
class Carpark < ActiveRecord::Base
belongs_to :taxirecord
end
In controller im finding taxirecord_id by find function based on param :taxirecord_id, but id is nil when create is called. Can you please help me to find out what Im doing wrong and how Can I solve this problem? Thanks for any help!
My carpark controller
class CarparksController < ApplicationController
def new
#car = Carpark.new
end
def create
#car = Carpark.new(carpark_params, taxirecord_id: Taxirecord.find(params[:taxirecord_id]))
if #car.save
flash[:notice] = "Zaznam byl ulozen"
redirect_to root_path
else
flash[:notice] = "Zaznam nebyl ulozen"
render 'new'
end
end
private def carpark_params
params.require(:carpark).permit(:car_brand, :car_type, :driver_name, :driver_tel)
end
end
I finally get it work
Ive added <%=link_to 'New Carpark', {:controller => "carparks", :action => "new", :taxirecord_id => #taxi_record.id }%>
to my taxirecord form and to carpark form <%= hidden_field_tag :taxirecord_id, params[:taxirecord_id] %>
And to my carpark controller : #carpark.taxirecord_id = params[:taxirecord_id]
Thanks everyone for great support and help!
I'd lean towards using something like:
before_action :assign_taxirecord
...
private
def assign_taxirecord
#taxirecord = TaxiRecord.find(params[:taxirecord_id])
end
And then in the create action:
def create
#car = #taxirecord.build_carpark(carpark_params)
...
end
Obviously there's a little tailoring needed to your requirements (i.e. for what actions the before_action is called), but I hope that helps!
No need to send taxirecord id.
class Carpark < ApplicationRecord
belongs_to :taxirecord
end
class Taxirecord < ApplicationRecord
has_one :carpark
end
Rails.application.routes.draw do
resources :taxirecords do
resources :carparks
end
end
for new taxirecord
t = Taxirecord.new(:registration => "efgh", :description =>"test")
for new carpark
t.create_carpark(:description=>"abcd")
#=> #<Carpark id: 2, taxirecord_id: 2, description: "abcd", created_at: "2017-10-12 10:55:38", updated_at: "2017-10-12 10:55:38">
I'm trying to redirect users to the next instance of my WordExposition model after update. What I have currently works for immediately-adjacent word_exposition id's, but raises RecordNotFound if the next lesson's word_exposition's ID skips (i.e. it will redirect properly between id's 1-4, but will break if the next id is 6). How can I get it to redirect also for those non-adjacent WordExposition instances that belong to the same lesson?
I based the next_exposition model method on the ideas from this post, but I'm missing something to get it to work here.
WordExposition model:
class WordExposition < ActiveRecord::Base
belongs_to :enrollment
belongs_to :word
def next_exposition
WordExposition.where(["id > ? AND enrollment_id = ?", id, enrollment_id]).first
end
end
WordExpositions controller:
class WordExpositionsController < ApplicationController
def update
current_word_exposition
#current_word_exposition.completed = true
#current_word_exposition.term_given_by_student = params[:word_exposition][:term_given_by_student]
if #current_word_exposition.save
flash[:notice] = "Congratulations!"
#currently only redirects correctly for adjacent words in the same lesson, should do so for non-adjacent word_expositions in the same lesson
if next_word = #current_word_exposition.next_exposition
redirect_to lesson_word_exposition_path(current_lesson, next_word)
end
else
flash[:alert] = "Enter the word exactly as shown!"
redirect_to lesson_word_exposition_path(current_lesson, current_word_exposition)
end
end
private
helper_method :current_lesson
def current_lesson
#current_lesson ||= Lesson.find(params[:lesson_id])
end
helper_method :current_enrollment
def current_enrollment
#current_enrollment ||= Enrollment.find_by!(lesson_id: params[:lesson_id], user_id: current_user.id)
end
def word_exposition_params
params.require(:word_exposition).permit(:completed)
end
helper_method :current_word_exposition
def current_word_exposition
#current_word_exposition ||= current_enrollment.word_expositions.find_by!(word_id: params[:id])
end
end
You can try this
def next_exposition
WordExposition.where('id = (select min(id) from word_expositions where id > ?)', self.id).first
end
When I write a message and when pressing the send option,
I want to store student_id, coach_id and message to the database. student_id and coach_id are being saved, but the message field is not being saved. It shows null in the database. How do I fix this?
Any help is appreciated.
Controller file:
class CourseQueriesController <ApplicationController
def index
#course_query = CourseQuery.new
end
def create
# #course_query = CourseQuery.new(course_query_params)
#course_query = CourseQuery.where(student_id: current_student.id, coach_id: "2", message: params[:message]).first_or_create
if #course_query.save
redirect_to course_queries_path, notice: 'Query was successfully send.'
else
render :new
end
end
private
def set_course_query
#course_query = CourseQuery.find(params[:id])
end
# def course_query_params
# params[:course_query].permit(:message)
# end
end
model/course_query.rb:
class CourseQuery < ActiveRecord::Base
belongs_to :student
belongs_to :coach
end
view/course_query/index.html.erb:
<%= simple_form_for (#course_query) do |f| %>
<%= f.button :submit , "Send or press enter"%>
<%= f.input :message %>
<% end %>
database /course_queries:
It seems you didn't permit :course_query.
Try to permit your params the following way:
def course_query_params
params.require(:course_query).permit(:message)
end
But according to the 2nd way you pass params (params[:message]) I think you have a bit different params structure. So try another one:
def course_query_params
params.permit(:message)
end
When you look into the params generated in the log, you will see that the message inside the course_query hash, so params[:message] should be params[:course_query][:message]
#course_query = CourseQuery.where(student_id: current_student.id, coach_id: "2", message: params[:course_query][:message]).first_or_create
I have a piece of code in Rails,
def create
#registration = Registration.new(registration_params)
if #registration.save
redirect_to #registration.paypal_url(registration_path(#registration))
else
render :new
end
end
I took it from tutorial. But I need just in this line:
#registration.paypal_url(registration_path(#registration))
Now, about my own controller, feed_controller, where
def create
#feed = Feed.new(check_params)
end
In the view erb file I put:
#feed.paypal_url(feed_path(#feed))
In my feed.rb (model):
def paypal_url(return_path)
values = {
business: "merchant#gotealeaf.com",
cmd: "_xclick",
upload: 1,
return: "#{Rails.application.secrets.app_host}#{return_path}",
invoice: id,
amount: course.price,
item_name: course.name,
item_number: course.id,
quantity: '1'
}
"#{Rails.application.secrets.paypal_host}/cgi-bin/webscr?" + values.to_query
end
Rake routes:
feed GET /:locale/feed(.:format) feed#index
feed#create POST /:locale/feed/create(.:format)
feed#new feed_new GET /:locale/feed/new(.:format)
feed#destroy feed_destroy GET /:locale/feed/destroy(.:format)
feed#edit feed_edit GET /:locale/feed/edit(.:format)
feed#update feed_update GET /:locale/feed/update(.:format)
But it prints the next error:
undefined method `paypal_url' for <#Feed::ActiveRecord_Relation:0x007fee24f5fc98>
How can I fix it? What is the problem?
UPDATE
def index
#current_user_is = current_user.email
session[:email] = #current_user_is
session[:id] = current_user.id
unless (current_user.member.present?)
#member = Member.new(:user_id => current_user.id)
#member.save()
redirect_to '/feed'
else
#new_feed = Feed.new
#feed = Feed.where(:member_id => current_user.member.id)
#category = Category.all
render 'home/uploads'
end
end
Simply use def self.paypal_url(return_path) instead of def paypal_url(return_path).
Explanation
You ran into your problem by defining a Class Method instead of an Instance Method, there's other posts discussing this.
The basic difference is, when defining:
def self.get_some_url
# code to return url of an instance
end
you can easily get the desired url of any objects, as in a view:
<% #feeds.each do |feed| %>
<%= feeds.get_some_url %>
<% end %>
Now calling Feed.get_some_url on the class would make no sense. Which url of the thousands would it call?
But there is a lot of use for class methods (where you define the method without self as you did)
def get_top_5
# code to return the top 5 most viewed feeds
end
Since this has nothing to do with a single instance, you define it for the entire Class. Leading to this call: Feed.get_top_5, which makes perfectly sense.
The second problem was not understanding the difference between where & find, this post will help you out with that.
I have a small encyclopedia of articles with my Article.rb:
class Article < ActiveRecord::Base
attr_accessible :name, :content
end
I now want to automatically link within the articles if I find text in one article that corrisponds to the name of another article. E.g. in the article named "Example One" the content is "You can also check Example Two for further reading." On save of "Example One" I want to set a link to the article "Example Two". My approach is to add to Article.rb
class Article < ActiveRecord::Base
attr_accessible :name, :content
before_save :createlinks
def createlinks
#allarticles = Article.all
#allarticles.each do |article|
self.content = changelinks(self.content)
end
end
def changelinks(content)
content = content.gsub(/#{article.name}/, "<%= link_to '#{article.name}', article_path(article) %>")
end
My articles_controller is:
def update
#article = Article.find(params[:id])
if #article.update_attributes(params[:article])
redirect_to admin_path
else
render 'edit'
end
end
But obviously there is an error refering to the line content = content.gsub(etc...):
NameError in ArticlesController#update
undefined local variable or method `article' for #
How can I fix this so that it checks all other article names and creates the links for the current article I want to save?
Your changelink method does not "know" what is the article variable. You have to pass it as an argument:
def createlinks
#allarticles = Article.all
#allarticles.each do |article|
self.content = changelinks(self.content, article)
end
end
def changelinks(content, article)
content = content.gsub(/#{article.name}/, "<%= link_to '#{article.name}', article_path(article) %>")
end
But this way to implement links instead of the Articles' name is not the best in my opinion.