interact between controller and view in rails - ruby-on-rails

I created a form that you can type text with a "post" button and i want that when you type something and press the post button it will show what you wrote.
My question is: What is the code to connect between the view and the controller?
This is the view i already created:(i also generated a home controller with a showmsg action)
<h1 align="center">MicroBlog</h1>
<br><br>
<div align="center">
<%= form_tag( {:controller => 'home', :action => 'showmsg'}, :method => "post") do %>
<%= text_field_tag(:p,#postword) %>
<%= submit_tag("post") %>
<% end %>
</div>
how should the showmsg action would look like so i could show the msg?
thank you very much!

Your controller will need to access the param being sent to it, assign it to an instance variable, which your view will then be able to access.
class HomeController < ApplicationController
def showmsg
#message = params[:p]
end
end
/app/views/home/showmsg.html.erb
...
<%= #message %>
...

Related

How to render show action as a partial

I have a search form. when i search for a record, the result goes to the show page as expected , but i want the result to render on the same page where ever i put the form partial
I want to create a partial that will render the show action on the same page with the form
Here is the shipment controller
class shipmentsController < ApplicationController
def show
#shipment = Shipment.find_by_trackCode(params["trackCode"])
unless #shipment.present?
end
......
Home controller index.html.erb
<%=render 'shipments/form' %>
Views/shipments/_form.html.erb
<%= form_tag search_path, :controller => 'shipments', :action => 'show', :method => 'get' do %>
<%= text_field_tag :trackCode %>
<%= submit_tag 'Track' %>
<% end %>
The present code will dispaly the result on the show page but I want the search result to be rendered using a partial so that i can render it where ever i use the form partial.
How can i archive that ?
are you really sure it's a good idea to use your show view as a partial? now if you are... you could rename your file _show.html.erb and reference it passing the folder name <%=render 'shipments/show' %> but you'd also have to change the loop to call the model Shipment.all.each instead of #shipment
render template: 'shipments/show', locals: {shipment: #shipment}
use variable shipment instead of #shipment in show.html.haml

search form taking me to a second page

This is the form that I made from Clients_indexI am using Elastic Search to find a client's name. I created a search for but as soon as you click search it takes me to a second page.
It takes me here
Here is my view for Clients/_index.html.erb
<label for="existing-ads-client-name" class="big">CLIENT INFORMATION</label><br>
<div class="existing-ads-client-name">
<%=form_tag clients_path,class: 'search-client', method: :get do |f| %>
<div id="client-name">
<%=text_field_tag :name, params[:name] %>
</div>
<%=button_tag "Search", class: 'btn btn-default', name: nil %>
<% end %>
</div>
I am rendering it from AdGroups/index.html.haml
<div class="module existing-ads">
<div class="first-column">
<div class="field-content">
=render 'clients/index'
And here is my Clients controller
class ClientsController < ApplicationController
def index
#clients = Client.search((params[:name].present? ? params[:name]: '*')).records
render :partial => 'clients/index', locals: {clients: #clients}
end
end
Any idea why?

How to call a controller method to populate partial on root page?

I've got a controller method:
def latest_blogs
#latest_blogs = BlogEntry.all.order('dato DESC').limit(4)
end
A root html.erb file which acts as my home page:
<div class="body-box">
<div class="body-content">
<%= render :partial => 'blog_list' %>
</div>
</div>
The blog_list partial:
<div class="blog-list">
<%= latest_blogs.each do |blog| %>
<%= render :partial => "blog_preview", locals: {blog: blog} %>
<% end %>
</div>
And the blog_preview partial which is just a stub for now:
<div class="blog-preview">
<%= blog.title %>
</div>
My routes.rb entries:
resources :blog_entries
root :to => 'home#blog_home', :as => 'root'
How can I tell my app when it loads the root page to present latest_blogs? Thanks.
I would start with: this method should not be in the controller. Put it in a model instead
class BlogEntry < ActiveDirectory::Base
def self.latest (n = 4)
all.order('dato desc').limit(n)
end
Then just use it as:
BlogEntry.latest
Note that this method will also be available on relations:
user.blog_entries.latest(1) #=> returns last user blog entry
You can declare the latest_blog as helper method. Please try the following:
class BlogsController < ApplicationController
def latest_blogs
#latest_blogs = BlogEntry.all.order('dato DESC').limit(4)
end
helper_method :latest_blogs
end
Is latest_blogs your actual controller action, or is it just some method you're exposing to the view? If it's just some method you're exposing then you should make it a helper method.
def latest_blogs
#latest_blogs ||= BlogEntry.all.order('dato DESC').limit(4)
end
helper_method :latest_blogs
Note that I changed the = to ||= in here. I think that's better in this case so that if you happen to call the helper multiple times then it won't re-evaluable it repeatedly.
And FYI, you can also clean up your markup code a little bit. Your root html file could do:
<div class="body-box">
<div class="body-content">
<div class="blog-list">
<%= render partial: 'blog_preview', collection: latest_blogs %>
</div>
</div>
</div>
Then _blog_preview.html.erb:
<div class="blog-preview">
<%= blog_preview.title %>
</div>

How can I display a list of child records?

I have a table of accounts and venues where an account can have many venues.
I'm displaying all the accounts as partials on the accounts index page and would like for each one to include the names of the venues linked to them.
Heres what I have:
account partial
<%= link_to free_account do %>
<div class="account_partial">
<span class="account_header"><%= free_account.name %></span> - <span class="free_account_highlight">(<%= free_account.role %>)</span><br>
<%= render :partial => 'venues/account_venue', :collection => #account.venues %>
</div>
<% end %>
account_venue partial
<%= venue.name %>
I'm getting this error:
NoMethodError in Accounts#index
undefined method `venues' for nil:NilClass
Extracted source (around line #12):
12: <%= render :partial => 'venues/account_venue',
:collection => #account.venues %>
Any help would be much appreciated!
edit
accounts_controller
class AccountsController < ApplicationController
load_and_authorize_resource
def index
#accounts = Account.all(:include => :venues)
end
end
accounts index.html.erb
<div id="narrow_container">
<div class="free_accounts_container">
<h2 class="show_orange">Free accounts</h3>
<%= render :partial => 'free_account', :collection => #accounts %>
</div>
<div class="premium_accounts_container">
<h2 class="show_orange">Premium accounts</h3>
<%= render :partial => 'premium_account', :collection => #accounts %><br><br>
</div>
<div class="clearall"></div>
<div class="button">
<%= link_to 'add account', new_account_path %>
</div>
</div>
_free_account.html.erb
<%= link_to free_account do %>
<% if free_account.role == "free" %>
<div class="account_partial">
<span class="account_header"><%= free_account.name %></span> - <span class="free_account_highlight">(<%= free_account.role %>)</span><br>
<div class="owner_details">
<span class="pre_account_highlight">Owners username:</span><span class="account_highlight"><%= free_account.user.username %></span>
<span class="pre_account_highlight">Owners e-mail:</span><span class="account_highlight"><%= free_account.user.email %></span>
</div>
<div class="account_details">
</div>
<%= render :partial => 'venues/account_venue', :collection => #account.venues %>
</div>
<% else %>
<% end %>
<% end %>
update
If I change the partial call to:
<%= render :partial => 'venues/account_venue', :collection => #accounts %>
and the account_venue partial to just read 'test' it loads without error but displays the word test 4 times (theres 4 account records) if I add a new account record it displays the word test 5 times.
You need to set #account to something in your controller, and that model instance should be joined to (or include) the Venues model. Like so:
#account = Account.find(params[:id], :include => :venues)
Edit: I take it you have already set up your Account and Venue models with has_many and belongs_to relationships?
Edit two: I see now that you're trying to access Accounts#index, in which case the code above should be changed to something like (since we're not looking at one specific account):
#accounts = Account.all(:include => :venues)
Edit three: Now that you've posted the controller and partials code as well, a couple of things stand out; When rendering a partial using a collection the resulting object inside the partial derives its name from the partial and not the collection. From the Rails Guides:
"When a partial is called with a pluralized collection, then the
individual instances of the partial have access to the member of the
collection being rendered via a variable named after the partial."
In your partial _account_venue.html.erb you have <%= venue.name %> - this needs to be changed to <%= account_venue.name %>.
Secondly, in _free_account.html.erb, where you call the account_venue partial, you're referring to a collection object named #account - where does this come from? Since the free_account partial is also called with a collection, the object you should be using will be called free_account - indeed you are referencing it by this name earlier in the same partial when you do <%= free_account.name %>. So the render partial call should look like this:
<%= render :partial => 'venues/account_venue', :collection => free_account.venues %>
Hope this helps!
Looks like your not assigning #account to anything. Should this be free_account instead?

Passing Form Values into a controller in Rails

say I have a text field like the following in a view called 'search':
<%= text_field_tag(:lookup) %>
how do I submit this ':lookup' value and pass it into the controller called 'search' and assign it to a variable?
It's a basic problem, but being a noob, it's difficult ;)
That will be accessible in the controller as
params[:lookup]
Your controller could look something like this:
class SearchesController < ActionController::Base
def search
lookup = params[:lookup]
#models = Model.find_by_lookup(lookup)
end
end
And your view should look like this:
<%= form_tag searches_path do %>
<label for="lookup">Lookup</label>
<%= text_field_tag :lookup %>
<%= submit_tag "Submit" %>
<% end %>

Resources