From travels, which belongs to offer, I'm trying to render a form generated with the mail_form gem.
But I get an error with this message:
*undefined method `model_name' for NilClass:Class*
travels/show
= render :partial => '/question_forms/form', :question_form => #question_form
Any hint how to get this? I've tried including the question_form model as an association to travel, but it doesn't work.
This is the question_forms_controller default code:
class QuestionFormsController < ApplicationController
def new
#question_form = QuestionForm.new
end
def create
begin
#question_form = QuestionForm.new(params[:question_form])
#question_form.request = request
if #question_form.deliver
flash.now[:notice] = 'Thank you for your message!'
else
render :new
end
rescue ScriptError
flash[:error] = 'Sorry, this message appears to be spam and was not delivered.'
end
end
end
In my travels_controller I tried to load #question_form using the same method, but this doesn't seem the way:
def new
#offer_season = OfferSeason.find(params[:offer_season_id])
#travel = #offer_season.travels.find(params[:id])
#question_form = #travel.questionforms.new
end
def create
#offer_season = OfferSeason.find(params[:offer_season_id])
#travel = #offer_season.travels.find(params[:id])
begin
#question_form = #travel.questionforms.new(params[:question_form])
#question_form.request = request
if #question_form.deliver
flash.now[:notice] = 'Thank you for your message!'
else
render :new
end
rescue ScriptError
flash[:error] = 'Sorry, this message appears to be spam and was not delivered.'
end
end
Related
I am trying to integrate Twilio with my Rails 4 app. I followed a tutorial, but I keep getting an error. Right now I am getting an Uninitialized Constant Error. I provided the code below. Thanks in advance.
Routes.rb
get '/share_over_sms' => 'listing_collections#share_over_sms'
Listing Collection Model
require "messenger"
def clean_number
client_number = self.client_number.scan(/\d+/).join
client_number[0] == "1" ? client_number[0] = '' : client_number
client_number unless client_number.length != 10
end
Messenger Module
module Messenger
def send_sms(number)
twilio_sid = "ENV['TWILIO_ACCOUNT_SID']"
twilio_token = "ENV['TWILIO_AUTH_TOKEN']"
#twilio_client = Twilio::REST::Client.new twilio_sid, twilio_token
from = '+1xxxxxxxxxx'
message = #twilio_client.account.sms.messages.create(
:from => from,
:to => "+1"+number,
:body => "This is a test message."
)
end
end
Listing Collections Controller
def share_over_sms
#client_phone = ClientPhone.new(listing_collection_params)
#client_phone.send_sms(#client_phone.clean_number)
redirect_to :back
end
In my RoR app, I'm passing flash messages using "notice" and "alert".
But, when I'm trying to pass like flash["warning"], for example, I receive: undefined local variable or method warning'
What can I do to be able to create a flash message passing 'warning' value?
Code:
#app/controllers/users/passwords_controller.rb
class Users::PasswordsController < Devise::PasswordsController
def create
user = params["user"]
if User.find_by_email(user["email"]).nil?
set_flash_message(:testing, :email_not_found) if is_navigational_format?
redirect_to root_path
else
self.resource = resource_class.send_reset_password_instructions(user)
if successfully_sent?(resource)
respond_with({}, :location => after_sending_reset_password_instructions_path_for(resource_name))
else
respond_with(resource)
end
end
end
protected
def after_sending_reset_password_instructions_path_for(resource_name)
root_path
end
end
#app/views/landing/index.html.erb
.
.
<h5><%= testing %></h5>
Any help? Thanks!
Try to use a symbol. flash[:warning] = "XYZ" for example.
in my Rails app in DB i have some query.
# PostController
#post.query # => 'blabla'
and i want put this string in google or yahoo
in simple ruby file i use
require ... all gems
Capybara.app_host = 'http://www.google.com/'
and
class Google
include Capybara::DSL
def search
visit('/')
fill_in "q", :with => "blah blah"
click_button("Google")
...
but how can I put this code in controller (or model method)?
Capybara is not intended for such use. You might want to try mechanize instead.
class PostsController < ApplicationController
require 'capybara'
require 'capybara/dsl'
include Capybara::DSL
Capybara.current_driver = :webkit
def index
#posts = Post.all
end
def show
#post = Post.find(params[:id])
visit(#post.search_type)
fill_in "q", :with => #post.query
click_button("Google")
loop_variable = 0
num_of_page = 1
max_count_of_page = 4
flag_on_break = false
while(loop_variable == 0) do
has_on_page = page.has_content?(#post.search_question)
if has_on_page
loop_variable = 1
else
find('.b:last a').click
num_of_page += 1
end
if num_of_page > max_count_of_page
flag_on_break = true
break
end
end
if flag_on_break
#number = 'Nothing not found'
else
#number = num_of_page
end
end
def new
#post = Post.new
end
def create
#post = Post.new(params[:post])
if #post.save
flash[:notice] = "Post created!"
redirect_to root_path
end
end
end
this is a piece of my code - but the main idea, I think, is understandable
I have a basic rails question where I need to save two associated objects.
The association is Rtake has_many :companies and Company belongs_to :rtake
def create
#rtake = RTake.new(:email => params[:contact_email])
#rtake.role = "PROVIDER"
#company = #rtake.companies.build(params[:company])
#company.rtake = #rtake
respond_to do |format|
if #company.save_company_and_rtake
format.html{ redirect_to admin_companies_url}
else
flash.now[:errors] = #company.errors.full_messages.join(", ")
format.html{ render "new" }
end
end
end
In my company.rb class I have
def save_company_and_rtake
status1 = self.save(:validate => false)
status2 = self.rtake.save(:validate => false)
status = status1 && status2
status
end
The problem I face is that the company.rtake_id remains nil. Ideally shouldn't the company.rtake_id get updated to the #rtake.id after save.
I know I am missing something basic. Would appreciate some help.
You shouldn't need this line:
#company.rtake = #invitation
#invitation is nil from what you've shown .
But also, when you built the #company, #rtake.id isn't set because it hasn't been saved.
#company = #rtake.companies.build(params[:company])
#company.rtake = #rtake
#rtake.companies.build(params[:company]) This already means #company.rtake == #rtake. it's redundent here.
I have report_controller where I have two objects, #report and #reporte, and I want to save both objects in database. When validation fails, I want to get fields populated in rendered form.
I can't use just #report = ReportMain.new(params[:report_main]) because I have two objects and just one params object.
I use exportnew action to show form, and encreate to save this form.
There is some simple way to get form populated?
ReportController:
class ReportController < ApplicationController
before_filter :authenticate_user!
before_filter :load
layout "application"
def load
#company = Company.find_by_id(current_user.company_id)
#date = Date.today
#report = ReportMain.new
#reporte = ReportE.new
end
def index
list
render("list")
end
def list
#reports = ReportMain.all
end
def exportnew
render("ennew")
end
def encreate
#report = ReportMain.new
#reporte = ReportE.new
#reportparam = params[:report_main]
#report.waste_id = params[:waste][:code]
#report.warehouse_id = Warehouse.find_by_user_id(current_user.id).id
#report.user_id = current_user.id
#report.company_id = current_user.company_id
#report.amount = #reportparam[:amount]
#report.isimport = false
#report.isfinished = false
#report.reportnumber = ReportMain.where(:company_id => current_user.company_id).count.to_i+1
if #report.save
#reporte.report_main_id = #report.id
else
#report_main = #report
render("etnew")
return
end
#reporte.vrstaotpada = params[:vrstaotpada]
#reporte.nacinpakovanja = params[:nacinpakovanja]
#reporte.ispitivanjebroj = #reportparam[:ispitivanjebroj]
#reporte.datumispitivanja = #reportparam[:datumispitivanja]
#reporte.q_pripadnost = #reportparam[:q_pripadnost]
#reporte.datumpredaje = #date
if #reporte.save
redirect_to(:action => 'show', :id => #reporte.id)
flash[:notice] = "Izveštaj je uspešno kreiran."
else
#report_main = #report
render("etnew")
end
end
def show
#report = ReportMain.find(params[:id])
#warehouse = #report.warehouse.name
end
end
View starts with (it's huge HTML):
<%= form_for(:report_main, :url => {:action => 'encreate'}) do |f| %>
Since you want your form to get populated after validation fails, I suggest make your from to submit through AJAX. There is a jQuery plugin which can help http://jquery.malsup.com/form/