Keeping track of which view a partial is called from - ruby-on-rails

I call the same partial from multiple views in Rails, the show page and the edit page. How can I keep track of which view the partial is called from? More specifically, I would like to adapt the partial slightly depending on which page it is rendered from. I have tried to request the uri using url_for(:only_path => true) and if-else statements to determine if the partial is rendered on the show or edit page, but this is a bit cumbersome. Is there a better approach?

you can try to use current_page? helper, i.e. if current_page?(root_path)

We put this in our application.html.erb template which shows the current controller, view and more debug information:
<%= debug(params) if Rails.env.development? %>
I think we got this from the Rails site:
http://guides.rubyonrails.org/debugging_rails_applications.html

Related

Use template to create multi-page ruby on rails web application

Is there any way to create a webpage template that I will be applying to all my webpages?
I am new to ruby on rails, I have gained enough knowledge to understand how flow works in it but can't find out the way to use the same page-template for all pages on the site.
I am using RubyMine but can work on command prompt if required.
Any help would be greatly appreciated.
app/views/layout/application.html.erb this is a common layout in which you will found <%= yield %> which render all the pages in <body> tag. Now as per your requirement you want some common template to show on all pages.
So better to make one partial file..For example, Header and Footer remains same in whole site. For doing this, make one partial file called _header.html.erb for header part and _footer.html.erb for footer part. Put these files under app/views/layout/_your_partialfile.html.erb
Then render them like:
<%= render partial: "/layouts/header" %>
<%= yield %>
<%= render partial: "/layouts/footer" %>
For more info refer : http://guides.rubyonrails.org/layouts_and_rendering.html
I hope this makes you clear to understand now. :)
In general, rails projects have a file in app/layouts/application.html.haml that's applied to every single page you load. You can put navbars there, login links, etc.

Conditional content in html.erb

i have a footer as the last element in my application.html.erb page, however for one page in the whole site, i do not want the footer to appear.
Whats the best way to handle this? every solution i come up with is wet (not dry)
Why don't you create a specific layout for this single page? It should be more maintainable than any extra logic.
DRY is not a goal to reach, it's like a conditional warning and like all warnings, you can ignore them if it makes sense.
If you really insist, do this:
<% unless defined? #no_footer %>
your html here
<% end %>
So the footer will disappear only if you set the instance variable in your controller:
#no_footer = true
Another way could be to check params action/controller and put the logic in a helper method.

Rails 3 HTML Injection

Currently following the Learning Rails Screencasts at http://www.buildingwebapps.com/learningrails, making any necessary changes to work in Rails 3. However, in the tenth episode, I'm having a problem when rendering html code out of the database. The Page model in the tutorial has a body field, where the html of each page is put. The viewer controller's 'show' method grabs a Page from the database, and yields the contents of #page.body into the view. However, instead of rendering tags such as h1 properly, when I view the html source in the browser my tags are being rendered as <h1;#gt. Is there any way I can fix this?
Just for reference, my 'show' view is as follows:
<%= #page.body %>
Try this:
<%= raw(#page.body) %>
Raw method prevents escaping HTML characters.

Best techniques to ajaxify Rails app?

I'm currently ajaxifying my Rails app as follows.
JS
application.js
$("a").live("click", function() {
$.getScript(this.href);
//do something
return false;
});
Views
index.js.erb
$("#core").html("<%= escape_javascript(render 'index') %>");
index.html.erb
<%= render 'index' %>
_index.html.erb
#my partial
So when a user clicks a link, it will be intercepted and the corresponding js file will be executed, which renders a partial in a div. This means that, for each action, I will need 3 views, say index.js.erb, _index.html.erb, and index.html.erb.
This is painstaking to set up, and the index.html.erb file is somewhat useless, it just renders the partial (perhaps there's a way to render a full view from another view directly, hence eliminating the need for a partial?).
Is this the best way to do things? How do you usually imbricate Ajax with Rails?
Thanks.
Javascript MVC seems like it works well with MVC style backends. It standardizes ajax calls, controllers and views in a similar way that you are showing above:
http://javascriptmvc.com/

Asp.Net MVC - RenderPartial - Create in a List view

I got a page that lists all my articles (Articles/List.aspx).
I also got a control that create article (Article/Create.ascx).
I will like that my List.aspx page that's render the Create.ascx to be able to create article.
I know that in MVC, the preferred approach is one page by action. But in this case I need to do that. It's a design issue and how the client want the Web site to work.
So for now, I got the following code in List.aspx :
<% Html.RenderPartial("Create", new Domain.Models.Article()); %>
That render correctly. But when I hit the create button, it's doesn't go in the Create[post] method of my ArticleController.
Any idea why and how I could resolve that issue ?
If you have problems with the button, it's not going to have anything to do with how you're rendering the user control. We need to see the form markup that the button is inside, that will show what the problem is most likely.
But just for reference, you're probably looking to do something like this:
<% using (Html.BeginForm("Create",
ViewContext.RouteData.Values["Controller"].ToString())) { %>
your control markup here
<% } %>

Resources