I'm using Angularjs in parts of my Rails app, which works great. But I'm wondering how to use an Angular value inside a link_to.
Here is my pseudo code:
%table
%tr{"ng-repeat" => "book in books"}
%td
{{book.title}}
%td= link_to "Show", book_url({{book.id}})
This gives me an error:
syntax error, unexpected '}', expecting tASSOC
This could also have to do with HAML causing the error, but how can I send the ID in the link_to?
This worked for me:
<li ng-repeat="deal in deals">
<%= link_to 'show,'#','ng-href' => "#{deals_path()}/{{deal.id}}" %>
</li>
Instead of a "link_to", I can of course use a normal link:
%a{href: 'books/{{book.id}}'} 'Show'
It work fine:
= link_to 'Show', URI::unescape(books_path('{{book._id}}'))
Related
I'm trying to move from Font Awesome to icomoon.
With Icomoon I can get the icons to work using the following syntax
<span data-icon="" aria-hidden="true"></span>Some Text
However, as I'm using a rails app I'd really prefer to use the following syntax, or something similar.
<%= link_to icon_tag("icon-pdf", "some text"), controller_path %>
I have tried the following as well, all to no avail
<%= link_to 'Some text', controller_path, {"data-icon" => "", "aria-hidden" => "true"} %>
It doesn't matter if I put the defined name (icon-pdf) or its hex value in there, but I can't seem to get the icon to appear.
Is there a way I can achieve this, or am I stuck with the data-icon method?
Try with this:
<%= link_to controller_path do %>
<span data-icon="" aria-hidden="true">Some Text</span>
<% end %>
Or
<%= link_to content_tag(:span, 'Some Text', :data_icon => "", :aria-hidden => "true" ), controller_path %>
It should work. Thanks
Suddenly flash and params aren't working for me. Even my normal flash error/success messages don't work.
This is just one sample - I have the following in a view:
<%= link_to 'New Comment', new_comment_path, :class => 'btn btn-primary', :onclick => flash[:worequest_id] = #worequest.id %>
And in the new comment form:
<% if flash[:worequest_id] != nil %>
<%= f.hidden_field :worequest_id, :value => flash[:worequest_id] %>
I have been using flash to pass data between a view and a form in quite a few places in my Rails app. Now, none of them are working!!
Could I have set some configuration that would turn off flash and params?
I appreciate your help!!
UPDATE
I read on another post, where someone was having "session problems" with FLASH. Where would I look for session problems?
UPDATE2
I added the following to the view that receives the data from the flash:
<% if flash[:worequest_id].blank? %>
<h3>flash blank</h3>
<% end %>
And "flash blank" showed up.
Really - This has been in many places of my app for months. And now it's not working!!!!!!!
UPDATE3
I also tried this:
<% flash.keep[:worequest_id] = #worequest.id %>
Is there a rails config file that deals with session? Maybe I messed up a parameter or something.
UDPATE4
This is the only line in my initializers/session_store.rb file:
Ndeavor::Application.config.session_store :cookie_store, key: '_ndeavor_session'
UPDATE5
Params don't work either !!!!!
params[:worequest_id]
UPDATE6
I just created a new app from scratch using Rails 3.2.11 and flash and params are working fine. So, it's not the version of Rails.
Im a bit confused about what/how you're trying to achieve.
Have you looked at the output produced by this?
<%= link_to 'New Comment', new_comment_path, :class => 'btn btn-primary', :onclick => flash[:worequest_id] = #worequest.id %>
I removed the jbuilder gem and now flash and params are working. It has to be something I did, because I just tried jbuilder in another app, and it didn't break flash. I'm going to try and figure out why my use of the gem caused the problem.
I'm having an issue with Rails, I get the following error:
No route matches {:action=>"publish", :controller=>"businesses"}
The offending code:
<div id="searchDatesDiv" style="margin: 0 auto;">
<%= form_tag(publish_business_path, :method => :post) do %>
<%= submit_tag("Publish") %>
<% end %>
</div>
routes.rb:
resources :businesses do
member do
post 'publish'
end
end
rake routes:
registration GET /registrations/:id(.:format) registrations#show
publish_business POST /businesses/:id/publish(.:format) businesses#publish
businesses GET /businesses(.:format) businesses#index
POST /businesses(.:format) businesses#create
I can see the path defined in rake routes. Why am I getting this error? The form is also a POST method. Any help would be great! I've tried looking at similar questions on SO but haven't found one that works in my case :(.
Your business publish path requires an id parameter that you are not giving it. Pass the business object to the path helper thusly:
<%= form_tag(publish_business_path(#business), :method => :post) do %>
Question update:
I have figured out that importing JQuery (even without using it at all) is causing :remote => true to not work properly. I get the following error in the Chrome console:
Uncaught TypeError: Object [object Object] has no method 'dispatchEvent' (prototype.js:5653)
And when the link is clicked, it throws the same error again before redirecting.
Responder.erb.html
<ul>
<% #choices.each_with_index do |choice, index| %>
<li><%= link_to choice, { :action => "submit_response", :id => #id, :response => index }, :remote => true %></li>
<% end %>
</ul>
Example generated HTML:
<ul>
<li>True</li>
<li>False</li>
</ul>
Since Dave never formally answered this question, the answer is:
"If you're trying to run both jQuery and Prototype in the same app, the load order of the libraries matters, or you must use noConflict."
– Dᴀᴠᴇ Nᴇᴡᴛᴏɴ
For the love of god, I've been banging my head on this for hours. Using rails3 rc, 1.9.2.
I'm trying to create a link_to that submits an ajax request, with parameters, a class and id, and needs a block so I can insert a span tag around the name. Documentation is of absolutely zero help, as are numerous google searches. Here's what I've got so far:
<%= link_to(
:url=>{
:controller => 'themes', :action => 'remove_tag',
:entity_id => entity_id, :theme_id => theme_id,
:entity => entity, :element_id => element_id, :parent_id=>parent_id
},
:remote => true,
:id => "theme-tag-#{entity}-#{entity_id}",
:class => "tag") do %>
<span class='subtract'><%= tag %></span>
<% end %>
The generated url looks like this:
<a href="/explore/index/theme-tag-user-3?url[controller]=themes&url[action]=remove_tag&url[entity_id]=3&url[theme_id]=16&url[entity]=user&url[element_id]=filter-contributor-3&url[parent_id]=filter-contributors&remote=true&class=tag">
Test descriptor
I can't indicate properly that the text "Test descriptor" is actually properly included within the span; code formatting is failing a little here, however, the href is wrong, there's no class or id, and downhill it continues to roll
If I didn't need the block, I could just add the name and not have to specify :url=>{...} (leaving if off throws an exception with the block, go figure) and then follow that with :remote=>true, :id=>"whatever", :class=>"blah" and it works. What am I doing wrong? Because I'm new to rails in general, I'd also like to understand why this syntax must differ so much? I mean really, thank god you don't have to write a lot of links like this in a web app... ;-)
Thanks in advance
turns out you have to do url_for(...) instead of :url=>{...} and it all worked as expected.
Just putting wkhatch's comment here so it's nicely formatted.
<%= link_to(
url_for(:controller=>'themes',
:action=>'remove_tag',
:entity_id=>entity_id,
:theme_id=>theme_id,
:entity=>entity,
:element_id=>element_id,
:parent_id=>parent_id),
:remote=>true,
:id=>"theme-tag-#{entity}-#{entity_id}") do %>
<span class='subtract'></span><%= tag %>
<% end %>