How to call routing helper inside a module? [closed] - ruby-on-rails

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
i have a module like this, where "partners_url" is a routing helper method
module A
def B
....partners_url
end
end
but i get this
NoMethodError - undefined method
How can i call that url helper in side a module?, thanks.

Put this line under your module declaration
include Rails.application.routes.url_helpers
Read about other options here

Related

ruby on rails beginner: rails for zombies code sample [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
where does that #user in the code in check_ammo come from?
see code for "weapon" in model, controller, mailer and schema.rb
rails for zombies code
That code cant be right.
Weaponmailer
def low_ammo(weapon, zombie)
attachments["weapon.jpg"] = weapon.picture_file
mail to: zombie.email, subject: "#{weapon.name} has low ammo"
end
expects 2 parameters in controller
WeaponMailer.low_ammo(#user).deliver
#user is not defined unless its defined in ActiveRecord::Base what is not the case i think. Its just broken code example

Resolve Fixnum error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a parameter (params[:authorization]) that comes from the URL, as you can see:
authorization_selected = params[:authorization]
new_parcel = params[:new_parcel].to_i
puts authorization_selected.class (in the console show type String)
puts new_parcel.class (in the console show type Fixnum)
In my controller, have:
#portability = Portability.new
#portability.employee_id = authorization_selected.employee_id
However this returns an error:
undefined method `employee_id' for 3:Fixnum
I need that both was integer. How do it?
You are calling the employee_id method on authorization_selectedwhich is a String and does not provide this method.
Obviously this does not work. You probably want to do
#portability = Portability.new
#portability.employee_id = authorization_selected
assuming that params[:employee] contains the employee_id and Portability is an ActiveModel or an ActiveRecord.
Perhaps you can change your form that the value can be assigned through the initializer?

How do I access data in the view from my controller? Rails [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have data I'm getting in my controller and want to display it in the view. I've tried using a helper method but no luck. I've also tried an instance variable but still doesn't work.
You can do so like this :
class SomeClass < ApplicationController
def index
#something = 'This is a cool text'
end
end
In the index.html.erb you can do this :
<%= #something %>

How to get a ruby class when it's name is in a variable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
So I'm passing a string with the name of an activerecord model class into my method and I want to be able to call some methods on that class.
This is wrong:
def perform(body_class, body_id)
body = body_class.constantize
request = body.find(body_id)
end
Update - that actually works fine.
What error are you getting? Make sure the string is in the correct ActiveRecord format.
For example:
"Person".constantize # works
"person".constantize # does not work
To get the second one to work you should also add camelize
"person".camelize.constantize # works

What does this controller code do? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've come across this pattern a few times, but am not sure what it is, or how to Google it.
class ApiController < ApplicationController
class InvalidAppToken < RuntimeError ; end
class InvalidUserToken < RuntimeError ; end
...
end
It creates custom errors. Doing this you can raise your personalized errors, then rescue them to better handle the app errors.
class MyController < ApiController
begin
unless DoSomething(params[:user_token])
raise InvalidUserToken
[...]
end
rescue InvalidUserToken
# manage this error
end
end
Here is a good option for searching the web with special symbols considered (most major search engines ignore special characters in searches):
http://symbolhound.com/?q=ruby+%3C+runtimeerror
It simply defines custom types of errors. Could be useful to raise very specific errors in your workflow.
Have a look at this book

Resources