0

On my site I want a form to be rendered based on what type of data the user is inputing. When I call the controller method in my view, it's outputting tons of HTML where the form should be as plain text starting from DOCTYPE to . I'm using a post form that was previous in place statically to try it out.

Controller:

def feed_form(form_type)

 form_type = %w{type_1 type_2}.include?(form_type) ? form_type : 'post'
 render "_#{form_type}_form"
end

And the view calling the action:

= feed_form 'post'

And the form:

  .feed-form
%h3 News Feed
= form_for(@post) do |f|
  = render 'shared/error_messages', object: f.object
  .field
    = f.text_area :content, placeholder: "Make your new post here..." 
  = f.submit "Post", class: "post-button"

1 Answer 1

1

You are don't supposed to call controller methods from a view.

You must prepare all the data for view rendering (e.g. all required models) in controller method, and put it into @variables.

And then in view you write all your html using already prepared @variables, you can call helpers methods from a view, but not controller's.

Try to put your def feed_form(form_type) code into a helper.

P.S.: and read something about MVC architecture.

Sign up to request clarification or add additional context in comments.

1 Comment

I forgot to mention I had "helper method: form_feed" in my controller, i thought it would provide the same functionality, but once I moved it into my helper it worked fine. I will look into helper_method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.