Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

noob ruby on rail question.

  • 28-04-2008 10:32pm
    #1
    Registered Users, Registered Users 2 Posts: 950 ✭✭✭


    Im very new to RoR, only been messing around with it for a few days. im trying to in include a file in a ruby on rails project
    in jsp i would have done
    [html]%@include file="Included Files/Main_Menu.jspf" %[/html]

    in ruby im doing
    [html]<%=require 'Main_Menu.js' %>[/html]

    But I keep getting a 'no such file to load ' error

    I have No idea what I’m doing wrong I tried to change the file to different locations even in the same directory level as the file I want to call it from but still no good. any help is appreciated.


Comments

  • Closed Accounts Posts: 8,866 ✭✭✭Adam




  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    Thanks for the reply, however I don’t think this is what I’m looking for, or else I’m not using it correctly. What I want to do is, the contents of the file that is to be included will be outputted with the rest of the Html?
    like my example if I have fields that i use on every page like menu fields then I want to put these fields in a separate file (for ease of maintenance) and the just included that file between <ul> tags


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Well this js file needs to be included in your view for the relevant controller(s). I assume you've got some sort of standard view template, since you mentioned that this menu will be on every page. So really, there will only be one instance of it anyway, so you could hard code it into the view template (e.g. standard.rhtml/standard.html.erb), or as you say, keep it in a seperate file, but this file still needs to be included in the view, if you follow me?

    Is the site up, or can you give me the names of the controllers/views you're working with? I could explain it better that way.


  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    well tbh I started a project a good while back (1 year +) in Java/JSP its just a client server app, but due to the sheer size of it i think i can make more progress in ruby on rails, with the generators that are available for it.

    its not a decision that im taking lightly, and the help out there on RoR is very limited, ever example iv seen so far is of a stupid web blog!!

    so i am not very familiar with it at the min. but from my understanding of it, if i add the menu items to a template then it will make it much less maintainable? because once you generate the model,view,controler, then the menu items are embedded in the view? so if the menu changes then i still have to go thgought ever single view and update the menu manually?

    Btw i really appreciate the help Mirror Thanks


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    You are correct indeed, if you have multiple views that all require the menu. But lets take an example, of boards.ie.

    Now as you can see for yourself, the layout of boards is basically always the same top and bottom, i.e. the header/menu and the footer, and whatever content the user is looking for e.g. a thread, is output in the middle, and is the dynamic content.

    So rather than having a view for the User CP, a view for After Hours, a view for Programming etc. we would have just one standard view, and the controllers for User CP, AH, Programming etc. would generate the content that's sitting between the header and footer.

    If your site follows this methodology, whereby you have a standard layout throughout most if not all of the site, and simply the main body of the site is dynamic content, you would use this approach too, in keeping with the DRY method RoR is so apt for. And therefore, with only one standard view, you would only have your menu code in one place.

    I'm not sure if you are aware, but you don't always need to generate a view for every controller you generate. If you have a controller, you can specifically tell it which view to render, either within individual methods, or at the very beginning of the controller with the code
    layout 'standard'
    
    def index
    ...code...
    end
    
    def new
    ...code...
    render :action => 'edit' //this line actually renders the view for the specified action, it's a
                                   //little confusing in that sense
    end
    
    etc.
    

    Using the above, the line "layout 'standard'" will automatically generate the view standard.rhtml from the folder app/views/layouts for all your actions in the controller, except the action "new", where I specifically told it a different view to render.

    Now, back off the theory, if you have a lot of different views, you would definietly keep the menu js in a seperate file, and include it in the views, and I'm confused as to how the link I gave you didn't work. Assuming your file Main_Menu.js is in the public/javascripts directory, placing the line
    <&#37;= javascript_include_tag "Main_Menu" -%>
    

    within the <head></head> section of any view should allow you to output the menu with subsequent html.

    However, it has just occured to me that you may not have any subsequent html...can you post the content of the js file? Is it intended to actually output the menu by simply including it in the correct position?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    Mirror wrote: »
    However, it has just occured to me that you may not have any subsequent html...can you post the content of the js file? Is it intended to actually output the menu by simply including it in the correct position?

    yes thats exactly what i was hopping for. the MainMenu.js was generated for me (with an online generator http://www.opencube.com) so i just stuck it in a seperate file a .jspf and outputted it to the screen using the JSP command %@include file="Included Files/Main_Menu.jspf" % and this worked fine for me in java/JSP but maybe i was a bit presumptuous to assume i could do the exact same thing in ruby.


  • Registered Users, Registered Users 2 Posts: 339 ✭✭duffman85


    If you want to repeat the menu in every view use a partial.

    Create a file called _menu.html.erb(if you're using Rails 2) or _menu.rhtml (for earlier versions of Rails) in the /app/views folder.
    The file must start with an underscore "_",this is a convention of rails.

    Place your menu code in this file.
    eg.
    <ul>
    <li>...</li>
    <li>...</li>
    <li>...</li>
    </ul>...
    

    Don't put the <html>,<head>,<body> tags in the partial file.


    In your application.html.erb(rails 2) or application.rhtml(older versions of rails) file, where ever you want the menu to appear on your page insert:
    <%= render :partial=>'menu' %>
    
    This tells rails to render the partial called 'menu'. The partial name is the filename without the underscore and file extension(.rhtml etc)

    You must include the javascript file using the javascript_include_tag in your application.html.erb(rails 2) or application.rhtml(older versions of rails) as Mirror mentioned.

    Also your js file must be in the /public/javascripts folder.

    www.railscasts.com is useful for quick video tutorials.


  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    Cool that worked perfectly, thanks a million for the help guys, really apprentice it.


  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    so I have my template setup (Thanks guys) but now i am having another problem. After I created a scaffolding of 'person' with some fields (Name:text, Age:text, Address:text...) start the server go to http:localhost:3000/person ever thing is displayed perfectly. But when I go to http:localhost:3000/person/new the templates is there but the CSS is not applied to it?? any ideas what I could be doing wrong? I will upload the template when I get home.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    How are you linking to the css? Assuming it's external, are you using the old html <link> tag or the ror method?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    yes im using the old style i.e. <LINK href="special.css" rel="stylesheet" type="text/css">


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Ah that's probably your problem. Google for the method to use in ror, I can't remember it off the top of my head but it's something obvious like link_to_stylesheet or something...basically you'll know it when you see it! :)


  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    yip that worked perfectly thanks mirror.

    Now another question on my never ending list :-(

    at this stage i have loads of pages set up, but now i need to change my standard template a little bit. I want to add some code that will retrieve a name from the database. sounds very simple but not sure how to implement it. i though it would be just a matter of adding
    <%=h @admin_person.name %>
    
    but thinking about it now ruby would have no way of teling what controller that came from so my question is how do i tell ruby to use a different controller to revrieve some data from the DB?


  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    still stuck on this one if any one would care to help?

    so i have got as far as this
    <%= render_component :controller => "admin_names", :action => "display_names" %>
    

    and in the admin_names controller i defined
    def display_name
        @admin_name= AdminName.find(:all)
     
      end
    

    but i dont know how to pass the value back to the vew?

    I'v tried doing this
    def display_name
        temp = @admin_name= AdminName.find(:all)
        render :text=> temp.name
      end
    
    but i get a NoMethodError

    Really appreciate some help thanks


Advertisement