How to set name of class in .yml in Rails - ruby-on-rails-4

I have an_errors.html.erb that takes an object and has
<%= object.class.name %>.
It is called by this in a _form.html.erb:
<%= render 'shared/errors', object: #product %>
What should I write in my xx.yml file to find the translation?
I have
models:
product: Produkt
which doesn't do the job.
Any clues?
Cheers Carl

I'm not quite clear on what you are going after, however, the following might work.
In your YAML file:
models:
product: 'Product'
In your ERB view:
# The following assumes "object" in your original post relates to the string (Product) in the YAML file
<%= object.constantize %> # this outputs the details of your class
<%= object.constantize.to_s %> # would be a string representation of your class (i.e., 'Product')
API docs for Constantize
Again, was not 100% sure what you were looking to accomplish but wanted to offer the suggestion in the event it helps!

Related

rails4-autocomplete with belongs to association

I want to implement autocomplete via rails4-autocomplete
Rails 4.2.4
Here is the controller
app/controllers/samples_controller.rb
class SamplesController < ApplicationController
autocomplete :patient, :code
Here is the route file,
config/routes.rb
resources :samples do
get :autocomplete_patient_code, on: :collection
end
And that's the view
app/views/samples/_form.html.erb
<div class="field">
<%= f.label :patient_code %><br>
<%= f.autocomplete_field :patient_id, autocomplete_patient_code_samples_path %>
</div>
With this code I mange to get the autocomplete
However I get invalid foregin key error when try to save the sample that's because the patient's code is passed to the foregin key instead of ID. How do I fix this?
Here is the request Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"blabla",
"sample"=>{"patient_id"=>"A123",
"commit"=>"Create Sample"}
Get "/samples/autocomplete_patient_code?term=A12" returns
{"id":"15","label":"A123","value":"A123"}]
After reading the GitHub documentation of rails4-autocomplete, I devised the following solution:
Add attr_accessor :patient_name to your Sample model and modify the form as follows:
...
<%= f.autocomplete_field :patient_name, autocomplete_patient_code_samples_path, id_element: '#patient_id' %>
<%= f.hidden_field :patient_id, id: 'patient_id' %>
...
With this change whenever you select any patient name, that patient's ID will be updated on the hidden field and it will be submitted as patient_id.
Hope this solves your problem.
Source: Github

Accessing class variables in views

What is the correct syntax for storing something in my class variable from a view ? Let's say that this is the code:
Controller:
class FooController < ApplicationController
##myvar = "I'm a string"
*
*
*
methods
*
*
*
end
Form:
<%= form_for(#something) do |f| %>
<%= f.text_field :myvar %>
<%= f.submit %>
<% end %>
This is just an example of a form as it is not functional, I cannot find what the correct syntax is for accesssing ##myvar from the view. Thanks !
as on request i edited, including attr_accessor
Rails way. Ill just fly over, hope you get it. you defenitely need to read more introductions to rails and its concept.
you have a rails-model, lets call it Animal
class Animal < ActiveRecord::Base
attr_accessor :non_saved_variable
end
this is having a database-table, lets say in this table we store race, name and age.
now we need a controller, to create/edit/update/delete animals
class AnimalController < ActionController::Base
def new
# this creates a new animal with blank values
#animal = Animal.new
end
end
now you need to go into your routes.rb and create a route for animals
resources :animal
this will create all (restful) routes for every actions of an animal.
now you need to have your template to render a form
form_for is a rails helper, to create a form, associated with the #animal (which is a new Animal). You pass |f| into the block, so with f you can access the form
=form_for #animal do |f|
then you can go for each field you need to call another rails helper
you can also access attr_accessors.
=f.text_field :race
=f.text_field :name
=f.text_field :age
=f.text_field :non_saved_variable
by that you get the stuff
dont fortget f.submit because your form needs a submit button
if you now click on your button, the form will be posted to the create method of rails. so you need to bring it into your controller
def create
# create a new animal with the values sended by the form
#animal = Animal.new params[:animal]
# here you can access the attr_accessor
#animal.do_something if #animal.non_saved_variable == "1337"
if #animal.save
# your animal was saved. you can now redirect or do whatever you want
else
#couldnt be saved, maybe validations have been wrong
#render the same form again
render action: :new
end
end
i hope that gave you a first insight of rails ?!
DO NOT DO THIS
You can get or set class variables from any class by:
<%= FooController.class_variable_get(:##myvar) %>
<%= FooController.class_variable_set(:##myvar, 'value') %>
This is probably not what you want. Do not do it. What are you trying to achieve?
DO THIS INSTEAD:
If it's a variable that you want to be available to all actions in that controller consider an instance variable set in a a before filter:
class FooController < ApplicationController
before_filter :set_var
private
def set_var
#my_var = "I'm a string"
end
end
Then in your view, just call <%= #my_var %>

trying to link image to specific model id in rails

I'm having a routing issue with an image. In my app I have images of items on the home page. I would like them to link to their image page.
Here is what my items controller looks like:
class ItemsController < ApplicationController
def show
#item = Item.find(params[:id])
end
end
This is what I have in my routes:
Rails.application.routes.draw do
resources :items
end
And this is what I have in the item partial:
<%= link_to(image_tag(item.image.url(:thumb)), item_path(:id)) %>
What I expected after reading the rails routing guide was that this would link to the item page for that image. Here is their example:
photo_path(:id) returns /photos/:id (for instance, photo_path(10) returns /photos/10)
I should also add that this is in my home page controller:
class StaticPagesController < ApplicationController
def home
#items = Item.where.not(category_id: 1)
end
However, that is not working. I've tried several different things, but all produce errors. Is there a simple way to do this?
The normal way to do what you want is this:
<%= link_to item_path(item) do %>
<%= image_tag(item.image.url(:thumb)) %>
<% end %>
You can just pass the instance of the item to item_path and also if you have complicated html for a link, it is usual to put it in a block for the link as shown here (with link_to something do).

Rails 4 Linking to records in mailer

I have built a mailer that notifies users of a change to a page they are tracking that contains product info. I cannot seem to get the link to the page correct. In this case,
<%= link_to #page_update.page.product.name, pages_url(#page_update.page) %>
I end up with a link to http://www.mydomain.com/pages.123
When I am hoping for http://www.mydomain.com/pages/123
I can provide more info if necessary, but I imagine since I'm so new to this that there's something simple going on here (I hope).
You need to use page_url instead of pages_url
<%= link_to #page_update.page.product.name, page_url(#page_update.page) %>

Strong Parameters Issue - Undefined Method "model" for <Model>

I am having an issue that I can't nail down and the other related questions don't seem to ever encounter this issue. I have a Message model and I am trying to add a Message (I am writing this to test something with Faye). I encountered an issue with Rails 4 and strong parameters. I followed the steps in the documentation to fix it but I am getting this error:
NoMethodError: undefined method `message' for #Message:0x007fc081202968>
Here is my controller and the section of the documentation where it directs you to do strong parameters this way (http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters):
class MessagesController < ApplicationController
def index
#messages = Message.all
end
def create
#message = Message.create!(message_params)
end
private
def message_params
params.require(:message).permit(:content)
end
end
Here is my model, very bare bones at this point:
class Message < ActiveRecord::Base
validates_presence_of :message
end
This is my form that I am submitting it with:
<%= form_for Message.new, remote: true do |f| %>
<%= f.text_field :content %>
<%= f.submit "Post" %>
<% end %>
I cannot figure out where the method "message" is being called on the message model. It says it is in the create action, I go there and it links to the strong parameters private method. I don't see how that is call "message" as the method anywhere. I am at a loss. I did binding.pry and walked through it step by step but I can't find where it is calling it there and it still fails when I step through with binding.pry.
Any help would be appreciated. I am guessing it is something obvious that I am overlooking at this point.
EDIT: add link to docs and change language surrounding my use of binding.pry
Found the problem, and it was obvious and ridiculous. In the validation I put the :content field is :message.