I've rendered a partial that also loads the Users latest post in one of the tab panels.
<div role="tabpanel" class="tab-pane" id="updates_panel">
<%= render :partial => "pages/update_panel", :locals => { :post => #user.latest_post } %>
</div>
At first the method latest_post was to display the users "Last or Latest Post".
def latest_post
posts.order(:created_at => :desc).first
end
But now I realized I need to display all the post in descending order and I cant seem to get this right.
create another method like latest_posts,
def latest_posts
posts.order(:created_at => :desc)
end
def latest_post
latest_posts.first
end
And now use this in your partal,
<%= render :partial => "pages/update_panel", :locals => { :post => #user.latest_post, :posts => #user.latest_posts } %>
Related
I need some help printing the values of my hash. In my "web.rb" file I have:
class Main < Sinatra::Base
j = {}
j['Cordovan Communication'] = {:title => 'UX Lead', :className => 'cordovan', :images => ['http://placehold.it/350x150','http://placehold.it/350x150','http://placehold.it/350x150']}
j['Telia'] = {:title => 'Creative Director', :className => 'telia', :images => ['http://placehold.it/350x150','http://placehold.it/350x150','http://placehold.it/350x150','http://placehold.it/350x150']}
get '/' do
#jobs = j
erb :welcome
end
end
In "welcome.rb" I'm printing the values of the hash, but it doesn't work:
<% #jobs.each do |job| %>
<div class="row">
<div class="span12">
<h2><%=h job.title %></h2>
</div>
</div>
<% end %>
Here's my error message:
NoMethodError at / undefined method `title' for #<Array:0x10c144da0>
Think what #jobs looks like:
#jobs = {
'Cordovan Communication' => {
:title => 'UX Lead',
:className => 'cordovan',
:images => ['http://placehold.it/350x150','http://placehold.it/350x150','http://placehold.it/350x150']},
'Telia' => {
:title => 'Creative Director',
:className => 'telia',
:images => ['http://placehold.it/350x150','http://placehold.it/350x150','http://placehold.it/350x150','http://placehold.it/350x150']}
}
Then remember that each called on a hash passes a key and a value to the block, and you'll see that you have:
#jobs.each do |name, details|
# On first step, name = 'Cordovan Communication', details = {:title => 'UX Lead', ...}
end
So what you want is probably:
<% #jobs.each do |name, details| %>
<div class="row">
<div class="span12">
<h2><%=h details[:title] %></h2>
</div>
</div>
<% end %>
There's no automatic method creation for Ruby hashes, e.g., you can't call job.title, since there is no title method on Hash objects. Instead you can call job[:title].
Also note that #jobs is a hash, not an array, so you probably want to call #jobs.each_pair rather than #jobs.each. It's possible to use #jobs.each, but in this case it's not going to give you what you expect.
I rendering a view partial like this.
<%= render :partial => 'resources/positions', :controller => 'resources',
:action => 'this_is_a_test',
:locals => {:id_resource => 42} %>
resources_controller.rb
def this_is_a_test
#test1 = "batman"
render :partial => 'positions'
end
_positions.html.erb
<%= #test1 %>
but the variable #test1 is empty. Do you have any idea ?
def this_is_a_test
render :partial => 'positions', :locals => { :test1 => "batman" }
end
and change _position.html.erb to
<%= test1 %>
Here is my simple ajax code.
The request is made, I get the response but my view does not get rendered with new data. I get a new page with new html that I asked to render instead being rendered inside current page.
controller -
def list
#users = User.find(:all)
end
def show
respond_to do |format|
format.html { render :partial => "ajaxshow" }
format.js
end
end
view -
<% #users.each do |c| %>
<%= link_to c.user_id, {:action => :show, :user_id => c.user_id, :remote => true } %>
<% end %>
partial -
# cat _ajaxshow.html.erb
<div align="left" id="user_ajax">
<table width="1000px">
<tr><th>User ID</th><th>First</th><th>Last</th><th>ID</th><th>Email</th></tr>
</table>
log -
Started GET "/users/show?remote=true&user_id=someuser" for 10.10.10.10 at 2012-04-17 04:01:09 -0400
Processing by UsersController#show as HTML
Parameters: {"remote"=>"true", "user_id"=>"someuser"}
Rendered users/_ajaxshow.html.erb (0.3ms)
Completed 200 OK in 2ms (Views: 1.3ms | ActiveRecord: 0.0ms)
Your link_to will not trigger the ajax call
<%= link_to c.user_id, {:action => :show, :user_id => c.user_id, :remote => true } %>
First change the above code
<%= link_to c.user_id, {:action => :show, :user_id => c.user_id }, { :remote => true, :method => :get } %>
You need to have a .js.erb file for the User controller action which will be called through the format.js call.
In show.js.erb use
$('#some_div_id').html($('<%= render :partial => "ajaxshow" %>'))
Anyhow the purpose of your action seems obscure to me.
I am trying to submit a form via AJAX. I figured this should be a simple thing, but it insists on posting as a regular form...
update
This form exists within another form, which is not AJAXified, or related? Would that cause a problem?
The error I get
ArgumentError in LinkWidgetsController#create
There was no default layout for LinkWidgetsController in
[/Users/victorstan/Sites/portrait/app/views,
/Users/victorstan/.rvm/gems/ruby-1.9.2-p290#portrait/gems/devise-1.5.1/app/views]
Relevant code:
routes
resources :profiles do
resource :link_widgets #this handles the form requests for create
member do
get 'new_link_widget' #this handles the form requests for new
end
resources :pages
end
controller
class LinkWidgetsController < ActionController::Base
before_filter :authenticate_user!
load_and_authorize_resource
def create
#link_widget = LinkWidget.new(params[:link_widget])
respond_to do |format|
if #link_widget.save
format.js { render :js => #link_widget, :status => :created, :layout => !request.xhr? }
else
logger.debug #link_widget.errors.inspect
format.js { render :js => #link_widget.errors, :status => :unprocessable_entity }
end
end
end
end
partial
%hr
= form_for [#profile, #link_widget], :remote => true, :format => :js do |f|
.field
= f.label :provider
= f.select(:provider, options_for_select(f.object.providers, f.object.provider))
.field
= f.label :link
= f.text_field :link, size: 37, class: 'input-width'
.field
= f.label :title
= f.text_field :title, size: 37, class: 'input-width'
%div
%small.form-help-text If you set an optional title, it will show up beside the icon as a link
.field
= f.label :order
= f.text_field :order, size: 2
- unless f.object.new_record?
.field
= f.label :delete
= f.check_box :_destroy
= f.hidden_field :profile_id
.actions
= f.submit 'Save', class: 'right'
.clear
If I understand what you mean by This form exists within another form, you have nested form tags (something like the following):
<form> <!-- this is a standard form -->
...
<form> <!-- this is the remote/AJAXified form -->
...
</form>
...
</form>
The HTML specification does not allow nesting of form tags. You should move the inner form to another place in your document. If that's not possible, you could try to use two submit buttons inside a single form, and then take appropriate action based on which button was pressed (you can do that on the server).
I am trying to filter Client Comments by using a select and rendering it in a partial. Right now the partial loads #client.comments. I have a Category model with a Categorizations join. This all works, just need to know how to get the select to call the filter action and load the partial with ajax. Thanks for you help.
Categories controller:
def filter_category
#categories = Category.all
respond_to do |format|
format.js # filter.rjs
end
end
filter.js.erb:
page.replace_html 'client-note-inner',
:partial => 'comments',
:locals => { :com => Category.first.comments }
show.html.erb (clients)
<% form_tag(filter_category_path(:id), :method => :put, :class => 'categories', :remote => true, :controller => 'categoires', :action => 'filter') do %>
<label>Categories</label>
<%= select_tag(:category, options_for_select(Category.all.map {|category| [category.name, category.id]}, #category_id)) %>
<% end %>
<div class="client-note-inner">
<%= render :partial => 'comments', :locals => { :com => #comments } %>
</div><!--end client-note-inner-->
Hope that makes sense.
Cheers.
It's straightforward with a simple onchange
<%= select_tag(:category, options_for_select(Category.all.map {|category| [category.name, category.id]}, #category_id), onchange => 'form.submit()') %>