In middleman, create a dynamic URL in link_to? - middleman

I'm new to middleman and ruby so the syntax is very unfamiliar.
I have setup a simple list page and a dynamic article page.
This all works fine, but how do i generate the dynamic URL in link_to?
My list page is at localhost/list/
And my dynamic article page should be localhost/list/1/ where the number is dynamic.
<% data.list.each do |b| %>
<%= link_to 'Read more', '/list/b.id.html' %>
<% end %>
How can I generate the dynamic URL?

<%= link_to 'Read more', '/list/' + b.id.to_s + '.html' %>

Related

How to specify twitter bootstrap's <i> tag in Embedded Ruby

-Newbie to Ruby on Rails and integrating a few aspects of Twitter Bootstrap in (or at least trying :))-
I have a link in regular HTML with Twitter Bootstrap icons in it:
<li class="sidebar"><i class="icon-user"></i> Home</li>
(The i tag is for twitter's image icons)
If I were writing that same HTML in embedded Ruby, how could I still get the icon in there?
Right now I have:
<%= link_to content_tag(:li, "Home"), root_path %>
Is it possible to specify Twitter's tag in embedded ruby?
Also, should I specify the sidebar class in regular html or with :class => "sidebar"
Thanks!
Sure, just try something like:
<%= content_tag(:li, :class => 'sidebar') do %>
<%= link_to '#' do %>
<%= content_tag :i, '', :class => 'icon-user' %> Home
<% end -%>
<% end -%>
The shorter alternative I use is
link_to " ".html_safe, '#'
It's 'dirtier' but somewhat easier to understand.

tinymce issue with rails 3

I have legacy rails 3 app, where I need to modify a page to use tinymce to edit a text_area.
There are existing pages in this app that already use tinymce.
For reasons I cannot go into here, I cannot use any of the tinymce plugins that are available.
Now my problem is as follows.
I have a model called Sections, that has two attributes, section_name and html.
I want to be able to edit the html using tinymce.
My view has a form which is as follows
<%= form_for #section , :url => update_section_path , :method => :put do |f| %>
<%= f.hidden_field :id %>
<%= f.label :section_name , "Section Name" %>
<%= f.text_field :section_name %> <br />
<%= f.label :html, "Html" %>
<%= f.text_area :html %>
<%= f.submit "Update" %>
<% end %>
The form appears as expected.
The TinyMCE editor also appears on the page with the original html.
The problem is that when I click on the Update button, the put query sent to my server, does not contain the new modified content of the Html text_area. It sends back the original text that was in that text_area.
Could anyone help me understand why.
Thanks in advance
=Puneet
I found an issue in my html which was causing this. Once I fixed that issue it worked fine

Correctly generate a form builder object while discarding the HTML without a deprecation warning

I have a partial which generates a div with some form fields in it. It uses the form builder variable "f" which is provided as input to correctly name the fields in the parameter has (fields are actually nested attributes, so the name is like "[author][book][0][title]").
I want to use that same partial when receiving an AJAX call to regenerate the div based on new user information. I am currently using <% form_for ... |f| %> in my erb file, but that generates a warning that "<% %>" is deprecated.
My erb file looks like the following:
<% if f.nil? %>
<% form_for(#author, :id => :coupon_form) do |f| %>
<%= render "books_detail1", :f => f %>
<% end %>
<% else %>
<%= render "books_detail1", :f => f %>
<% end %>
So what is the correct way to create a form builder context while discarding the generated HTML?
The correct answer is to use fields_for. It generates the same form builder object without the html. I lost track of this in it's use for sub-forms, but it's really the same thing.

Redirecting outside a web app?

This is my first time developing a rails app from scratch. The goal of my code is to use a title and link (both stored in a database table) to redirect users to the link. (Problem) When I click the link title, I'm redirected to localhost:3000/google.com instead of google.com. (Assuming google.com was the value in link.link)
<h1>Links#index</h1>
<% #links.each do |link| %>
<p>
<%= link_to link.title, link.link %>
</p>
<% end %>
Notes:
(1) Using Rails 3.1
(2) The contents of my routes.rb file are below (Not sure if the use of resources :links has something to do with my problem)
CodeHed::Application.routes.draw do
resources :links
get "links/index"
root :to => "links#index"
end
Are your links prefixed with "http://"? If not, try adding that in programmatically with something like:
def add_http(link)
if (link =~ /http(?:s)?:\/\//)
link
else
"http://#{link}"
end
end
If that doesn't work then you could simply enter raw html:
<h1>Links#index</h1>
<% #links.each do |link| %>
<p>
<%= link_to title, add_http(link) %>
</p>
<% end %>
(I haven't checked this code)

how can i use fb_server_fbml helper method in facebooker2?

I am developing a facebook app using rails 3 and facebooker2
i cannot find any example on how to use fb_server_fbml helper method.
what is "proc" in its paramter. Can anyone provide me with sample code?
Thanks
The &proc parameter is used as a content block that is inserted between the <fb:serverFbml> tags. You can learn more about block helpers and differences between Rails 2.3 and Rails 3 here: Block Helpers in Rails 3
So, try something like this:
<% fb_server_fbml do %>
<%#Insert here your content %>
<% end %>
If you want to use the request form, try the following:
<% fb_server_fbml do %>
<% fb_request_form("Your app name","http://www.example.com/callback","Try this out!") do %>
<%= fb_multi_friend_selector("Invite your friends:", {:rows => 3}) %>
<% end %>
<% end %>