PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label partial-views. Show all posts
Showing posts with label partial-views. Show all posts

Wednesday, May 18, 2022

[FIXED] How to pass local variable to partial from application layout in rails 3?

 May 18, 2022     layout, partial, partial-views, ruby-on-rails     No comments   

Issue

I have a couple of variables that need to be called in all controllers. Displaying latest news in the layout footer.

I create them in application_controller.rb

@hq_news_item = NewsItem.where(:branch_code => "CORP").first
@branch_news_item = NewsItem.where(:branch_code => "MN").first

In my layouts/application.html.haml

= render :partial => "layouts/footer_news"  , :hq_news_item => @hq_news_item, :branch_news_item => @branch_news_item

And then in my layouts/_footer_news I style them

= hq_news_item.title
= hq_news_item.author.name
... etc

Here is the thing, no matter what I do - it keeps saying that hq_news_item is undefined in partial.

All my other partials work fine. I think it has to do with the fact that it's a layout not a view. Can't find anything meaningful in the docs.

Any ideas?

Thank you.


Solution

I think you need to pass the variables as local variables to the partial:

= render :partial => "layouts/footer_news", :locals => { :hq_news_item => @hq_news_item, :branch_news_item => @branch_news_item }

Otherwise Rails won't really understand what you are passing as a variable to the partial and what you are passing as an argument to the render function.



Answered By - Pan Thomakos
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to use @Html.Partial() directly on a page in MVC 3

 May 18, 2022     asp.net-mvc-3, partial, partial-views     No comments   

Issue

I want to use @Html.Partial("_partialView") to include a partial view on my page in MVC 3.

Both the page and the viewmodel have a viewmodel; thus, the following error is generated:

The model item passed into the dictionary is of type '[...]page', but this dictionary requires a model item of type '[...]partialview'.

How can I use the @Html.Partial() method while keeping the two viewmodels?


Solution

You should use this overload that allows the model object to be passed to partial view

    public static MvcHtmlString Partial(
      this HtmlHelper htmlHelper,
      string partialViewName,
      Object model
    )

By the way, do you really need to call Partial? RenderPartial is better - it writes directly to the response stream(compared to partial that returns string), so reserves the memory. Partial views can be quite big, so there's memory overhead with Partial if you do not absolutely need it.



Answered By - archil
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to put modals in partials?

 May 18, 2022     modal-dialog, partial, partial-views, ruby-on-rails, twitter-bootstrap     No comments   

Issue

I can't get how to put modals in partials. Help me, please !

I have button:

 <a class="btn btn-large" data-toggle="modal" href="#show_me_modal"
 onclick="printpage()">Name of button<sup>TM</sup> message in action</a>

and div modal:

    <div class="modal hide fade" id="show_me_modal">
     <div class="modal-header">
      <a class="close" data-dismiss="modal">×</a>
         </div>
      <div class="modal-body">
      <p>some text</p>

      <ul class="media-grid">
        <%= image_tag("/images/pic02.png") %>
      </ul>

    </div>

    <div class="modal-footer">
      <b><%= link_to "#", '#' %></b>          
    </div>
  </div>

Solution

I have created ModalHelper for one project. It helps dynamically create modals and links to them. Hope it helps you:

Helper code:

Create file app/helpers/modal_helper.rb

module ModalHelper
    def modal(css_id, header_text, hidden = true, &block)
        content_tag(:div, :class => 'modal', :id => css_id, :style => ("display:none;" if hidden) ) do
            concat modal_header(header_text)
            concat modal_body(&block)
        end
    end

    def modal_button(link_text, href)
        modal_caller link_text, href, :button
    end

    def modal_link(link_text, href)
        modal_caller link_text, href
    end

    private

    def modal_caller(link_text, href, type = nil)
        options = { :"data-toggle" => "modal" }
        options.merge!({ :class => "btn" }) if type == :button
        link_to link_text, "#" + href, options
    end

    def modal_header(header_text)
        content_tag(:div, :class => 'modal-header') do
            concat content_tag(:button, 'x', :class => 'close', :"data-dismiss" => 'modal')
            concat content_tag(:h3, header_text)
        end
    end

    def modal_body
        content_tag(:div, :class => 'modal-body') do
            yield
        end     
    end 
end

Link to modal:

Generates link to your modal.

<%= modal_link('Sign up', "myModal") %>

Rendering to modal:

Contains code you want to render.

<%= modal('myModal', 'Registration') do %>
    <% render 'devise/registrations/register' %>
<% end %>


Answered By - thesis
Answer Checked By - Terry (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, May 17, 2022

[FIXED] How can I render Partial views in asp.net mvc 3?

 May 17, 2022     asp.net-mvc, asp.net-mvc-3, partial, partial-views     No comments   

Issue

I have some data in ViewData.Model, and in my views I want to write a partial view and to pass their current model I have in my page.

How I can pass their current ViewData.Model and render them through the location of partials?


Solution

Create your partial view something like:

@model YourModelType
<div>
  <!-- HTML to render your object -->
</div>

Then in your view use:

@Html.Partial("YourPartialViewName", Model)

If you do not want a strongly typed partial view remove the @model YourModelType from the top of the partial view and it will default to a dynamic type.

Update

The default view engine will search for partial views in the same folder as the view calling the partial and then in the ~/Views/Shared folder. If your partial is located in a different folder then you need to use the full path. Note the use of ~/ in the path below.

@Html.Partial("~/Views/Partials/SeachResult.cshtml", Model)


Answered By - David Glenn
Answer Checked By - Katrina (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing