Rails Routes Path Trailing Slashes - ruby

Could someone please tell me why this
<%= destroy_password_url #user.password_reset_token %>
generates
http://localhost:3000/api/destroy_password.4G5EoRVYMUAtiIKqOerKsw
routes.rb
get 'api/destroy_password' => 'services#destroy_password', :as => 'destroy_password'
I'm just following http://railscasts.com/episodes/274-remember-me-reset-password?view=asciicast I don't even know why it's adding a DOT. Could someone please help me out?

In your route file, you're not saying that you accept a parameter. You should probably rewrite it like this:
get 'api/destroy_password/:id' => 'services#destroy_password', :as => 'destroy_password'

Related

What is the best way use link_to in my scenario

I am working with rails, So I have got a problem here
link_to 'Movie Title', {:action=> 'sort', :checks=>"#{#temp.join('_')}"}, :id => 'title_header'
This is my code snippet and on hovering or clicking on this link what I get is,
/movies/sort?checks=G_PG_NC-17
But I need it to be like movies/sort/G_PG_NC-17 , See the difference there is no "checks?" in second url, Anyway I can make that happen with the help of javascript and some changes in routs.rb , but I think there is something else , I can easily get it done.
Your inputs are highly appreciated.
Cheers.
Jossie.
I don't think there's an easier way than to edit the routes.rb file and add something like:
match 'movies/sort/:checks
add this in routes
get '/movies/sort/:checks' => 'movies#sort', as: :movies_sort_checks
then
link_to 'Movie Title', movies_sort_checks_path(#temp.join('_'))
Finally It is done by myself, Anyway I am quite new to rails, I think other wise I would not have come up with this question.
So this part link_to 'Movie Title', {:action=> 'sort', :checks=>"#{#temp.join('_')}"}, :id => 'title_header' changes to link_to 'Movie Title',returm_link(#temp), :id => 'title_header'
See now return_link is a helper method.
go to controller and add
def return_link(kick)
'/'+shot+'/'+kick.join('_')
end
helper_method :stringo
Finally as you expect change routes.rb
match '/sort/:checks', :to => 'movies#sort'
Done everything works as expected, so a custom helper is really helpful here
Cheers .
Jossie.

Instantiating Devise user models manually using contents of params hash

I'm losing my mind trying to figure this out:
There's a registration module which side-steps the main devise registrations controller, and I have to manually instantiate the Devise-generated user model using User.new(...). I'm using a form_tag to submit all of the necessary parameters necessary for User.new(). Here's a truncated version of my code:
def registration_three
#user = User.new(:email => params[:email], :password => params[:password], :password_confirmation => params[:password_confirmation])
#user.save
#user = User.new(:email => 'patlopez#yahoo.com', :password => 'qwertyui', :password_confirmation => 'qwertyui')
#user.save
end
The second #user.save works, but the first one doesn't. params[:email], params[:password], and params[:password_confirmation] certainly exist, according to the print statements in stdout.
Anyone have any ideas?
EDIT:
So I tried changing all of the single-quote strings in the second User.new(...) to double-quote strings like the ones stored in the params hash...and the second instantiation failed as well. So I'm fairly sure that the issue involves double- vs single-quote strings. Anyone know how to convert double-quote strings to single-quote ones? Thanks in advance.
Store your params in variable first then use them in query to avoid quote errors

What is link_to t() in Rails 3?

I watched Railscast 328 this morning and I am having difficulty finding docs for a method.
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_boy_scout_path(boy_scout), :class => 'btn btn-mini' %>
I understand the link_to method, but I am confused about the t('edit .... ) parameter and it is in this method call twice. An explanation or even pointing me to some docs would be great. Thanks for all the help
The t function is an alias for I18n.translate.
The default: option gives the translation to use if the requested key is missing (the '.edit' of your example).
See guide in internationalization (and go to 4.1.2 for the syntax of the :default option)

Rails 3.1 routing error, Named parameter not working

I have a route setup for downloading a document which I need a named parameter for the document name so I get the doc name on the download document. I'm also passing a few unnamed parameters to identify the document.
Looks like this:
match "download/:name", :to => 'documents#download', :as => "doc_download"
And I have my link setup like this:
<%= link_to doc.pdf_name, doc_download_url(doc.pdf_name,
:prefix => doc.doc_prefix,:num => doc.doc_num, :change => doc.doc_change) %>
When I run the page I get the following error:
No route matches {:controller=>"documents", :action=>"download",
:prefix=>"D", :num=>"002", :change=>0, :name=>"sdr_vor_000.pdf"}
The weird thing is the route shows up in my rake routes:
doc_download /download/:name(.:format) {:controller=>"documents", :action=>"dow
nload"}
Ideas?
Also if I remove the name parameter or make it optional, it works, but doesn't stick the name into the url. It just gets tacked onto the query string.
Your name parameter contains a period - by default, the part after the period is interpreted as the format, not part of the name parameter. To allow periods in the filename, amend your route like this (tweak the regex to your needs):
match "download/:name", :to => 'documents#download',
:constraints => { :name => /[a-z0-9\.]+/i }, :as => "doc_download"

Ruby on Rails - Truncate to a specific string

Clarification: The creator of the post should be able to decide when the truncation should happen.
I implemented a Wordpress like [---MORE---] functionality in my blog with following helper function:
# application_helper.rb
def more_split(content)
split = content.split("[---MORE---]")
split.first
end
def remove_more_tag(content)
content.sub(“[---MORE---]", '')
end
In the index view the post body will display everything up to (but without) the [---MORE---] tag.
# index.html.erb
<%= raw more_split(post.rendered_body) %>
And in the show view everything from the post body will be displayed except the [---MORE---] tag.
# show.html.erb
<%=raw remove_more_tag(#post.rendered_body) %>
This solution currently works for me without any problems.
Since I am still a beginner in programming I am constantly wondering if there is a more elegant way to accomplish this.
How would you do this?
Thanks for your time.
This is the updated version:
# index.html.erb
<%=raw truncate(post.rendered_body,
:length => 0,
:separator => '[---MORE---]',
:omission => link_to( "Continued...",post)) %>
...and in the show view:
# show.html.erb
<%=raw (#post.rendered_body).gsub("[---MORE---]", '') %>
I would use just simply truncate, it has all of the options you need.
truncate("And they found that many people were sleeping better.", :length => 25, :omission => '... (continued)')
# => "And they f... (continued)"
Update
After sawing the comments, and digging a bit the documentation it seems that the :separator does the work.
From the doc:
Pass a :separator to truncate text at a natural break.
For referenece see the docs
truncate(post.rendered_body, :separator => '[---MORE---]')
On the show page you have to use gsub
You could use a helper function on the index page that only grabs the first X characters in your string. So, it would look more like:
<%= raw summarize(post.rendered_body, 250) %>
to get the first 250 characters in your post. So, then you don't have to deal w/ splitting on the [---MORE---] string. And, on the show page for your post, you won't need to do anything at all... just render the post.body.
Here's an example summarize helper (that you would put in application_helper.rb):
def summarize(body, length)
return simple_format(truncate(body.gsub(/<\/?.*?>/, ""), :length => length)).gsub(/<\/?.*?>/, "")
end
I tried and found this one is the best and easiest
def summarize(body, length)
return simple_format = body[0..length]+'...'
end
s = summarize("to get the first n characters in your post. So, then you don't have to deal w/ splitting on the [---MORE---] post.body.",20)
ruby-1.9.2-p290 :017 > s
=> "to get the first n ..."

Resources