How to define actionmailer subject - ruby-on-rails

Is there a way to define ActionMailer subjects in the email view erb?
Something like:
<%= set_subject("abc") %>
I would like to use class variable values in the subject through string interpolation and was wondering if there was a way to have all the email parameters in one place so I can edit them cleanly.

Add this line at the beginning of the view.
<% message.subject = "abc" %>

Related

Add arbitrary string to URLs in Rails/Middleman app

I'm using middleman to generate a static webpage. I need to add a consistent but understandable string to all urls so I can understand how users navigate on the page.
Now i do it like this
<% link_to '/'+?button=navigation , class: 'logotype', itemprop: 'url' do %>
...
<% end %>
I would prefer not having to manually add all the parameters but rather just use something that's already there, like a scope or something. I was thinking about using the name of the template file for example. The url is not unique enough.
Any suggestions?
The standard way of doing this would be to write a helper method that encapsulates your functionality:
<%= link_to_as_nav('/', class: 'logotype', ...) do %>
...
<% end %>
Then write a helper method:
def link_to_as_nav(url, options)
link_to(url + '?button=navigation', options)
end
This is the naïve approach and won't account for a url argument that already has parameters added, but that's something you can work to fix.

How to define a method for a model attribute

How do I define a method the attribute of a model.
I have a Picture model that has a title.
Picture.title = "Some title #with a few #hashtags"
I want to make those hashtags links to the tag#show
Picture.title.with_links = "Some title <%= link_to "#with", tag_path(tag) %> a few <%= link_to "#hashtags", tag_path(tag) %>"
Whats the best way to do this. Where do I define the method(with_links)? in Picture.rb? or Pictures_helper.rb?
In short, Rails model does not have access to routes. In very rare cases it is possible to use routes from within model, however in this case it is not the right place to do so.
The right place for with_links is PicturesHelper, so that it would be accessible in view via with_links(picture). The declaration would be:
def with_links(picture)
out= "Some title "
out += picture.tags.collect do |tag|
link_to(tag.name, tag_path(tag))
end.join(", ")
raw out
end

how to pass a static string into a link_to as an argument in ruby on rails view

I have a modified string. Here is the code where I do the changes:
<% device=#devices.find(1) %>
<% #string ="" %>
<% device.attributes.keys.each do |attribute| %>
<% next if attribute == 'id' || attribute== 'token' || attribute =='carrier' || attribute =='segment' || attribute =='created_at' || attribute =='updated_at' %>
<% x=attribute.to_s %>
<%#string = #string + x +":device."+x +"," %>
<% end %>
<% #string %>
<% #arguments= #string.gsub(/\,$/, '') %>
<%= #arguments %>
It works and it is in the right format to put it in the link_to helper.
This is how I first wrote the link_to helper, and it worked.
<td><%= link_to 'Send notification', controller: "home", action: "send_notification", token: device.token, first_name: device.first_name, last_name: device.last_name %></td>
I tried to change it like this:
<td><%= link_to 'Send notification', controller: "home", action: "send_notification", token: device.token, #arguments %></td>
or #{arguments}
But it doesn't work. I even created another variable without # but it didn't work either.
How can I paste my arguments?
This is my arguments string btw:
"first_name:device.first_name,last_name:device.last_name,nickname:device.nickname"
What should I change?
Another simple newbie question; I feel like I am doing most of the coding in the wrong place. Is it right thatI write so many things in view?
What is the best approach in Ruby on Rails programming?
Thanks in advance
I'll answer your code question first and leave the string question for last.
First, some of your code is in the wrong places. Rails expects you to retrieve the database record in the controller and then pass it into the view. Something like:
devices_controller.rb
class DevicesController < InheritedResources::Base
def send_notification
#device = Device.find(id)
end
....
Then in your show view (app/views/devices/send_notification.html.erb) you can use the #device object and access its attributes like #device.first_name and #device.last_name and print them out or whatever.
Second, the link_to method needs a Hash of arguments, not a string. But either way, there is no use case in Rails that I can think of for passing the entire set of object attributes into the link_to method. It's just generating a link. You probably don't really want it to be littering your html elements with every one one of your record attributes.
All you need to do if you want access to that data when the user clicks the link is to pass the id in as a url element and then have the controller at the other end of the link (in your case Home?) catch the id and create an object out of it there.
I'd suggest taking a good look at: http://guides.rubyonrails.org/

simple_form_for: Removing root model name from params

When I create a form using simple_form_for #model, upon submit the post params has all the attributes grouped under params[model]. How do I get simple_form to drop this grouping and instead send it directly (under params root)?
<%= simple_form_for #user, do |f| %>
<%= f.input :name %>
<%= f.input :password %>
<%= f.submit %>
Now the name and password attributes would normally be sent under params[:user][:name], params[:user][:password] etc. How do I get simple_form to post these as params[:name], params[:password] etc.?
Thanks!
Ramkumar
ps: In case you are wondering why I need this, the bulk of my app is to serve as an API and I have built a set of methods to validate a request which expect some attributes to be in root. In a rare instance (forgot password), I actually need to present a form and I am looking for a way to use these methods.
you can explicitly define the name for an input by passing input_html to it:
input_html: { name: :name }
(needed this myself for sending an resource to a thirdparty endpoint with redirect to my side which relied on the plain attribute names, but i actually wanted not to built up label and input via the tags ;) )
also see simple form builder impl
Two ways I can think of:
The first is, don't use simple_form to build your form, but do it by hand or with the form_tag and *_tag methods. These will allow you to more closely specify what parameters are used in your form.
If you want to keep simple_form, though, then have it call a different controller action. Refactor the controllers to strip out the logic into a separate method. Something like:
class UsersController
def create_from_api
controller_logic(params)
end
def create_from_form
controller_logic(params[:user])
end
def controller_logic(params)
[actual work happens here]
end
end

Message Module + handling links for private messages + rails

i'm creating an application where i need to send a private message to a user and ask him/her for a confirmation on whether they would like to join or not. I have created a private message module and whenever i want to send a message, i do something like :
def sendMessage(attributes)
subject = 'whatever'
body = 'whatever'
current_user.sendMessage(current_user, subject, body)
end
Then, i get this message and print it out to the needed places, using <%=h %> for escaping stuff. My problem now is, what happens if i want to include 's or even more importantly <%= link_to %> inside that ?
How can i insert such things to be printed out and also be careful about escaping the user provided attributes ? I was thinking of creating sort of like a partial to do this for me, but i would certainly like to hear what you think about it.
Thank you :)
First off, you probably should name your method send_message since that tends to be the convention in Ruby and Rails.
For the question, why not do this:
<p><%=h user_submitted_info %> and check out this <%= link_to "Awesome Link", "/" %></p>
This will escape the user submitted content but leave the link_to alone. Why does the user submitted content and the link need to be in the same ERB tags?

Resources