0

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

tcooc
  • 20,629
  • 3
  • 39
  • 57
databaseGuy
  • 37
  • 1
  • 8
  • 1
    You should be doing little to no programming in the view. Something like an `.each` loop is generally acceptable when you need to iterate over a list. But the stuff you are doing should be done in the Controller. – Justin Wood Aug 12 '14 at 14:18
  • @JustinWood i think i didn't quite understand the connection between them, can you help me with that or point me in right direction to read some documentation etc. i knew i was doing something wrong :) – databaseGuy Aug 12 '14 at 14:21
  • Here's a great start for documentation, databaseGuy: http://guides.rubyonrails.org/ – vlasits Aug 12 '14 at 14:22
  • https://www.codeschool.com/courses/rails-for-zombies-redux is a free course. Code School has a number of good web tutorials (most are **not** free). I would suggest taking a look at some of those courses. They are well worth the money if you want to take this seriously. – Justin Wood Aug 12 '14 at 14:24
  • @JustinWood well they are really time consuming i always loved learning in the process but i ll take a look for sure – databaseGuy Aug 12 '14 at 14:27

1 Answers1

0

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/

vlasits
  • 2,215
  • 1
  • 15
  • 27
  • ouch so it simply doesn't take string. I didn't really understand your solution to the problem, can you explain it a little more – databaseGuy Aug 12 '14 at 14:35
  • The solution is basically for you to have another look at the rails documentation and re-write your code to leverage the strengths of rails. It's probably too much to explain here. Here is the documentation on link_to http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to and here is a similar question about it. http://stackoverflow.com/questions/10773695/rails-passing-parameters-in-link-to But really, I'd recommend taking a good look at a tutorial and reading http://guides.rubyonrails.org/. Rails is very powerful, but you do need to spend some time learning it. – vlasits Aug 12 '14 at 14:49
  • Also, you will probably want to look at Action Mailer which is the Rails module that assists with creating and sending emails: http://guides.rubyonrails.org/action_mailer_basics.html – vlasits Aug 12 '14 at 14:52