How to add .erb file in existing model in rhoMobile

陌路散爱 提交于 2019-12-12 05:15:13

问题


I have created a simple application and edited the index.erb file so that I have a simple view with a text box and a button.

Now when i click on that button i want it to navigate to a new view. I know that we can add models and in that models we have different .erb files. but i want to create a single .erb file or add it to an existing model so that i can change edit the view and call that view as i press the button.

Is it like for every screen we have to create a model??

I dont know how to do the same, i tried searching but no help so far.


回答1:


It is not that for every screen you've to create a model rather the reverse. Every model for which you need interfaces you'll create views.

Why don't you start with this guide and move ahead understanding the basics.

Other than CRUD interfaces? You can add a view file directly to the view folder of the controller this model is associated with. For example, if the model is post.rb in app/models and it has a corresponding controller posts_controller.rb in app/controllers and it has corresponding views in app/views/posts then you can add your view to app/views/posts folder with a corresponding method in the controller which will render that view provided there is a route for that in the config/routes.rb file.

Say I want to add a landing_page.html.erb view to Post. I would add a method in posts_controller.rb (although, this is not mandatory. But, might be useful for you to check some conditions before rendering the view):

class posts_controller < ApplicationController
  ...
  def landing_page
  end
end

Add a view in app/views/posts directory:

# app/views/posts/landing_page.html.erb

Add a route to the config/routes.rb file:

map.resources do
  member do
    get :landing_page
  end
end

Now, you can access the page at http://localhost:3000/posts/:id/landing_page.




回答2:


No you don't have to create models for each view(.erb). In case you want to add new view to existing model, just add new method(def) into the controller(.rb), and new view(.erb) with the same name as of the new method.

let say there exist a model DemoController.rb in app/Demo. you can add new method to it as like

class DemoController < Rho::RhoController
  ...
  def index
  end

  def new_method
  end

end

To navigate from the index view to the new_method, you can write

<button onclick="location.href='/app/Demo/new_method'">new method</button>

or

<button onclick="location.href='<%= url_for :action => :new_method %>'"
>new method</button>


来源:https://stackoverflow.com/questions/8601582/how-to-add-erb-file-in-existing-model-in-rhomobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!