I am trying to DRY up some of my HTML in my application currently I have a block of HTML that will get re-used multiple times
<div class="block">
<div class="block_head">
<div class="bheadl"></div>
<div class="bheadr"></div>
<h2>Configuration Needed</h2>
</div>
<div class="block_content">
<div class="message warning">
<p>You have not create an admin user yet</p>
</div>
</div>
<div class="bendl"></div>
<div class="bendr"></div>
</div>
What I would like to do is to create a partial or something along those lines and be able to pass in the content to the block header and content
Does anyone know of a way to do this in rails 3
The way i do it is to have a views/shared folder. Then, i create partials inside and i call them like :
<%= render "shared/flash_error", :error => flash[:error] %>
where shared/flash_error is :
<% if error %>
<%= error %>
<% end %>
If you want to have your partials in the partial folder, use this syntax :
<%= render :partial => "partials/your_partial", :locals => { :error => flash[:error] } %>
Related
I have a jquery that everyone at my site can click on to update their subscription that looks like this
<div id="role-options-<%= current_user.id %>" class="modal modalSubscription" style="display: none;">
<%= simple_form_for current_user, :url => user_path(current_user), :html => {:method => :put, :class => 'form-horizontal' } do |f| %>
...Form stuff
<div class="modal-footer">
<%= f.submit "Update Settings", :class => "btn btn-green" %>
<a class="btn btn-green closeModal" data-dismiss="modal" href="#">Close</a>
</div>
<% end %>
</div>
And i am trying to push this to a custom route that is an update subscription path. I am using devise. In a perfect world, i would like to send it to a separate controller that is labeled SubscriptionsController so that it can handle the update action. I've tried many things in the routes
#I expected this to send the request to the subscription controller after i changed the :url to update_subscriptions_path, but it did not
put "/users/update_subscription", :to => 'subscription#update'
I also tried just adding an "update_subscription" method in the controller and set the url in the form to
users_update_subscription_path
However this posted to the update path
Alright so was making a mistake, the paths i was adding were after resources :users, it looks like rails sees the put request and automatically assumes that I want to update the user and didn't make it down to the extra route I had added.
I started using Ruby on Rails a few days ago, and I have a problem with the each do loops.
Here's my AccueilController's code :
def index
#postsTest = Post.last(1)
#articles = Article.last(1)
#articlesList = Article.last(2)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #users }
end
Here's my application.html.erb code :
<div id="container">
<div id="col1">
<%= render :partial => 'shared/listArticles', :collection => #articlesList %>
<div class="clear"></div>
</div>
<div id="col2">
<%= yield %>
</div>
</div>
</div>
And now my shared/listArticles's code :
<% #articlesList.each do |article| %>
<div id="blocArticle">
<div id="pic">
<%= image_tag (article.photo.url(:thumb)) %>
</div>
<div id="contentArticle">
<div id="titleArticle">
<%= link_to article.title, article %>
<div class="clear"></div>
</div>
<div id="contentArticleDesc">
<%= link_to article.desc, article %>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
<% end %>
And my problem is that if I write #articlesList = Article.last(2) then the last two articles will appear two times; if I write #articlesList = Article.last(3) then the last three articles will appear three times etc...
Of course, I would like that each of them appear just one time.
Does someone have any ideas about the source of the problem ?
You're rendering a partial with a collection, therefore Rails calls the partial for each item in the collection. Remove your loop from the partial view or remove the collection param, don't do both!
I want the home page to show everyones microposts, but I keep getting errors. I feel like this might be because the microposts have a belong_to has_many relationship with users. But anyways, This is the code for the home page..
<section>
<%= render 'shared/user_info' %>
</section>
<section>
<div id= "purchases">
<%= render 'shared/micropost_form_purchase' %>
</div>
<div id="sales">
<%= render 'shared/micropost_form_sale' %>
</div>
</section>
<ol class="microposts">
<%= render #microposts %>
</ol>
<%= will_paginate #microposts %>
and it gives me this error: 'nil' is not an ActiveModel-compatible object that returns a valid partial path. at the bottom.
I added
def home
#microposts = Micropost.all
end
to the microposts controller.
can anyone help me out? please?
The call to render is looking for a partial view that it can't find. If you're keeping all your partials in views/shared/, then do you have a views/shared/micropost.html.erb for it to render?
So I have this file:
<h1>Calendar view</h1>
<div class="events">
<% #events.each do |e| %>
<%= raw(e.content)%>
<% end %>
</div>
<br />
<div class="messages">
<% #messages.each do |m| %>
<%= raw(m.content)%>
<% end %>
</div>
With #events and #messages being valid instance variables in the controller...but when I go to the page the html looks like this:
<h1>Calendar view</h1>
<div class="events">
<br>
<div class="messages">
This is another message test
</div
Event Content
</div>
I'm confused. Maybe I'm missing something obvious?
The problem is that raw() will output raw HTML content. The Rails template engine will try to merge that with the .erb template you supplied.
Therefore, if either m.content or e.content are malformed, you will most likely get unexpected output.
The best way would be to look for syntax errors, especially missing closing elements.
I have a blog set up with the usual articles and tags on the index page. The Tags section also shows up on the sign-in and sign-up pages which doesn't look great.
The tags are set up in their own column under _side.html.erb - is there a way to hide them from all other pages apart from the index page?
<h3>Blog Tags</h3>
<div id= "tags" >
<% cache('all_tags') do %>
<% for tag in Tag.find(:all, :order => 'name') %>
<ul style="list-style-type: none">
<li>
<%= link_to "#{tag.name}", tag_path(tag) %>
</li>
</ul>
<% end %>
</div>
If you
render :partial => 'side'
from within your application.html.erb, then you could use another layout for the index page which then renders the partial while the standard layout would not.