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)
Related
I need to show all my elements on the same page.
In routes:
namespace :nourishment do
resources :diets do
resources :nourishment_meals, :controller => 'meals'
get 'nourishment_meals/show_all_meals' => 'meals#show_all_meals', as: "show_all_meals"
end
end
which will generate:
nourishment_diet_nourishment_meals_path GET /nourishment/diets/:diet_id/nourishment_meals(.:format) nourishment/meals#index
POST /nourishment/diets/:diet_id/nourishment_meals(.:format) nourishment/meals#create
new_nourishment_diet_nourishment_meal_path GET /nourishment/diets/:diet_id/nourishment_meals/new(.:format) nourishment/meals#new
edit_nourishment_diet_nourishment_meal_path GET /nourishment/diets/:diet_id/nourishment_meals/:id/edit(.:format) nourishment/meals#edit
nourishment_diet_nourishment_meal_path GET /nourishment/diets/:diet_id/nourishment_meals/:id(.:format) nourishment/meals#show
PATCH /nourishment/diets/:diet_id/nourishment_meals/:id(.:format) nourishment/meals#update
PUT /nourishment/diets/:diet_id/nourishment_meals/:id(.:format) nourishment/meals#update
DELETE /nourishment/diets/:diet_id/nourishment_meals/:id(.:format) nourishment/meals#destroy
[**THIS**]
nourishment_diet_show_all_meals_path GET /nourishment/diets/:diet_id/nourishment_meals/show_all_meals(.:format) nourishment/meals#show_all_meals
The problem, when I do this:
<%= link_to "Show all meals", nourishment_diet_show_all_meals_path, :class=>"button green" %>
This error raise:
Problem:
Problem:
Document(s) not found for class NourishmentMeal with id(s) show_all_meals.
Summary:
When calling NourishmentMeal.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): show_all_meals ... (1 total) and the following ids were not found: show_all_meals.
Resolution:
Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.
The error is here, on my meals_controller.rb
private
# Use callbacks to share common setup or constraints between actions.
def set_nourishment_meal
#nourishment_diet = NourishmentDiet.find(params[:diet_id])
[***HERE***] #nourishment_meal = #nourishment_diet.meals.find(params[:id])
end
Method:
def show_all_meals
puts "This word does not appear"
end
Can someone help me?
The route below expects a :diet_id. A diet instance has to be provided as an argument for this path to call corresponding action.
nourishment_diet_show_all_meals_path GET /nourishment/diets/:diet_id/nourishment_meals/show_all_meals(.:format) nourishment/meals#show_all_meals
This should be changed:
<%= link_to "Show all meals", nourishment_diet_show_all_meals_path, :class=>"button green" %>
to:
<%= link_to "Show all meals", nourishment_diet_show_all_meals_path(diet), :class=>"button green" %>
Notice the argument (diet) above.
I think you should pass diet_id parameter in params. You should try something like this: <%= link_to "Show all meals", nourishment_diet_show_all_meals_path(#diet.id), :class=>"button green" %>. #diet.id is just an example, use whatever works for your application.
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.
I'm updating my rails 2 apps to rails 3 and find that the use of 'render :text' does not behave the same anymore.
#results is an array. In my controller:
render :text => "<ul>#{#results}</ul>"
It's returning the whole array as a string rather than iterating through each value:
<ul>
["
<li>Steve</li>
", "
<li>John</li>
"]
</ul>
Worked fine in Rails 2.x but not in 3. How do I fix this?
I'm expecting a result of:
<ul>
<li>Steve</li>
<li>John</li>
</ul>
I know this question is for Rails 3.1.4 only.
But those who come here and are on a more recent version, starting with Rails 5.1 we'll do this:
render plain: "I'm like everyone else."
The string contains HTML tags so you will need to mark it as safe so that Rails doesn't escape the tags.
render :text => "<ul>#{#results}</ul>".html_safe
NOTE: Unless there is a valid reason to have HTML in your controller, I recommend moving the list items to a view.
6/23/2014 UPDATE: In retrospect, I don't like having this string parsing logic in the controller. The #results suggests there is HTML embedded in an object somewhere. I recommend using a presentation object and call a method like #results.list. The Draper gem is well-suited for this.
Cite
https://github.com/drapergem/draper
I would suggest doing the following instead of render :text
render :partial => "result", :collection => #results
and add the file: _result.html.erb with
<ul>
<%= result %>
</ul>
or even better if you can remove the li tags from #results
<ul>
<li><%= result %></li>
</ul>
The Rails 3 docs say render text should be used for NON HTML text, which does not fit your use case. Using render :partial :collection is a better and more rails 3 way to iterate through your list.
is it possible to add html-content inside a link_to helper in HAML?
i tried this, but all i get is a syntax error:
= link_to "Other page", "path/to/page.html"
%span.icon Arrow
expected output:
Other Page<span class="icon">Arrow</span>
You should use block
= link_to "path/to/page.html" do
Other page
%span.icon Arrow
If anyone is still using Rails 2.x on a project, it looks like the accepted answer returns the block, thus duplicating the link in the markup. Very simple change: use - instead of =
- link_to "path/to/page.html" do
Other page
%span.icon Arrow
The simplest way to do it is by using html_safe or raw functions
= link_to 'Other Page<span class="icon"></span>'.html_safe, "path/to/page.html"
or using raw function (recommended)
= link_to raw('Other Page<span class="icon"></span>'), "path/to/page.html"
Simple as it can get !!
Don’t use html_safe method unless you’re sure your string isn’t nil. Instead use the raw() method, which wont raise an exception on nil.
I'm working with a model that I know is working (records exist in the data base, can be searched for and displayed in other views, etc.) but when I try to use the form_for tag to generate a view for editing one of these records, I get an error message:
Showing /var/www/caucus/app/views/registration_loader/checkIn.html.erb where line #13 raised:
undefined method `voter_path' for #<#<Class:0x98cabdc>:0x98c8878>
Extracted source (around line #13):
10: </div>
11:
12: <%= form_for(
13: #voter,
14: { :controller => "registration_loader",
15: :action => "editVoter"
16: } ) do |f| %>
The #voter refers to a Voter object retrieved by:
# Get the voter.
#voter = Voter.where( [ "voter_id = ?", #voterId ] )[ 0 ]
if not #voter
flash[ :error ] = "NO VOTER!"
redirect_to :action => 'search'
elsif not #voter.kind_of?( Voter )
flash[ :error ] = "NO VOTER RECORD! (#{#voter.class.to_s})"
redirect_to :action => 'search'
end
When I change the #voter to :voter, it stops giving me the error, but does not populate the fields in my view with the data for the record I want to edit.
According to the Rails 3.1 API guide, passing a model object into form_for should generate code that allows me to edit the data in that object, but evidently there is a missing helper method (voter_path). Where is this voter_path method supposed to be defined, and what is its proper semantic and signature? Nowhere in the documentation is creating such a method discussed, nor can I find any examples of writing such a method.
Is the *_path method supposed to be auto-generated? If not, can someone point me to the documentation that specifies the syntax and semantics of this method?
Thanks,
John S.
Short answer: don't use form_for unless you have also designed your code to use "resourceful controllers". Use form_tag instead. Adding resources :voters to routes creates routes to a non-existent controller.