简体   繁体   中英

Ruby on Rails: Passing a value to the model

I've been having issues with the MVC flow for a little while now, I think I'm close to having this functional. How can I pass a value to the model, from either the view or the controller, so that I can perform a logic check in the model?

I would like to pass a value to, or set the value of, page_title. Then I will use that value to determine which array should be used from the steps function.

subject.rb

   def current_step
    @current_step || steps.first
   end

   def page_title(the_title)
      @page_title = the_title
   end

   def steps
      if page_title == 'Baseline'
       %w[sfmfa ]
      elsif page_title == 'Treatment Completion'
         %w[smfa phq whoqol_bref ]
      elsif page_title == '6 Month' 
         %w[smfa phq  ]
      else
        %[#{self.page_title}]
      end

   end

Here is part of the view:

baseline.html.erb

    <%= form_for(@subject) do |f| %>
        <%= render "base_#{@subject.current_step}", :f => f %>

I'm trying to figure out if I should pass a value within the same line that I call this render, or if I need to do it before the form_for. I'm all mixed up right now.

I could also call params[:title] in the controller, but so far I haven't found a way to send that value to the model.

All I need is to be able to select the steps value, or set it somewhere else.

Thank you for your time.

If you want to access @subject.steps within the rendered partial, then you would need to call @subject.page_title anywhere before the render call.

I'm not exactly sure what your use case is here, but it would be easier to follow if steps took a title parameter, as opposed to setting the "state" of your subject instance with another method.

Also, this smells like presentation formatting logic that maybe is just better off in the view or in a view helper.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM