Redirecting outside a web app? - sql

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)

Related

In middleman, create a dynamic URL in link_to?

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' %>

ransack gem rails 3

i am getting stuck with using the gem ransack. I have placed ransack in my gemfile and then run bundle install ( though i used just bundle, does that make a difference? i didnt think it did?)
Next i have placed this in my Recipe controller
def all_recipes
#q = Recipe.search(params[:q])
#searchresults = #q.result(:distinct => true)
end
Within my view (all_recipes)i have the search form and block to display my results
<%= search_form_for #q do |f| %>
<%= f.label :dish_name_cont %>
<%= f.text_field :dish_name_cont %>
<%= f.submit %>
<% end %>
---------------------
<% #searchresults.each do |r| %>
<tr>
<td><%= r.dish_name %></td>
</tr>
<% end %>
Ive got two problems, without even conducting a search i get this in my view where the block is
fish and chips my first recipe lasagne Lasagne
and then if i search say las it redirects me to my index page after the get request but as i have no block to display the results i get an undefined error, which is expected.
After this i placed my controller code within the index action and the form and block within the index view and now it all works? Why cant i use the all_recipes action and why does it redirect?
In depth demo, may be helpful - http://railscasts.com/episodes/370-ransack?view=asciicast

rails routes - Resource not appending _path

I am attempting to use resources to auto generate routes for my resource. The namespace is admin and the resource is author. The following code seems to work for most instances.
namespace :admin do
resources :author
end
When I run
rake routes
I get the following
admin_author_index GET /admin/author(.:format) admin/author#index
POST /admin/author(.:format) admin/author#create
new_admin_author GET /admin/author/new(.:format) admin/author#new
edit_admin_author GET /admin/author/:id/edit(.:format) admin/author#edit
admin_author GET /admin/author/:id(.:format) admin/author#show
PUT /admin/author/:id(.:format) admin/author#update
DELETE /admin/author/:id(.:format) admin/author#destroy
From what I can tell I am expecting the named paths to have a
_path
at the end. I am rather green at this. I have searched and searched but I could just be using the wrong terms to find the answer. Any help is appreciated. Thanks!
-edit- I should add that
<%= form_for [:admin, #author] do |f| %>
<%= f.label :first_name %>
<%= f.text_field :first_name %>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<%= f.submit %>
<% end %>
Gives me errors saying it can not find admin_author_path
You can append either _path or _url to these. Basically everything looks good.
So for example
admin_author_index GET /admin/author(.:format) admin/author#index
the helper method can be admin_author_index_path or admin_author_index_url. These helpers can be used in controllers and views to point to a controller/action or url. Read this link http://guides.rubyonrails.org/routing.html to understand more.
No. The route name does not have the _path suffix.
Refer to Rails routing from inside in for more information. It explains routing in great detail.

Rails search functionality

I am taking a rails class at my University and I am trying to create a search form which will show the results on the same page rather than show a different page of results. Is this something simple to do? I am creating a museum app with artifacts for each museum but I want the user to search artifacts from either page.
On my routes.rb I have
resources :artifacts do
collection do
get 'search'
end
end
On my museum index I have the code below that he gave us but not sure how to tweak the get routes for the same page.
<%= form_tag search_artifacts_path, :method => 'get' do %>
<p>
<%= text_field_tag :search_text, params[:search_text] %>
<%= submit_tag 'Search' %>
</p>
<% end %>
<% if #artifacts %>
<p> <%= #artifacts.length %> matching artifacts. </p>
<h2> Matching Artifacts </h2>
<% #artifacts.each do |a| %>
<%= link_to "#{a.name} (#{a.year})", a %><br />
<% end %>
<% end %>
Yes, this is easy. Just have the index page return the search results if params[:search_text] is present - this way you don't need a new route or a different page.
class ArtifactsController < ApplicationController
def index
#artifacts = Artifact.search(params[:search_text])
end
end
class Artifact < ActiveRecord::Base
def self.search(query)
if query
where('name ILIKE ?', "%#{query}%")
else
all
end
end
end
So then your form looks like:
<%= form_tag artifacts_path, :method => 'get' do %>
<p>
<%= text_field_tag :search_text, params[:search_text] %>
<%= submit_tag 'Search' %>
</p>
<% end %>
Edit:
So what you really want to do is any page you want to search, include a form which makes a request to that same page.
Then in each of those controller methods just put this line of code:
#artifacts = Artifact.search(params[:search_text])
and that will populate the #artifcats array with only artifacts that match the search query.
Try using "Ransack" gem. It can also perform some more powerful searches.

Rails: Two separate create forms..how to keep them separate?

In Rails 3.0 I have the standard 'new' form that creates a new record, in this case a patient. It works fine and the validations / error showing also work fine.
The client now wants the form in Spanish.
So, I did this:
Created a new html doc called "newspanish" (Cut / paste code from "patients/new")
Created a new partial called "_form_newspanish" and referenced it where the "form" partial is in "newspanish" (Cut / paste code from view "patients/_form")
Created a controller action in "patients" called "newspanish" and cut/pasted exact code from the "new" action.
I left the "create" action untouched.
Added match "patients/newspanish" to routes.
Translated the english parts to spanish in views/newspanish and views/_form_newspanish. Just the stuff that users read on the page, of course...not the rails code.
And, it works, for perfect submissions.
For submissions that fail validation (like putting 3 digits in as a phone number), the page reverts to the view "patients/new" and shows the errors above the form... in English, of course, because patients/new is in English.
Of course, I want it to revert to "views/newspanish" and also show custom verbage in the validations errors (spanish).
Any thoughts on how I can get the patients/newspanish view to load when error validation it tripped?
Here's my code for "_form_newspanish"
<%= form_for(#patient) do |f| %>
<% if #patient.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#patient.errors.count, "error") %> prohibited this subscriber from being saved:</h2>
<ul>
<% #patient.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p><label for="mobile">Número de teléfono celular o móvil*</label>: <%= f.text_field :mobile %></p>
<br />
<%= f.submit "Inscribirme" %>
</div>
<% end %>
And controller... patients/newspanish
def newspanish
#patient = Patient.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #patient }
end
end
<%= form_for(#patient) do |f| %>
is creating a form whose url is submits to is "/patients" which matches to patients_controller, create action.
That create action probably has a line that says (in my pseudo code)
if #patient.save
redirect to somewhere
else
render :new
end
That line "render :new" is showing the "patients/new" view.
So what you have to figure out is to either
1) detect in patients_controller # create how to tell if its spanish, and render "newspanish"
OR
2) change <%= form_for(#patient) do |f| %> to submit to a new url that just handles the spanish version, and make a new controller or action that just handles the spanish form (and renders "newspanish" if the #patient doesn't save
For #2, you could manually change where the form submits to with
<%= form_for(#patient), :url => spanish_patients_path do |f| %>
and in your routes create
post "patients/spanish" => "patients#create_in_spanish"
and add def create_in_spanish to your patients controller