I am using Rails 3 and found that if I add
:remote => :true, there will be added to the tag the data-remote = true attribute. But I can't find a way to add custom data- attributes to the urlhelper. The followings won't work:
<%= link_to projects_path, :history => "new"%>
<%= link_to projects_path, :data-history => "new"%> #this throws an error
<%= link_to projects_path, :data_history => "new"%>
What I want to generate is:
New Project
anyone?
What about:
<%= link_to 'New Project', new_project_path, 'data-history' => 'new' %>
( http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to )
This is an elegant solution:
<%= link_to "foo", foo_path, data: { foo: "bar" } %>
Related
If I would like to assign a class to my embedded ruby form, like so?:
<%= form_for(User.new) do |f|, :class => "form-horizontal" %>
How could I go about doing it? I keep getting a syntax error.
Thanks!
from http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for
form_for(record, options = {}, &proc)
meaning:
<%= form_for(User.new, { :class => 'form-horizontal' }) do |f| %>
Hi I got a noob question, I want to create the following HTML result:
TEXT<span class="arrow-big"></span>
In the above HTML I want to have text with a span-class to style in an image via css.
When I try the following implementations the result reflects just one part of the needed implementation:
<%= link_to "TEXT", controller_path, :class => "button-big layer" %>
results in:
TEXT
and
<%= link_to(content_tag(:span, "", :class => "arrow-big"), controller_path, :class => "button-big layer") %>
results in:
<span class="arrow-big"></span>
Does anyone know how to accomplish?
You could also nest tags by using alternative syntax for link_to helper
<%= link_to controller_path, :class=> "button-big layer" do %>
Text
<%= content_tag(:span, "", :class => "arrow_big" %>
<% end %>
Simply concatenate your text with the 'span':
<%= link_to(("TEXT" + content_tag(:span, "", :class => "arrow-big")).html_safe,
controller_path,
:class => "button-big layer") %>
You'll need the .html_safe around the concatenation since the + operator will otherwise escape the HTML of the content_tag.
Reading your question I did solve my problem.
Than I propose another way to answer your question.
You could create a helper method to make this kind of link that you need.
It would be something like this
def link_with_text_and_span(href, text, span_options= {}, link_options = {})
span_tag = content_tag(:span, span_options[:content] || '', :class => span_options[:class] || '')
link_to(span_tag, href, :class => link_options[:class] || '')
end
The good about it is that your view will be cleaner.
Then you can just call this helper method in your view
<%= link_with_text_and_span("/controller/action", "TEXT", {class: 'arrow-big'}, class: button-big) %>
PS: This code can be improved for sure, if other users want to, please do it.
Here's another way you could use without the content_tag. Not the cleanest but it works!
<%= link_to '<span class="arrow_big"></span>'.html_safe, controller_path, class: "button-big layer" %>
I have a <% #user.comments.each do |comment| %>
and inside it is
<%= form_for [????, Subcomment.new],:remote => true do |form| %>
What can I use at ???? to ensure that the current #user.comment is passed to the controller?
If I use
<%= form_for [comment, Subcomment.new],:remote => true do |form| %>
I get
No route matches {:controller=>"subcomments", :comment_id=>#<Comment body: "Comments coming up twice", user_id: 2, commenter: "gleb", id: nil>}
If I got your question right, you need to create a hidden field comment_id
This is the code that I use in rails 2
<%= link_to_remote image_tag("icon_edit.png", :onmouseover=>"$(this).up('.tooltip-wrapper').down('.tooltip-wrapper-box').show()",:onmouseout=>"$(this).up('.tooltip-wrapper').down('.tooltip-wrapper-box').hide()" ).html_safe, :url => { :controller =>'/resume/contact_detail',:action => 'edit_contact_detail'}, :html => { :class => "link_grey" },:before => "$('edit_contact_link').hide();show_spinner('view_contact_detail','view_contact_detail_spinner')" %>
How can I make it to rails 3 ?
I am new to unobtrusive javascript so someone can help me in converting this ?
I tried using the following :
<%= link_to :url => { :controller =>'/resume/contact_detail',:action => 'edit_contact_detail'}, :remote=>true do %>
<%= image_tag('icon_edit.png', :onmouseover=>"$(this).up('.tooltip-wrapper').down('.tooltip-wrapper-box').show()",:onmouseout=>"$(this).up('.tooltip-wrapper').down('.tooltip-wrapper-box').hide()") %>
<% end %>
But I am missing the :before option :(
Thank you
Instead of using :before as you would do in Rails 2.X, define it as a :onclick.
<%= link_to :url => { :controller =>'/resume/contact_detail',:action => 'edit_contact_detail'}, :remote => true, :onclick => "$('edit_contact_link').hide();show_spinner('view_contact_detail','view_contact_detail_spinner')" do %>
<%= image_tag('icon_edit.png', :onmouseover=>"$(this).up('.tooltip-wrapper').down('.tooltip-wrapper-box').show()",:onmouseout=>"$(this).up('.tooltip-wrapper').down('.tooltip-wrapper-box').hide()") %>
<% end %>
The onclick event should be run before the :remote call.
See my comment on your question as to why the remote part isn't running.
To become member, A user can use a button to create a membership on the community page. This is made on a partial that create a new "membership"
the memberships_controller :
#community = Community.find(params[:community_id])
#community.memberships.create(:user => current_user, :role => 1)
in The view :
<% form_remote_tag :url => community_memberships_path(#community) do %>
<%= submit_tag 'Join' %>
<% end %>
After upgrading to Rails 3, that doesn't work anymore !
I tried this :
<% form_tag( {:url => community_memberships_path(#community)}, :remote => true) do %>
and this :
<% form_tag :url => {:controller => '/memberships/new', :action => :create,:community_id => #community }, :remote => true do %>
But no chance.. and have this error
No route matches "/communities/2
Thx for help
The URL is the first argument, options are secondary.
<% form_tag(community_memberships_path(#community), :remote => true) do %>
...
As for your No route matches exception, check you have translated you routes to the Rails 3 syntax correctly.