0

Setting which functions as a post. It has the variables: Title:string Description:text, Photo:image and Paragraph:text.

I have another controller which controls my Home, About, News and contact.
On my Home, I'm trying to access the title of the setting with the id 3. I can't figure it out. Do i need to define the controls for the settings in the page controller as well? How can I access it?.

Here is my code at the Home,

<% @setting.(id[3]) do |setting| %>
<h2><%= setting.title %></h2>
<% end %>

Error: Undified local varible or method 'id' for #<#

Would appreciate some help, or a push in the right direction. Thanks :)

7
  • 1
    In controller set the @setting by @setting = Setting.find(3), and then in view put <h2><%= @setting.title %></h2> without all this <% @setting.(id[3]) do |setting| %> Commented Sep 17, 2013 at 10:52
  • Okey but if I want to display for example setting id 1 at one place at the page and setting id 3 at another place, how do I make all the settings avalible in the controller? is it enough with @settings = Settings.all and @setting = Setting.find(params[id]), I'll try it :) Commented Sep 17, 2013 at 10:57
  • You could get them by @settings = Setting.find([1,3]) and then use each of them into view. Commented Sep 17, 2013 at 11:11
  • Okey, how do i then display them in the html ( guessing that I should not use the find([1]) cuz i've already dont that in the controller) Commented Sep 17, 2013 at 11:45
  • It depends on your app and what are you going to do. For example, you can iterate through them: <% @settings.each do |setting| %> and then <h2><%= setting.title %></h2> and <% end %> at the end. Commented Sep 17, 2013 at 11:50

1 Answer 1

3

If you want to display single record then there is no need to have loop, so you can try this.

<% setting = Setting.find(3) %>
<h2><%= setting.title %></h2>
Sign up to request clarification or add additional context in comments.

3 Comments

worked but did not work with <% setting = Setting.find(3) do%> <h2><%= setting.title %></h2><% end %> If I want to display the title of the 4th post somewhere on the page the setting id is already defined, is it not? :) Thanks
And now for only one page you will get a lot of SQL requests to DB. Not so clever :)
you can also do this to display more than one record at any place in a view. in your controller write @setting = Setting.all and in your view <% setting = @setting.find(3) %> <h2><%= setting.title %></h2>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.