understanding some embedded erb in Rails with bootstrap - ruby-on-rails

Below is a chunk of code for a modal in bootstrap. I am trying to fully grasp what is happening and i am having trouble with role-options-<%= user.id %> both in the href= and div id tags. I just am not getting what is happening here. I feel like it is passing data some how. I also am not sure how to search correctly for this. So if anyone has any insight for me or places they could point to that would explain this one line.
is it linking directly to the div and passing the user.id?
<a data-toggle="modal" href="#role-options-<%= user.id %>" class="btn btn-mini" type="button">Change role</a>
<%= render user %>
<div id="role-options-<%= user.id %>" class="modal" style="display: none;">
<%= simple_form_for user, :url => user_path(user), :html => {:method => :put, :class => 'form-horizontal' } do |f| %>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Change Role</h3>
</div>
<div class="modal-body">
<%= f.input :role_ids, :collection => Role.all, :as => :radio_buttons, :label_method => lambda {|t| t.name.titleize}, :label => false, :item_wrapper_class => 'inline', checked: user.role_ids.first %>
</div>
<div class="modal-footer">
<%= f.submit "Change Role", :class => "btn" %>
<a class="btn" data-dismiss="modal" href="#">Close</a>
</div>
<% end %>
</div>

Anywhere you see <%= ... %> tags in ERB markup, the Ruby code within the tags is being run and the result is printed out as text in the template (technically, the resulting value from the Ruby code has the #to_s method called on it, to convert it to a String).
So in this snippet:
<div id="role-options-<%= user.id %>"
the ERB template is generating a <div> element with an id attribute whose value is "role-options-", where will be whatever the Ruby code user.id.to_s returns.
And this snippet is similar: its creating a link (<a>) element whose href attribute is of the form #role-options-<user ID>. The only difference is that it begins with #, which means the link will be a page anchor, i.e. it will link to a different section of the same page, namely to the previously mentioned <div>.

Related

How to a parameter from my URL to Modal and Controller in Rails 4 application

All, i am facing an issue with passing a parameter into my Modal to sent the value of a field. I am able to pass and lookup the value via params if i go to new page where i can find the value from the URL params. But i am trying to use the same logic but in a modal and i can't seem to get it to work.
_my comments partial has the following
<% Reply.all.each do |reply|%>
<% if reply.parent_id == comment.id%>
<p class="feed-s-connection-update-item__headline Sans-1px-black-55%">
<%= image_tag(User.find(reply.user_id).avatar,:size => "20x20",class: "image-responsive") %>
<%=reply.body %>
</p>
<%end%>
<%end%>
<% from_reply_form ||= nil %>
<% unless from_reply_form %>
<p>
<%= link_to 'reply', new_reply_path( :id => comment.id) %>
</p>
<%= link_to 'Add release', new_reply_path(#reply,:id => comment.id), {:remote => true, ':id' => 'comment.id','data-toggle' => "modal", 'data-target' => '#replyModal'} %>
<% end %>
<div id="modal-window" class="modal hide fade" role="dialog" aria- labelledby="myModalLabel" aria-hidden="true"></div>
</div>
</li>
</ul>
<%end%>
I am trying this out using this link:
<%= link_to 'Add release', new_reply_path(#reply,:id => comment.id), {:remote => true, ':id' => 'comment.id','data-toggle' => "modal", 'data-target' => '#replyModal'} %>
If i use i am taking to the new page and will have the params in the url which works fine
url: https://hidemyurldevel/replies/new?id=72
<%= link_to 'reply', new_reply_path( :id => comment.id) %></p>
I am able to see the same URL when i try the other URL where remote is true, but i am not able to see the PARENT_ID populated.
Replies_controller - know this part i need to remove the respond but i have it break here so i can see whats returning
def create
#reply = Reply.new(reply_params)
if #reply.save
flash[:notice] = "thanks for reply"
respond_to do |format|
format.js
redirect_to :back
end
end
end
def new
#reply = Reply.new
#comment = Comment.find(params[:id])
#userid = current_user.id
end
Here is the Modal i call
for some reason the value is not populated into the Modal
<%= form_for(#reply) do |f| %>
<div id="replyModal" class="modal fade" tabindex="-1" role="dialog" aria- hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria- hidden="true">×</button>
Post A Reply
</div>
<div class="modal-body">
<form class="form center-block">
<div class="form-group">
Post <%= f.text_area :body, class:"form-control input-lg" %>
<%= f.hidden_field :user_id, :value =>#userid %>
<%= f.hidden_field :parent_id, :value =>#comment.id %>
</div>
<div class="field">
</div>
</form>
</div>
<div class="modal-footer">
<div>
<button class="btn btn-primary btn-sm" data-dismiss="modal" aria-hidden="true">reply</button>
<ul class="pull-left list-inline"><li><i class="glyphicon glyphicon-upload"></i></li><li><i class="glyphicon glyphicon-camera"></i></li><li><i class="glyphicon glyphicon-map-marker"></i></li></ul>
</div>
<div class="actions">
<%= f.submit class: "btn btn-primary btn-sm"%>
</div>
</div>
</div>
</div>
<%end%>
I can't seem to be able to use the parent_id from the URL and pass it into the
This works fine if you go to the new page but not when i try passing it into the URL to the controller. Please help!
So guys i solve my problem... I am not sure if this is the right way to go, but i wasn't setting the comment parameters and it was Nill to begin with.... i just initialize the comment and set it to the instance of the comments and that works..
Excerpt
i am iterating the comment
then in my reply section where i add a comment field next to each comment i am able to pass the value in the URL
<%#comment = comment%>
<%= link_to 'Add release', new_reply_path(:#comment => comment), {:remote => true, :id => comment.id,'data-toggle' => "modal", 'data-target' => '#replyModal'} %>

Ruby on Rails - dynamic navigation with link_to in a div

I want to add navigation to a series of div, which are loaded dynamically.
For this I am using link to covering the div and passing it the attribute to the method.
The problem is that when I press on the div the attribute does not correspond.
I put the code.
With this code all it's ok, but i don't have the link in the div.
<%for i in inicio..final %>
<div class="col-md-3">
<div class="tarjeta_comercio">
<%= link_to 'mezcla/mostrar_institucion', {:controller => "mezcla", :action => "mostrar_institucion", :institucion_actual => #institucions[azar[i]].id} %>
<div class="foto">
<%= image_tag #institucions[azar[i]].logo.url(:original) %>
</div>
<div class="titulo">
<%= #institucions[azar[i]].nombre %>
</div>
<% end %>
But the problem is when i do a block with "link_to...do-div-end". If i do this the parameter don't run. I don't explain why this happen. This is the wrong code.
<%for i in inicio..final %>
<div class="col-md-3">
<div class="tarjeta_comercio">
<%= link_to 'mezcla/mostrar_institucion', {:controller => "mezcla", :action => "mostrar_institucion", :institucion_actual => #institucions[azar[i]].id} do %>
<div class="foto">
<%= image_tag #institucions[azar[i]].logo.url(:original) %>
</div>
<div class="titulo">
<%= #institucions[azar[i]].nombre %>
</div>
<% end %>
<% end %>
In :institucion_actual each time put 0 (when i pulsed over).
Thanks.

Rails partial form in a form

I have a Rails app with Expenses that belongs_to Vendors.
While the user is adding a new Expense, they might need to add a new Vendor.
I thought I could just add a button and a Vendor partial to the Expense form.
Like this:
<%= simple_form_for #expense, :html => {:class => 'form-horizontal'} do |f| %>
<div class="span5">
<a data-toggle="modal" href="#vendorform">
<p class="pull-right btn btn-primary btn-mini">New Vendor</p></a>
<%= render :partial => "vendors/vendorform" %>
</a>
<%= f.association :vendor, :label_method => :vendor_name, :label => 'Vendor', collection: Vendor.order('vendor_name ASC') %>
The _vendorform.html.erb looks like this:
<div id="vendorform" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="vendorformLabel" aria-hidden="true">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4 id="vendorformLabel">Add Vendor</h4>
</div>
<div class="modal-body">
<%= simple_form_for Vendor.new do |f| %>
(bunch of fields)
</div>
<div class="modal-footer">
<%= f.submit 'Save Vendor', :class => 'btn btn-mini btn-primary' %>
<a class="btn btn-mini" data-dismiss="modal" href="#">Close</a>
<% end %>
</div>
</div>
This partial shows up OK. But, the Vendor record doesn't get created.
The submit of the partial actually somehow triggers the submit for the Expense. The page comes back with all of the required Expense fields red.
I think it's posting to the Expenses controller when the f.submit button in the Vendor partial is pressed !!!
Do I need something in the Expense controller?
Or is there a better way?
Thanks for the help!
Are you sure the vendor partial is in the same folder as the expense form? If not you should have
<%= render :partial => "folder/vendorform" %>
When you have nested attributes, you should use the simple_fields_for method which help manage associations. Then, remove the submit button for the vendor form and add it to the expense form instead. So the vendor form would be -
<div class="modal-body">
<%= simple_form_for Vendor.new do |f| %>
(bunch of fields)
<% end %>
</div>

RoR link_to action in div and pass params

I have link_to and when i click link then div(id="edit_comment_form") showed (This div is form for user edit content) and i want pass params[:cc] from link_to to div This is source code.
<%= link_to "Edit", "#edit_comment_form", :class => "btn", "data-toggle" => "modal" %>
<div id="edit_comment_form" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<%= form_tag({controller: "comments", action: "edit"}, method: "get", class: "xxx") do %>
<div class="modal-body">
<%= text_area_tag :comment, "" ,rows: 10,:style => "width:515px; resize: none;",:placeholder => "Please type your comment." %>
<%= hidden_field_tag(:cc, params[:cc]) %> ### I want params[:cc]
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<%= submit_tag "Vote",:class => "btn btn-primary" %>
</div>
<% end %>
How can i pass params to div. Please help me!
You will need to use "data-cc" if you want it to be an HTML attribute on the <a> element and not break the validity of your HTML. You will need to write a custom javascript onclick handler if you want to pass data into the form on click, regardless of how you structure the data into the <a> element that link_to creates.
This post states you shouldn't use data-toggle when you want additional functionality, to prevent some other issues that will arise. You will likely need to build out a full onclick event handler yourself.

Rails: Simple form + bootstrap - When errors, fields not turning red

I have started using Simple-form and Bootstrap and I have tried to follow this reference: Simple form + Bootstrap but I don't know what is going on because when a field is failing, here is what happens:
Regarding this screenshot I have a question:
1) As you see, the Price field is not being red surrounded. How can I do that?
Here is my code for the form:
<%= simple_form_for #lesson, :html => { :class => 'well' } do |lesson_form| %>
<% if lesson_form.error_notification %>
<div class="alert alert-error fade in">
<a class="close" data-dismiss="alert" href="#">×</a>
<%= lesson_form.error_notification %>
</div>
<% end %>
<%= lesson_form.input :title %>
<%= lesson_form.input :category %>
<%= lesson_form.input :description %>
<%= lesson_form.input :price %>
<%= lesson_form.button :submit, :label => 'Create', :class => 'btn btn-primary btn-large' %>
<% end -%>
I believe the code in your reference is somewhat misleading and incorrect insofar as error notifications go.
Change:
<% if lesson_form.error_notification %>
<div class="alert alert-error fade in">
<a class="close" data-dismiss="alert" href="#">×</a>
<%= lesson_form.error_notification %>
</div>
<% end %>
to, simply:
<%= lesson_form.error_notification %>
Then, in config/locals/simple_form.en.yml, change default_message to:
default_message: '<a class="close" data-dismiss="alert">×</a>Some errors were found, please take a look:'
This will fix the asymmetry in the error alert box (by fixing the resultant markup).
In order to troubleshoot the issue with your validation error not displaying in red, you'll have to share the markup so we can see what's going on.
Look at the text next to the price field. Simple for puts the errors next to the field.
To clean up your forms look trying the follow:
Remove this, its not needed with simple-form gem:
<% if lesson_form.error_notification %>
<div class="alert alert-error fade in">
<a class="close" data-dismiss="alert" href="#">×</a>
<%= lesson_form.error_notification %>
</div>
<% end %>
Change your :html => { :class => 'well' } to :html => { :class => 'well form-horizontal' }
This will layout your form better and apply bootstrap css to error fields and text.

Resources