help with rails render action vs routing - ruby-on-rails

I was using some image cropping example that I found online and now I got confused. There is actually no "crop" method in my controller. Instead (following the guide) I put a
render :action => 'cropping', :layout=> "admin"
In my create method. That renders a page the view called cropping.html.erb . It works fine but I have no idea how to link or render that page otherwise, like if I wanted to hit a URL directly or press a button to recrop an image. Should I actually create a crop method in my controller and hook it up via routing if I want to be able to do this, or is there a way within my view to link to the same place that renders the cropping action?
Sorry about the confusion :) It doesn't help that the first version of the tutorial did have a cropping method and he removed it!! Any explanation on why one method is better over the other would be great. Thanks!!

In your case you would normally name the file create.html.erb which is where rails will look for the file by default. Writing code like:
render :action => 'viewname'
usually happens if you want to render one file in one case and a different one in another.

The best way to do this depends on how you intend to be using the cropping template, and the associated controller logic. You'll find it useful to read up on the render documentation before proceeding.
If you're only ever going to use the cropping template one way. With the same controller logic that independent of the referring action (As in; Not part of a form submission). Then you should be defining a new action and route. It's your choice of whether you want to make a named route or just add a new one to the resource definition in routes.rb
Depending on how you define your route you could do a link_to "Cropping", cropping_url
If you're going to be rendering it from multiple controllers each needing different preparation before rendering the template.
render :template => 'path/template_name'
Where path is the relative path from TEMPLATE_ROOT (RAILS_ROOT/app/views unless otherwise defined) and template name is the filename without the trailing .html.erb/.rhtml
If you want to render cropping as a part of another view, you can make it a partial.
To make it a partial just rename the file to '_cropping.html.erb'. Now it can be called from any view with
<%=render :partial => 'path/partial_name' %>
Again, path is the TEMPLATE_ROOT relatave path to your partial. And partial_name is the file name of the partial, after omitting the leading underscore and trailing .html.erb or .rhtml.
Note: In either of the solutions involving a path to a template, the path can be omitted if the calling controller matches the path. Ie: if the template path is 'users/cropping.html.erb' called from the UsersController.

Related

Is there anyway to create a centralized partial in Rails 4?

I am pretty new to Rails. One of the requirement is to have a Filter Panel that would appear along each record list and will be used to filter records based on criteria. What actually I am looking for an HTML based UI of it that will contain input fields along with labels that I would like to pass from Controller. Since I will be using it across views so I don't want to put in a view specific folder. What's the best way to accomplish this?
You can actually render partials from any folder. For example, in users/show.html.erb you can render a partial _info from, say, transactions.
<%= render 'transactions/info' %>
A common thing to do is to put such shared partials into a separate directory with a descriptive name (I use "shared").
<%= render 'shared/filter_panel' %>
you can put them in /views/application/ dir, rails automatically look for partials in this directory
if you use the application directory then you can just do render 'partial' from any view and it will render /views/application/_partial.html.erb
you can also create for e.g. /views/admin/base dir (if you have admin/base_controller.rb) and put your admin namespace partials there
if you have many partials, I recommend you put them into subdirectories

Render an action view into a file in Rails

I'd like to render an action view into a file (or a string it doesn't matter as long as I can write it to a file later), it seems simple enough but I spend many hours on this without success.
Of course we can render a partial or whatever anywhere, but I also need the controller part. So for example, when I go to /mycontroller/myaction I want the rendering to be done into a file (am I making sense ? :)).
Thanks
you're looking for render_to_string method. It accepts all the options you can pass to render but it will return it as a string which you can write to a file.
Use render_to_string as per the Rails rendering guide:
http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

can I change the format of render partial in a way which changes the format of the partial it loads?

I'm trying to simplify a view from spree for a tablet, using mobile-fu, I copied the view I want to simplify and named it .tablet.erb and i also had to copy the admin layout and rename it to .tabler.erb, the problem is that the layout uses more partials, and those partials use some more, i'm trying to avoid having to copy all the files just to give them a different name or just plain change the render to tell it to use the html one.
Is there a way to tell it to use html format from that point on? like setting the format recursively?
<% self.formats = [:mobile, :html] %> on each view solved it
Got it from here

Still having a hard time with RoR MVC approach

I suppose it should do justice to state what I think I know so far as well as what I've done:
1) I created the app and did my first db migration; I now have my dev, test and production databases. The dev db has a table called 'wines'.
2) I made a scaffold which created the necessary files.
3) The basic index/update/destroy methods are set up and I can browse the pages.
4) From what I gather, the ActiveRecord class "Wine" automatically inherits properties from the database? Each column is a property and each row in the table 'wines' is a potentially instantiated object which is called from the wine_controller script.
The problem I'm having now is that I want to create a common layout that all controllers use. The only things that will change will be the page title, potentially some <link> tags in the header, the <body> attributes (javascript onload events most likely) and whatever lies inside the <body> tag.
I find myself looking up functions that will do what I want (like "favicon_link_tag", "stylesheet_link_tag" and "auto_discovery_link_tag"...) but I can't find the right place to PUT them! I know this has something to do with my lack of understanding of how things are executed/inherited. For example if I were to declare #pageTitle in application_controller.rb and use #pageTitle in ApplicationHelper it won't work. Or even using "stylesheet_link_tag" in application_controller.rb throws an error. I'm just not getting something.
How does each thing relate to another in terms of chronological execution, scope, etc.?
In your "app/views" directory there is a folder called "layouts." By default there should be an "application.html.erb" file in there, but if there isn't you can create it.
Your "application" layout file is the default layout file used by any view. However, if you want a particular controller to use a different view, you can override this. See this railscast, and this one is helpful too.
The main thing to understand is the content from any particular view will show up wherever the yield method appears in your application layout. The main 'yield' block gets the view file specified by your controller action, but you can mark anything inside any view to be passed to another yield block instead. For instance, the "title" example you gave could be passed to the head of your application layout. See this railscast for a detailed example of that.
For more, you should read the Rails Guide, and you might want to consider picking up a Rails starter book.
I got my feet wet with "Beginning Rails 3," which was a phenomenal introduction to the framework. A couple days with that book and it was all making sense to me, and I was developing faster than I ever had before. Rails rocks once you get to know it, but it's definitely worth going through a book.
Please continue to ask questions, I'll help if I can :)
-EDIT- To answer your question about control flow, it basically works like this:
Your browser sends a GET request for a particular URL.
The router takes that request, matches it to a controller action, triggers that controller action, and provides the controller any parameters associated with the request. For instance: if you requested example.com/posts/123?color=red this would trigger the SHOW action of your posts_controller, and would pass {:color => 'red'} to the params hash. You would access that using params[:color]
The controller action does its thing, and when it's done it renders output. By default it renders whatever view is located in app/<controller_name>/<action_name>, and will whichever file matches the extension appropriate to the request (ie an AJAX request would trigger <action_name>.js.erb and a GET request would trigger <action_name>.html.erb.
You can override this using the render method, for example by passing render 'foo/bar' to render using the view for FooController, Bar action instead of your current action.
Note that no matter what you render, the data available to the view is whatever is in the specific controller action the router triggered, not the controller action that would 'normally' render that view.
The view file is parsed using the data from the controller that called it. If you have any content_for methods then the view code that is inside the content_for block will go where you tell it, otherwise everything else will go to the main YIELD block in your application layout (or whatever layout your controller specified instead).
The application layout is parsed, and the content from the view is inserted into the appropriate areas.
The page is served to the user.
That's a simplification in some ways, but I think it answers your question. Again, feel free to keep asking :)

rails render question

My question is about rails rendering rule. if the the following line of code
render "intentions"
appears in a rails app, how do you interpret it? my understanding is that rails will try to find an action named intentions, and find the action's template file named intentions.html.erb
under the current controller's view directory, and finally render it.
but in my caee, in a
100% working app, there is no action named intentions under the current controller. but there is a template file named _intentations.html.erb under current controller's view directory and
it is this template finally be rendered.
what kind of rendering rule it is? I need a explanation. thanks in advance.
Rails automatically looks through your views when rendering to see if any match. If you are calling render from within a view it looks for a partial view, which is defined by a underscore as the first character in its name.

Resources