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
Related
I'm new to learning Rails 3 and working through a Q&A app tutorial. I'm just wondering why I can't do this (I get an error) in relating a particular answer to a question. It works for the current user...
class AnswersController < ApplicationController
before_filter :auth, only: [:create]
def create
#question = Question.find(params[:question_id])
**#answer = Answer.new(params[:answer])
#answer.question = #question
#answer.user = current_user**
if #answer.save
flash[:success] = 'Your answer has been posted!'
redirect_to #question
else
#question = Question.find(params[:question_id])
render 'questions/show'
end
end
end
The tutorial says that this is the correct way:
class AnswersController < ApplicationController
before_filter :auth, only: [:create]
def create
#question = Question.find(params[:question_id])
**#answer = #question.answers.build(params[:answer])**
#answer.user = current_user
if #answer.save
flash[:success] = 'Your answer has been posted!'
redirect_to #question
else
#question = Question.find(params[:question_id])
render 'questions/show'
end
end
end
Doing the following
#answer = #question.answers.build(params[:answer)
Is the same as doing this
#answer = Answer.new(params[:answer])
#answer.question_id = #question.id
Doing a build adds the relation attributes to the new answer, in this case question_id
As for the error, can you provide the type of error you receive?
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
I want to get all the microposts where 'something' = true.
This code works fine
class UsersController < ApplicationController
.
.
.
def show
#user = User.find(params[:id])
#microposts = #user.microposts
#titre = #user.nom
end
end
But when I tried to make a where sql method this code doesn't work.
class UsersController < ApplicationController
.
.
.
def show
#user = User.find(params[:id])
#microposts = #user.microposts.where("something = 'true'")
#titre = #user.nom
end
end
any idea ?
def show
#user = User.find(params[:id])
#microposts = #user.microposts.where(something: true)
#titre = #user.nom
end
See here or here for more info on narrowing the query scope.
Write like this:
Micropost.where(user_id: params[:id], something: true)
I am trying to write a forum with Ruby on Rails.
On model side, I finished association between Topic and Forum
# forum.rb
class Forum < ActiveRecord::Base
has_many :topics
attr_accessible :name, :description
end
# topic.rb
class Topic < ActiveRecord::Base
has_many :posts
belongs_to :forum
end
Controller for Forum
# forums_controller.rb
class ForumsController < ApplicationController
def new
#forum = Forum.new
end
def create
#forum = Forum.new(params[:forum])
if #forum.save
flash[:success] = "Success!"
redirect_to #forum
else
render 'new'
end
end
def index
#forums = Forum.all
end
def show
#forum = Forum.find(params[:id])
end
end
Controller for Topic
class TopicsController < ApplicationController
def new
#topic = current_forum???.topics.build
end
def create
#topic = Topic.new(params[:topic])
if #topic.save
flash[:success] = "Success!"
redirect_to #topic
else
render 'new'
end
end
def index
#topics = Topic.all
end
def show
#topic = Topic.find(params[:id])
end
end
How do I change new and create for topics_controller to make sure the topic is created for current forum rather than some other one?
So for example, if I create a new topic from a forum with id=1, how do I make sure that forum_id=1 for the new topic created?
Using nested resources
resources :forums do
resources :topics
end
you will have a path like
/forums/:forum_id/topics/new
then in your TopicsController
def new
#forum = Forum.find(params[:forum_id])
#topic = #forum.topics.build
end
class TopicsController < ApplicationController
def new
#forum = Forum.find(params[:id])
#topic = #forum.topics.build
end
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.