What is the best way use link_to in my scenario - ruby-on-rails-3.1

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.

Related

Sinatra param filter

I am about to finnish my Sinatra Haml School project but I need a couple of things to get solved (I tried really hard but cannot solve the issues). I have a Software portfolio CMS where I can create new software entries (Title, Description, Language and Github link), being language a dropdown list filled from the database. Thing is I want to let the user select a filter from the list and filter by categories in the software list, but when I press the filter button it only shows the first entry. Here's the code in app.rb
get '/softwares/:filter' do
halt(404,'Forbidden') unless session[:admin]
#sware = Software.all
#categ = Category.all
haml :sware
end
post '/softwares/:filter' do
#sware = Software.find(category: params[:category])
haml :sware
end
And here is the HAML code that shows the list of softwares
%form{:action => "/softwares/:filter", :method => "post", :role => 'form'}
%select{:name => "category"}
- #categ.to_a.each do |category|
%option= category.name
%input{:type => "submit", :value => "Filter", :class => "btn"}
%ul.list
- #sware.each do |software|
%li(class="glyphicon glyphicon-search" aria-hidden="true")
%a{:href =>"/software/edit/#{software.id}", :class =>"btn btn-lg btn-primary"}= software.title
%a.pull-right(href="/software/delete/#{software.id}" class="btn btn-lg btn-danger") Delete
%li(role="separator" class="divider") ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Thanks a lot for your answers. I really appreciate every information you can provide about what am I doing wrong.
It is a bit hard to tell from your question because you don't provide much info, e.g. what ORM you are using.
Supposing you're using ActiveRecord:
Software.find(...)
#=> returns the first match
Software.where(...)
#=> returns all matches

Sinatra dropdown list

Im glad you answered my question about my dev issue, I'll try to be more self-explanatory this time.
I have a main app.rb where I use several endpoints redirecting to my Sinatra Haml views.My project is about a Software Portfolio, so I have this class: Software, and Category, which relationship is: one software has one category, and a category has many softwares. In the form where you create a new Software entry, I put a dropdown list where you can choose between 3 different categories: Desktop, Web and App.
Until there, everything is going well. The thing is, when the Software list shows up, I want to put a dropdown list to filter by created categories (I already have the "add category" form with its Class) and I can't figure out how to add the filter within a Filter button in the software list form. Can you guys help me please? Of course I know how to put the button there, but I want to show only the software entries where the selected category matches. Here's the list form.
%select{:name => "category"}
%option Desktop
%option Web
%option Device
%input{:type => "submit", :value => "Filter", :class => "btn"}
%ul.list
- #sware.each do |software|
%div{:class =>"list-group"}
%a{:href =>"/software/edit/#{software.id}", :class =>"btn btn-lg btn-primary"}
= software.title
%a.pull-right(href="/software/delete/#{software.id}" class="btn btn-lg btn-danger") Delete
Thanks a lot in advance!
You're calling the index method like this:
post '/all' do
index(:category)
end
You're passing an argument to the index call, but the index method doesn't take any arguments.
Please include the full error with your question.
index action can be DRY'ed:
def index
category = case
when params[:Web] then :Web
when params[:Desktop] then :Desktop
when params[:Device] then :Device
end
#sware = Software.title.where(categorization: { Software.categorization => category })
end
"It just doesn't work" is not a good place to start investigation of the problem. More debug information is required.
Beside the point that #max pleaner made, you're not actually calling the right object in params. It should be params[:category] and you should be able to rewrite that much simpler:
get '/all' do
halt(401,'Not Authorized, please login to continue') unless session[:admin]
#sware = Software.all
haml :sware
end
post '/:category' do
#sware = Software.title.where(categorization: {Software.categorization => params[:category]}
haml :index # assuming index.haml is where you want to go
end
Then, assuming your file is properly indented, your Haml file should work as well:
%select{:name => 'category'}
%option Desktop
%option Web
%option Device
%input{:type => 'submit', :value => 'Filter', :class => 'btn'}
%ul.list
- #sware.each do |software|
%div{:class =>'list-group'}
%a{:href =>"/software/edit/#{software.id}", :class =>'btn btn-lg btn-primary'}
= software.title
%a.pull-right{:href=>"/software/delete/#{software.id}" :class=>'btn btn-lg btn-danger'} Delete
Of course, the more information you can provide, the better the problem can be understood.

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 Routes Path Trailing Slashes

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'

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